SlideShare a Scribd company logo
Adding GraphQL to
your Existing Architecture
Sashko Stubailo
Open Source Lead, Apollo
@stubailo
• Client-side developer ergonomics
• Flexibility means fewer API changes
• Better efficiency, fetch just what you need
GraphQL makes everyone’s life better
So what might block people from using it?
• Where do I start?
• Can I use this at work without rewriting my
existing stuff?
• How do I get running in production?
First questions people ask
• Sometimes presented as totally new way of thinking,
which makes it seem like a big investment
• In practice, it’s a standardization of things people were
already doing
Is GraphQL really that different?
SIDE NOTE
• It’s what made React so easy to adopt
• Can you use it for just one feature or component?
• GraphQL can be done the same way
The golden ticket: Incremental adoption
Web App
Elastic Search MongoDB Dynamo DB
C L I E N T
S E R V E R
API Server
Existing app
Web App
Elastic Search MongoDB Dynamo DB
C L I E N T
S E R V E R
API Server
Existing app + GraphQL
?
?
• Fetch GraphQL anywhere you can do an HTTP request
• For fancier features—caching, network state
management, mutation handling—we’ve worked hard to
make Apollo Client compatible everywhere
• For some projects, great opportunity to improve your UI
and data management together
On the client, incremental is easy
• Model virtually any kind of application data
• Start with client-side needs, fill in API bit by bit
• Uniquely suited to being an API gateway, calling
your existing backends
• Suggestion: Experiment with clients that you can
push code to easily, like web or React Native
Incrementally adoptable API?
Two ways to present GraphQL
• Brownfield, existing app with existing
backend, every situation is different
• GraphQL addresses critical problems
• Greenfield, new app with new
backend, easy to write tutorials
• GraphQL sets you up to grow
• Most content builds “hello world” style APIs from
scratch, or starts with existing GraphQL API
• After you get a taste of the GraphQL experience, you
need a way to get it running in your existing app
• Everyone’s situation is unique
• Can we cut the problem into small bits?
Problem: Not enough guidance
Share live examples of GraphQL server patterns
Launchpad
• Mocking: launchpad.graphql.com/98lq7vz8r
• Neo4j: launchpad.graphql.com/3wzp7qnjv
• MongoDB: launchpad.graphql.com/vkmr1kl83
• Share your own patterns—DataLoader, pagination, etc
—at apollographql/awesome-launchpad
Existing samples on Launchpad
GROW
GRAPHQL COMMUNITY
Getting ready for
production
THE NEXT STEP
1. Understand what is going on inside your server
with instrumentation
2. Know the tools available to make improvements
3. Ensure that backend services are not adversely
affected by the new API layer
Production learning checklist
• You need your instrumentation to understand GraphQL
• Need to have some way to identify common queries,
expensive fields, critical paths, and schema usage
• Write your client code and queries in a way that lets you
identify what you see on the server
First: Instrumentation
Two classes of problems
• High latency or bandwidth usage causes bad experience
for the end-user client app
• Inefficient execution or expensive requests result in too
much load on the API or backend
Client-side improvements
• Load data judiciously to avoid
performance issues
• Load less data at a time, split into
multiple queries, avoid expensive fields
• Improve client-side caching with an
offline cache, cache redirects, and
optimistic UI
• Otherwise, need to improve server
query Human {
human {
name
weather
friends {
name
}
}
}
query Human
Biggest question: Server performance
• Executing each query with low latency needs to be
balanced with load on the API server and backends
• Two dimensions that can be optimized: GraphQL server
overhead, and work done by backend APIs and databases
GraphQL server overhead?
• GraphQL execution engine itself is quite efficient for the value it
provides, usually not the main thing to optimize
• Caching parsing and validation helps a lot, those are some of the
most expensive steps
• Make it stateless to scale horizontally
• Break out of GraphQL data model if necessary with custom scalars
• In general, overhead is easy to limit
Backend API usage
• GraphQL API is much closer to the backend, so
same number of roundtrips is less time
• We should try to make sure we do the same
number of fetches or less than we were doing
without GraphQL
• How can we reduce fetches to the backend or
make them more efficient?
REST
Reducing backend load
• Leverage backend features when you can
• Use something like DataLoader to collect requirements,
then make one batched request and avoid extra roundtrips
• Make sure GraphQL server manages backend load by
listening to backpressure or rate limiting calls
• In theory GraphQL can be very efficient since all data needs
are known ahead of time
Persisted queries?
• Save queries to a database and only send the ID instead
of the whole query string
• Supported by Apollo Client and Relay Modern
• Important to distinguish several possible concepts:
• Static queries - no runtime modification of queries
• Whitelisted queries - only accept known queries
• Persisted queries - send query ID, look it up on server
• Compiled queries - do some optimization in build step
• Different potential benefits from each one
{
id: ‘23235’,
variables: …
}
Preventing malicious queries
• Simple: Basic rate limiting
• Medium: Statically calculate cost from the query
document before execution, reject if too high
• Advanced: Track cost of each client over time, to block
people loading too much data quickly
• For extreme cases: Explicit whitelisting
Advanced performance ideas
• Get the same performance as a hand-crafted REST endpoint
but with no loss of generality
• For certain applications that have extremely common
queries, hand-optimize queries or fragments
• In the resolver, check if we are looking for that selection set
and run a more efficient batched fetch for the subtree
• HTTP caching of whole query results
• You can incrementally adopt GraphQL, start with a quick
prototype to get a feel for it
• Instrument everything to avoid unnecessary optimizations
• Work with backend devs to make sure the databases and
services are happy
• The normal thing is usually fine, but if you discover issues
there are many paths to optimize your GraphQL API
Conclusions
Help grow the community!
• Write more content about intermediate GraphQL topics,
to help people get this amazing API with their data
• Write about your production experiences, performance
optimizations you’ve made, and new patterns you come
up with
Tweet or DM me, let’s work together: @stubailo

