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. How 4 different ways we can implement GraphQL for a Springboot microservice/API.
2. SOHAM
DASGUPTA
Father and Football lover
Tech Enthusiast/Programmer/Architect
Speaker & Blogger
Twitter : @iamsoham
LinkedIn: dasguptasoham
Github: sohamda
Medium: @iam.soham
3. 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
4. Let me tell you story...
Provider names and associated Services
Provider (names)
Services
Services with associated Provider
Valid Services
Services + Provider + logged in User region
Providers + Services + logged in User region
Providers + Services + No email addr for unauthorized
5. 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.
Nothing to do with Graph DB.
Not a replacement of REST.
Not going to magically solve every API design
issues.
What’s GraphQL
6. 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.
8. Hierarchical & Aggregator Strongly Typed
Validation & Type check
out-of-the-box
API evolution without
versioning
GraphQL
Characteristics
9. 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
10. N+1 queries Caching complexity
Community presence Pagination
GraphQL
Maturity
11. Show me some code
graphql-java
graphql-java-kickstart
graphql-dgs-framework
spring-graphql
22. 22
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
23. 23
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
24. 24
DataFetchers
Piece of code that fetches the data
1. Define a @DgsComponent.
2. Map the schema operations with
@DgsData
dgs
25. 25
DataFetchers
Piece of code that fetches the data
1. Define a @Controller.
2. Map the schema operations with
@SchemaMapping or @QueryMapping
spring-graphql
26. What’s N+1
when the data access framework executed N additional SQL statements to fetch the same data that could have been retrieved
when executing the primary SQL query
27. 27
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
28. 28
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
29. 29
DataLoaders
Piece of code that is responsible for N+1 query issue
1. Define a @DgsDataLoader.
2. Map the schema operations with
@DgsData
dgs
30. 30
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
32. 32
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
33. 33
Error Handling
Piece of code that handles exceptions/errors
• Define a @Component which
implements
DataFetcherExceptionHandler
dgs
34. 34
Error Handling
Piece of code that handles exceptions/errors
1. Define a Config bean and register
the exception handlers.
spring-graphql
35. 35
Client
How to call a GraphQL API
1. No defined way.
2. Needs text templates to generate
requests.
graphql-java
36. 36
Client
How to call a GraphQL API
1. Gives a Webclient library.
2. Needs text templates to generate
requests.
graphql-java-kickstart
37. 37
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
38. 38
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
39. 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. @AutoConfigureGraphQlT
ester to initialize context.
3. Responses can be mapped
to custom objects.
40. 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.
• Dataloader needs list
of keys.
• 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