A walk in GraphQL
Spring boot setup
The Spring Boot GraphQL Starter combined with the GraphQL Java Tools library offer a fantastic way to get a GraphQL server running in a very short time, we need only write the code necessary for our resolvers and services.
Requirements
- Java 1.8
AdoptOpenJDK 8 (LTS) Installation
Installers are currently available for Windows®, Linux®, and macOS® JDK and JRE packages. Installation steps are covered in the following sections:
Project Dependencies
graphql-spring-boot-starter
to turn your boot application into GraphQL server. (see graphql-java-servlet)playground-spring-boot-starter
to embed GraphQL Playground tool for schema introspection and query debugging (see GraphQL Playground)graphql-java-tools
a schema-first tool that allows us to use the GraphQL schema language to build your graphql-java schema (see graphql-java-tools). Inspired by apollo graphql-tools, it parses the given GraphQL schema and allows you to BYOO (bring your own object) to fill in the implementations.
Spring Boot will automatically pick these up and set up the appropriate handlers to work automatically. By default, this will expose the GraphQL Service on the /graphql
endpoint of our application and will accept POST requests containing the GraphQL Payload. This endpoint can be customised in our application.properties
file if necessary.
Keep in mind
- The
GraphQL Tools
library works by processing GraphQL Schema files to build the correct structure and then wires special beans to this structure. - The
Spring Boot GraphQL starter
automatically finds these schema files, we just need to save these files with the extension “.graphqls” on the classpath. - Query and Mutation objects are root GraphQL objects. They don’t have any associated “data” class. In such cases, the “resolver” classes would implement
GraphQLQueryResolver
orGraphQLMutationResolver
. Beans Representing Types:
Every complex type in the GraphQL server is represented by a Java bean. Fields inside the Java bean will directly map onto fields in the GraphQL response based on the name of the field.- Sometimes, the value of a field is non-trivial to load. This might involve database lookups, complex calculations, or anything else. GraphQL Tools has a concept of a “Field Resolver” that is used for this purpose.
The field resolver is any bean in the Spring Context that has the same name as the data bean, with the suffix “Resolver”, and that implements the
GraphQLResolver
interface. Methods on the field resolver bean follow all of the same rules as on the data bean but are also provided the data bean itself as a first parameter. If a field resolver and the data bean both have methods for the same GraphQL field then the field resolver will take precedence.
Run Application
Using Maven Wrapper
The Maven Wrapper is an excellent choice for projects that need a specific version of Maven (or for users that don’t want to install Maven at all).
- Open a terminal
- go to the java exercise directory
- run
./mvnw spring-boot:run
(linux) ormvnw.cmd spring-boot:run
(windows) to start the GraphQL server.
Using the IDE (IntelliJ IDEA)
- Open IntelliJ IDEA
- Select “Open or Import” option
- go to the exercise directory and choose the “java” folder
- run DemoGraphQlApplication
Testing GraphQL queries
To display a GUI for editing and testing GraphQL queries and mutations against the server you can open your browser and type http://localhost:8080/playground
Inspect the Database
H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. After starting the application, you can navigate to: http://localhost:8080/h2-console/login.jsp and enter the following information:
- JDBC URL: jdbc:h2:mem:graphQL
- User Name: sa
- Password: <blank>