More Related Content

PDF
GraphQL across the stack: How everything fits together
PDF
Modular GraphQL with Schema Stitching
PDF
Why UI Developers Love GraphQL - Sashko Stubailo, Apollo/Meteor
PDF
The Apollo and GraphQL Stack
PDF
GraphQL: The Missing Link Between Frontend and Backend Devs
PDF
Meteor MIT Tech Talk 9/18/14: Designing a New Platform For Modern Apps
PDF
GraphQL over REST at Reactathon 2018
PDF
GraphQL: Enabling a new generation of API developer tools
GraphQL across the stack: How everything fits together
Modular GraphQL with Schema Stitching
Why UI Developers Love GraphQL - Sashko Stubailo, Apollo/Meteor
The Apollo and GraphQL Stack
GraphQL: The Missing Link Between Frontend and Backend Devs
Meteor MIT Tech Talk 9/18/14: Designing a New Platform For Modern Apps
GraphQL over REST at Reactathon 2018
GraphQL: Enabling a new generation of API developer tools

What's hot (20)

PDF
GraphQL
PDF
GraphQL + relay
PPTX
Into to GraphQL
PPTX
Taking Control of your Data with GraphQL
PPT
Graphql presentation
PPTX
GraphQL Introduction
PPTX
Introduction to GraphQL
PDF
How to GraphQL
PPTX
An intro to GraphQL
PDF
GraphQL & Relay
PDF
PDF
Graphql
PDF
How to GraphQL: React Apollo
PDF
Better APIs with GraphQL
PDF
REST vs GraphQL
PDF
Serverless GraphQL for Product Developers
PDF
Intro to GraphQL
PDF
GraphQL With Relay Part Deux
PPTX
Introduction to graphQL
PDF
Performance optimisation with GraphQL
GraphQL
GraphQL + relay
Into to GraphQL
Taking Control of your Data with GraphQL
Graphql presentation
GraphQL Introduction
Introduction to GraphQL
How to GraphQL
An intro to GraphQL
GraphQL & Relay
Graphql
How to GraphQL: React Apollo
Better APIs with GraphQL
REST vs GraphQL
Serverless GraphQL for Product Developers
Intro to GraphQL
GraphQL With Relay Part Deux
Introduction to graphQL
Performance optimisation with GraphQL
Ad

Similar to Adding GraphQL to your existing architecture (20)

PDF
apidays LIVE Paris - GraphQL meshes by Jens Neuse
PDF
LeanIX GraphQL Lessons Learned - CodeTalks 2017
PDF
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
DOCX
GraphQL Advanced Concepts A Comprehensive Guide.docx
PPTX
GraphQL - an elegant weapon... for more civilized age
PDF
GraphQL Bangkok meetup 5.0
PDF
Introduction to GraphQL for beginners
PDF
GraphQL for Native Apps
DOCX
Graphql for Frontend Developers Simplifying Data Fetching.docx
PDF
Apollo server II
PDF
All you need to know about GraphQL.pdf
PDF
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything together
PDF
Why UI developers love GraphQL
PDF
From rest api to graph ql a 10 year journey
PPTX
Introduction to Graph QL
PPTX
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
PDF
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
PDF
GraphQL the holy contract between client and server
PPTX
React Flux to GraphQL
PDF
GraphQL vs. (the) REST
apidays LIVE Paris - GraphQL meshes by Jens Neuse
LeanIX GraphQL Lessons Learned - CodeTalks 2017
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
GraphQL Advanced Concepts A Comprehensive Guide.docx
GraphQL - an elegant weapon... for more civilized age
GraphQL Bangkok meetup 5.0
Introduction to GraphQL for beginners
GraphQL for Native Apps
Graphql for Frontend Developers Simplifying Data Fetching.docx
Apollo server II
All you need to know about GraphQL.pdf
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything together
Why UI developers love GraphQL
From rest api to graph ql a 10 year journey
Introduction to Graph QL
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
GraphQL the holy contract between client and server
React Flux to GraphQL
GraphQL vs. (the) REST
Ad

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation theory and applications.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Electronic commerce courselecture one. Pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
A comparative analysis of optical character recognition models for extracting...
MYSQL Presentation for SQL database connectivity
Encapsulation theory and applications.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Advanced methodologies resolving dimensionality complications for autism neur...
Electronic commerce courselecture one. Pdf
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Reach Out and Touch Someone: Haptics and Empathic Computing

