SlideShare a Scribd company logo
GraphQL-ify your APIs
SOHAM
DASGUPTA
Father and Football lover
Tech Enthusiast/Programmer/Architect
Speaker & Blogger
Twitter : @iamsoham
LinkedIn: dasguptasoham
Github: sohamda
Medium: @iam.soham
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
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
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
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.
https://landscape.graphql.org/card-mode?category=graph-ql-adopter&grouping=category&style=borderless
Who is using it ?
Hierarchical & Aggregator Strongly Typed
Validation & Type check
out-of-the-box
API evolution without
versioning
GraphQL
Characteristics
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
N+1 queries Caching complexity
Community presence Pagination
GraphQL
Maturity
Show me some code
graphql-java
graphql-java-kickstart
graphql-dgs-framework
spring-graphql
https://github.com/sohamda/serivce-repository-swagger
graphql-java https://www.graphql-java.com/documentation/getting-started
https://github.com/graphql-java - Version 18.2 : Last updated on June’22
https://github.com/graphql-java/graphql-java-spring
This project is archived in favor of the official Spring GraphQL integration
https://github.com/sohamda/graphql-java
graphql-java- kickstart https://graphql-java-kickstart.com/
https://github.com/graphql-java-kickstart
Version 14 : Last updated on Aug’22
https://github.com/sohamda/graphql-java-kickstart
dgs-framework
https://netflix.github.io/dgs/
https://github.com/Netflix/dgs-framework
Version 5.4.1 : Last updated on Oct’22
https://github.com/sohamda/graphql-dgs-netflix
Spring GraphQL https://docs.spring.io/spring-
graphql/docs/current/reference/html/
https://github.com/spring-projects/spring-graphql
Version 1.0.2 : Last updated on Sep’22
2-11-2022
20
spring-graphql application structure
H2
Providers(1)
Services(*)
JPA
Repository
Service
spring-graphql
BatchLoaderRegistry
Controller
/graphql/providerservice
SchemaMapping
MutationMapping
https://github.com/sohamda/spring-graphql
What’s better
Implementation easiness – code, generation, error
handling, testing, clients
N+1 problem
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
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
DataFetchers
Piece of code that fetches the data
1. Define a @DgsComponent.
2. Map the schema operations with
@DgsData
dgs
25
DataFetchers
Piece of code that fetches the data
1. Define a @Controller.
2. Map the schema operations with
@SchemaMapping or @QueryMapping
spring-graphql
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
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
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
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
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
31
Error Handling
Piece of code that handles exceptions/errors
1. Implement GraphQLError
graphql-java
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
Error Handling
Piece of code that handles exceptions/errors
• Define a @Component which
implements
DataFetcherExceptionHandler
dgs
34
Error Handling
Piece of code that handles exceptions/errors
1. Define a Config bean and register
the exception handlers.
spring-graphql
35
Client
How to call a GraphQL API
1. No defined way.
2. Needs text templates to generate
requests.
graphql-java
36
Client
How to call a GraphQL API
1. Gives a Webclient library.
2. Needs text templates to generate
requests.
graphql-java-kickstart
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
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
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.
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
Soham Dasgupta
• Twitter : @iamsoham
• LinkedIn: dasguptasoham
• Github: sohamda
• Medium: @iam.soham
Slides: https://bit.ly/3B02NSZ Code : https://bit.ly/3ofzep2

More Related Content

PPTX
GraphQL_devoxx_2023.pptx
PDF
GraphQL-ify your APIs
PPTX
GraphQL-ify your APIs
PPTX
GraphQL-ify your APIs - Devoxx UK 2021
PDF
GraphQL and its schema as a universal layer for database access
PDF
Code-first GraphQL Server Development with Prisma
PDF
Spring GraphQL
PDF
GraphQL across the stack: How everything fits together
GraphQL_devoxx_2023.pptx
GraphQL-ify your APIs
GraphQL-ify your APIs
GraphQL-ify your APIs - Devoxx UK 2021
GraphQL and its schema as a universal layer for database access
Code-first GraphQL Server Development with Prisma
Spring GraphQL
GraphQL across the stack: How everything fits together

Similar to GraphQL-ify your API - JFall 2022 (20)

PDF
Tutorial: Building a GraphQL API in PHP
PDF
GraphQL the holy contract between client and server
PDF
How easy (or hard) it is to monitor your graph ql service performance
PPTX
GraphQL & DGraph with Go
PPTX
An intro to GraphQL
PDF
Training Series: Build APIs with Neo4j GraphQL Library
PDF
GraphQL + relay
PPTX
React inter3
PDF
GraphQL & Ratpack
PPTX
Optimization and fault tolerance in distributed transaction with Node.JS Grap...
PPTX
How to provide a GraphQL API - I want it that way
PDF
Grails 101
PDF
Graphql Overview By Chirag Dodia
PDF
Building Fullstack Serverless GraphQL APIs In The Cloud
PDF
Training Week: GraphQL 2022
PPTX
GraphQL API Gateway and microservices
PDF
Simplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) Extension
PDF
Implementing OpenAPI and GraphQL services with gRPC
PDF
Getting Started with Spring for GraphQL
PDF
What could go wrong with a GraphQL query and can OpenTelemetry help? KubeCon...
Tutorial: Building a GraphQL API in PHP
GraphQL the holy contract between client and server
How easy (or hard) it is to monitor your graph ql service performance
GraphQL & DGraph with Go
An intro to GraphQL
Training Series: Build APIs with Neo4j GraphQL Library
GraphQL + relay
React inter3
GraphQL & Ratpack
Optimization and fault tolerance in distributed transaction with Node.JS Grap...
How to provide a GraphQL API - I want it that way
Grails 101
Graphql Overview By Chirag Dodia
Building Fullstack Serverless GraphQL APIs In The Cloud
Training Week: GraphQL 2022
GraphQL API Gateway and microservices
Simplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) Extension
Implementing OpenAPI and GraphQL services with gRPC
Getting Started with Spring for GraphQL
What could go wrong with a GraphQL query and can OpenTelemetry help? KubeCon...
Ad

More from Soham Dasgupta (6)

PPTX
Are you testing your unit tests?
PPTX
Spring Native : Why not YET!
PPTX
OneBot: A Comprehensive Case Study on Enterprise Digital Assistants
PPTX
No-Java Enterprise Applications: It’s All About JavaScript [DEV5107]
PDF
Javascript for Enterprise Application
PPTX
How the Dutch Police became “Chatbot” interactive
Are you testing your unit tests?
Spring Native : Why not YET!
OneBot: A Comprehensive Case Study on Enterprise Digital Assistants
No-Java Enterprise Applications: It’s All About JavaScript [DEV5107]
Javascript for Enterprise Application
How the Dutch Police became “Chatbot” interactive
Ad

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Spectroscopy.pptx food analysis technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectral efficient network and resource selection model in 5G networks
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Building Integrated photovoltaic BIPV_UPV.pdf
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25 Week I
Spectroscopy.pptx food analysis technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf

GraphQL-ify your API - JFall 2022