GraphQL is query language for APIs, but what are the advantages and how would one implement such in their microservices/APIs. In this session, I will go through the basics of GraphQL, different aspects of GraphQL and architecture of such APIs. There will be a demo/live-coding on, how 4 different ways we can implement GraphQL for a Springboot microservice/API. Lots of examples, live coding and helpful comparison on structure, usage and implementations of GraphQL in Springboot & Java world.
2. What’s in store today
GraphQL
What
Queries & Mutation
Schemas & Types
Show me some code
graphql-java
graphql-java-kickstart
graphql-dgs-framework
spring-graphql
What’s better
Implementation easiness – code,
generation, error handling, testing,
clients
N+1 problem
3. GraphQL is a query language for your API, and a
server-side runtime for executing queries.
Backed by your existing code and data.
It was designed by Facebook to get around
common constraints in fetching data via REST.
It was open sourced by Facebook in 2015.
Nothing to do with Graph DB.
Not a replacement of REST.
Not going to magically solve every API design
issues.
What’s GraphQL
4. Schema Query Results
Schema , Query & Results
GraphQL provides description of the data in your API, gives clients the power to ask for exactly
what they need.
6. GraphQL vs REST API vs SOAP
https://trends.google.com/trends/explore?date=today%205-y&q=%2Fg%2F11cn3w0w9t,REST%20API,%2Fm%2F077dn
7. Hierarchical & Aggregator Strongly Typed
Validation & Type check
out-of-the-box
API evolution without
versioning
GraphQL
Characteristics
8. Query & Mutation
• Ask what you need.
• Expand the same
query.
• Send arguments.
• Use Aliases, Fragment
Schemas & Types
• GraphQL schema language -
allows us to talk about
GraphQL schemas in a
language-agnostic way.
• Scalar : Int, String, Boolean, ID,
Float
• Lists & Non-Null
• Union, Interfaces, Input Types
21. 21
DataFetchers
Piece of code that fetches the data
1. Define a @Component which defines
datafetching methods.
2. Register them in while building/parsing
the schema
graphql-java
22. 22
DataFetchers
Piece of code that fetches the data
1. Define @Component which implements
GraphQLQueryResolver.
2. For field-level define a @Component
which implements GraphQLResolver<T>.
graphql-java-kickstart
23. 23
DataFetchers
Piece of code that fetches the data
1. Define a @DgsComponent.
2. Map the schema operations with
@DgsData
dgs
24. 24
DataFetchers
Piece of code that fetches the data
1. Define a @Controller.
2. Map the schema operations with
@SchemaMapping or @QueryMapping
spring-graphql
25. 25
DataLoaders
Piece of code that is responsible for N+1 query issue
1. Define a BatchLoader which
return s a CompletableFuture.
2. Define a DataLoaderRegistry
@Bean which registers the
Loader.
3. Define a DataFetcher using that
Loader.
graphql-java
26. 26
DataLoaders
Piece of code that is responsible for N+1 query issue
1. Define @Component which
defines a loader and adds it to
the registry.
2. Define a @Component which
implements
GraphQLServletContextBuilder
and associate the registry.
3. Define a @Component which
invokes the dataloader.
graphql-java-kickstart
27. 27
DataLoaders
Piece of code that is responsible for N+1 query issue
1. Define a @DgsDataLoader.
2. Map the schema operations with
@DgsData
dgs
28. 28
DataLoaders
Piece of code that is responsible for N+1 query issue
1. Register (Mapped)BatchLoader.
2. Map it to the method that can be
invoked for a list of keys.
3. Load the dataloader with
@SchemaMapping/@QueryMapping
spring-graphql
30. 30
Error Handling
Piece of code that handles exceptions/errors
• Enable ExceptionHandler property
and handle them in a Spring way.
OR
• Implement GraphQLErrorHandler
and manage the errors in overridden
method.
graphql-java-kickstart
31. 31
Error Handling
Piece of code that handles exceptions/errors
• Define a @Component which
implements
DataFetcherExceptionHandler
dgs
32. 32
Error Handling
Piece of code that handles exceptions/errors
1. Define a Config bean and register
the exception handlers.
spring-graphql
33. 34
Client
How to call a GraphQL API
1. No defined way.
2. Needs text templates to generate
requests.
graphql-java
34. 35
Client
How to call a GraphQL API
1. Gives a Webclient library.
2. Needs text templates to generate
requests.
graphql-java-kickstart
35. 36
Client
How to call a GraphQL API
1. GraphQLClient uses String as query
which is same as the previous two.
2. Code generation using
Gradle/Maven. This is the type-safe
option.
dgs
36. 37
Client
How to call a GraphQL API
1. Define WebGraphQlClient object.
2. Options are http, websocket,
rsocket.
3. Executing the query gives Mono
object.
4. Responses can be mapped to an
custom object.
spring-graphql
37. Testing & Code Generation
grpahql-java graphql-java-kickstart dgs spring-graphql
X X
1. Code generation using
Gradle/Maven. This is the
type-safe option.
2. Use the generated classes
to build request and test
using
DgsAutoConfiguration
class.
1. No Code generation.
2. @AutoConfigureXXGraph
QlTester to initialize
context.
3. Responses can be mapped
to custom objects.
38. graphql-java
graphql-java-
kickstart
dgs
spring-graphql
• Is there since long, so
evolved and matured.
• A lot of Boilerplate
code just to write a
simple API.
• Library for Spring boot
is archieved.
• Not a easy way to Unit
Test everything you
write.
• No Clients avaiable.
Need to use text
templates to create
queries.
• Matured but missing
extended
documentation.
• Still a bit of Boilerplate
code.
• Directly usable within
Springboot. Although
other modules
available to work on
Java only
environments.
• Not a easy way to UT
everything you write.
• Basic Client avaiable.
• Not sure about
maturity, but Netflix is
behind this, so faith is
there.
• No Boilerplate code.
• Directly usable within
Springboot.
• You can UT everything
you write.
• Client avaiable.
• Code generation using
Gradle/Maven.
• Just released not
enough usage data
available.
• No Boilerplate code.
• Spring is behind this.
• You can UT everything
you write.
• Client avaiable.
• No Code generation.
Final words