Adding GraphQL to your existing architecture

  • 1. Adding GraphQL to your Existing Architecture Sashko Stubailo Open Source Lead, Apollo @stubailo
  • 2. • Client-side developer ergonomics • Flexibility means fewer API changes • Better efficiency, fetch just what you need GraphQL makes everyone’s life better So what might block people from using it?
  • 3. • Where do I start? • Can I use this at work without rewriting my existing stuff? • How do I get running in production? First questions people ask
  • 4. • Sometimes presented as totally new way of thinking, which makes it seem like a big investment • In practice, it’s a standardization of things people were already doing Is GraphQL really that different? SIDE NOTE
  • 5. • It’s what made React so easy to adopt • Can you use it for just one feature or component? • GraphQL can be done the same way The golden ticket: Incremental adoption
  • 6. Web App Elastic Search MongoDB Dynamo DB C L I E N T S E R V E R API Server Existing app
  • 7. Web App Elastic Search MongoDB Dynamo DB C L I E N T S E R V E R API Server Existing app + GraphQL ? ?
  • 8. • Fetch GraphQL anywhere you can do an HTTP request • For fancier features—caching, network state management, mutation handling—we’ve worked hard to make Apollo Client compatible everywhere • For some projects, great opportunity to improve your UI and data management together On the client, incremental is easy
  • 9. • Model virtually any kind of application data • Start with client-side needs, fill in API bit by bit • Uniquely suited to being an API gateway, calling your existing backends • Suggestion: Experiment with clients that you can push code to easily, like web or React Native Incrementally adoptable API?
  • 10. Two ways to present GraphQL • Brownfield, existing app with existing backend, every situation is different • GraphQL addresses critical problems • Greenfield, new app with new backend, easy to write tutorials • GraphQL sets you up to grow
  • 11. • Most content builds “hello world” style APIs from scratch, or starts with existing GraphQL API • After you get a taste of the GraphQL experience, you need a way to get it running in your existing app • Everyone’s situation is unique • Can we cut the problem into small bits? Problem: Not enough guidance
  • 12. Share live examples of GraphQL server patterns Launchpad
  • 13. • Mocking: launchpad.graphql.com/98lq7vz8r • Neo4j: launchpad.graphql.com/3wzp7qnjv • MongoDB: launchpad.graphql.com/vkmr1kl83 • Share your own patterns—DataLoader, pagination, etc —at apollographql/awesome-launchpad Existing samples on Launchpad
  • 16. 1. Understand what is going on inside your server with instrumentation 2. Know the tools available to make improvements 3. Ensure that backend services are not adversely affected by the new API layer Production learning checklist
  • 17. • You need your instrumentation to understand GraphQL • Need to have some way to identify common queries, expensive fields, critical paths, and schema usage • Write your client code and queries in a way that lets you identify what you see on the server First: Instrumentation
  • 18. Two classes of problems • High latency or bandwidth usage causes bad experience for the end-user client app • Inefficient execution or expensive requests result in too much load on the API or backend
  • 19. Client-side improvements • Load data judiciously to avoid performance issues • Load less data at a time, split into multiple queries, avoid expensive fields • Improve client-side caching with an offline cache, cache redirects, and optimistic UI • Otherwise, need to improve server query Human { human { name weather friends { name } } } query Human
  • 20. Biggest question: Server performance • Executing each query with low latency needs to be balanced with load on the API server and backends • Two dimensions that can be optimized: GraphQL server overhead, and work done by backend APIs and databases
  • 21. GraphQL server overhead? • GraphQL execution engine itself is quite efficient for the value it provides, usually not the main thing to optimize • Caching parsing and validation helps a lot, those are some of the most expensive steps • Make it stateless to scale horizontally • Break out of GraphQL data model if necessary with custom scalars • In general, overhead is easy to limit
  • 22. Backend API usage • GraphQL API is much closer to the backend, so same number of roundtrips is less time • We should try to make sure we do the same number of fetches or less than we were doing without GraphQL • How can we reduce fetches to the backend or make them more efficient? REST
  • 23. Reducing backend load • Leverage backend features when you can • Use something like DataLoader to collect requirements, then make one batched request and avoid extra roundtrips • Make sure GraphQL server manages backend load by listening to backpressure or rate limiting calls • In theory GraphQL can be very efficient since all data needs are known ahead of time
  • 24. Persisted queries? • Save queries to a database and only send the ID instead of the whole query string • Supported by Apollo Client and Relay Modern • Important to distinguish several possible concepts: • Static queries - no runtime modification of queries • Whitelisted queries - only accept known queries • Persisted queries - send query ID, look it up on server • Compiled queries - do some optimization in build step • Different potential benefits from each one { id: ‘23235’, variables: … }
  • 25. Preventing malicious queries • Simple: Basic rate limiting • Medium: Statically calculate cost from the query document before execution, reject if too high • Advanced: Track cost of each client over time, to block people loading too much data quickly • For extreme cases: Explicit whitelisting
  • 26. Advanced performance ideas • Get the same performance as a hand-crafted REST endpoint but with no loss of generality • For certain applications that have extremely common queries, hand-optimize queries or fragments • In the resolver, check if we are looking for that selection set and run a more efficient batched fetch for the subtree • HTTP caching of whole query results
  • 27. • You can incrementally adopt GraphQL, start with a quick prototype to get a feel for it • Instrument everything to avoid unnecessary optimizations • Work with backend devs to make sure the databases and services are happy • The normal thing is usually fine, but if you discover issues there are many paths to optimize your GraphQL API Conclusions
  • 28. Help grow the community! • Write more content about intermediate GraphQL topics, to help people get this amazing API with their data • Write about your production experiences, performance optimizations you’ve made, and new patterns you come up with Tweet or DM me, let’s work together: @stubailo