Skip to content

ns-code/spring-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot GraphQL

Description

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.

Running

.\gradlew bootRun

Integration Testing

.\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"}}}}


E2E Testing QueryMapping

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"
      }
    }
  }
}

E2E Testing MutationMapping

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"
      }
    }
  }
}

About

This project extends the official Spring GraphQL demo by adding a MutationMapping

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages