This provides examples to use GraphQL in a spring-boot based project.
GraphQL is a query language to retrieve data from a server. It is an alternative to REST, SOAP or gRPC in some way. It gives more power and flexibility to your API consumer to query only required data.
GraphQL-Java is the Java (server) implementation for GraphQL. This provides a low level libraries to define GraphQL schema and how the actual data for a query is fetched. You have to write a lot of boilerplate code if you use graphql-java.
Fortunately, We have two libraries available which provides a wrapper on top of graphql-java and saves use from writing boilerplate code. They are:-
- GraphQL Java Kickstart
- Netflix DGS
GraphQL-Java-Kiskstart provides a wrapper on top of graphql-java and has following features:-
- Provide comprehensive Spring boot configuration to customize GraphQL Java Server
- Auto-detect schema files in
src/main/resources/*.*/*.graphqls
directory. This is where you write GraphQL schema, queries and mutation. - Concepts of Resolver. Implement
GraphQLQueryResolver
,GraphQLMutationResolver
andGraphQLResolver<T>
to specify how to fetch data for the queries, mutation and nested data respectively. - Easy integration with build tools such as GraphiQL, PlayGround and Voyager by adding runtime dependency. Provide comprehensive Spring Boot Configurations to customize these tools.
- Easy to write integration test using
GraphQLTestTemplate
provided by test dependency. - Excellent tutorial series by Philip Starritt which gives you quick start.
Netflix-DGS is developed by Netflix on top of graphql-java and recently made it public to use. It has following features:-
- Do not provide Spring boot based configurations
- Auto-detect schema files in
src/main/resources/schema/*.*/*.graphqls
directory. This is where you write GraphQL schema, queries and mutation. - Concepts of DataFetcher. Provide annotations
@DgsComponent
at class level and@DgsQuery
,@DgsMutation
,@DgsData
at method level to specify how to fetch data for the queries, mutation and nested data respectively. - Provide integration with GraphiQL
- Provide good support to write unit and integration test cases using
DgsQueryExecutor
- Follow example to quick start