/graphql-java-spring-boot-example

Sample GraphQL server implemented with graphql-java and Spring Boot

Primary LanguageJavaMIT LicenseMIT

graphql-java-spring-boot-example

Sample app for my tutorial Building a GraphQL Server with Spring Boot.

For testing Java 11 can be usede with this repo (The original repo seemst to have been tested with Java 9).

Clone this repo and execute mvnw spring-boot:run. Or inside an IDE, execute the class com.example.DemoGraphQL.DemoGraphQlApplication.

You can go to http://localhost:8080/h2-console/login.jsp and enter the following information:

  • JDBC URL: jdbc:h2:mem:testdb
  • User Name: sa
  • Password:

Run Examples

You may use curl on the command line

curl -X POST -H "Content-Type: application/json" -d '{"query": "{findAllBooks{id title}}"}' http://localhost:8080/graphql

Or the built-in graphical user interface http://localhost:8080/graphiql to start executing queries.

{
  findAllBooks {
    id
    isbn
    title
    pageCount
    author {
      firstName
      lastName
    }
  }
}

Or:

mutation {
  newBook(
    title: "Java: The Complete Reference, Tenth Edition", 
    isbn: "1259589331", 
    author: 1) {
      id title
  }
}

Run Error Case Examples

Ask for non-existing attribute

{
  findAllBooks {
    id
    title
    publisher
  }
}

Try to update a non-existing book

mutation {
  updateBookPageCount(pageCount: 1313 id: 3)
     {
      id
      title
      pageCount
    }
}

A Java Client would be nice ...

SpringBoot provides support to intuitively build GraphQL server apps. Unfortunately, java client support is lacking. The list below provides some pointers to using Java for building GraphQL clients.

GraphQL Java Client Links

TODO

License

MIT