This project has a helloworld style demo for Spring Boot support for GraphQL. This demo extends the official demo at https://spring.io/guides/gs/graphql-server by adding a mutation mapping.
.\gradlew bootRun
.\gradlew test
Alternatively, using curl:
QueryMapping:
curl -X POST "http://localhost:8080/graphql" -H "Content-Type: application/json" -d "{\"query\": \"query { bookById(id:\\\"book-1\\\") { id name pageCount author { id firstName lastName } } }\"}"
gives
{"data":{"bookById":{"id":"book-1","name":"Effective Java","pageCount":416,"author":{"id":"author-1","firstName":"Joshua","lastName":"Bloch"}}}}
MutationMapping:
curl -X POST "http://localhost:8080/graphql" -H "Content-Type: application/json" -d "{ \"query\": \"mutation AddBook($input: BookInput!) { addBook(book: $input) { id name pageCount author { id firstName lastName } } }\", \"variables\": { \"input\": { \"id\": \"book-44\", \"name\": \"The Hitchhiker's Guide to GraphQL\", \"pageCount\": 420, \"author\": { \"id\": \"authid44\", \"firstName\": \"Douglas\", \"lastName\": \"Adams\" } } }}"
gives
{"data":{"addBook":{"id":"book-44","name":"The Hitchhiker's Guide to GraphQL","pageCount":420,"author":{"id":"authid44","firstName":"Douglas","lastName":"Adams"}}}}
Access the UI http://localhost:8080/graphiql?path=/graphql
Running
query bookDetails {
bookById(id: "book-1") {
id
name
pageCount
author {
id
firstName
lastName
}
}
}
should give
{
"data": {
"bookById": {
"id": "book-1",
"name": "Effective Java",
"pageCount": 416,
"author": {
"id": "author-1",
"firstName": "Joshua",
"lastName": "Bloch"
}
}
}
}
Running
mutation AddBook {
addBook(
book: {
id: "book-4"
name: "The Hitchhiker's Guide to GraphQL"
pageCount: 420
author: {
id: "author-5"
firstName: "Douglas"
lastName: "Adams"
}
}
) {
id
name
pageCount
author {
id
firstName
lastName
}
}
}
should give
{
"data": {
"addBook": {
"id": "book-4",
"name": "The Hitchhiker's Guide to GraphQL",
"pageCount": 420,
"author": {
"id": "author-5",
"firstName": "Douglas",
"lastName": "Adams"
}
}
}
}