SlideShare une entreprise Scribd logo
1  sur  60
Télécharger pour lire hors ligne
Getting Started
with
Apollo Client
Morgan Dedmon-Wood
$whoami
Software Engineer @ Match
LinkedIn:
morgandedmon.com
Github:
mrgn-w
Twitter:
@morgan_dedmon
GraphQL
Apollo
Set of tools to use with GraphQL
Server Libraries
• Apollo-server: bind graphQL endpoint to Node server
• Apollo-engine: Middleware for Node
• GraphQL-Tools: Build, mock, manipulate, stitch schemas
• Subscriptions-transport-ws: transport for Subscriptions
Apollo
Set of tools to use with GraphQL
Developer Tools
• Apollo Dev Tools
• Launchpad: Demo platform for GraphQL servers
• Apollo-Tracing: Performance Tracing
• Eslint-plugin-graphql
Apollo
Set of tools to use with GraphQL
Client Tools
• Apollo Client
• View integrations (react-apollo, etc)
Apollo Client
Fully featured GraphQL Client
• Incrementally Adoptable
• Universally Compatible
• Simple to get started with
• Inspectable and understandable
• Community Driven
Framework Agnostic
Fetching Data
Fetching GraphQL Data
{
"data": {
"allPeople": {
"people": [
{
"name": "Luke Skywalker"
},
{
"name": "C-3PO"
},
{
"name": "R2-D2"
},
{
"name": "Darth Vader"
},
{
"name": "Leia Organa"
}
]
}
}
}
query {
allPeople {
people {
name
}
}
}
Fetching GraphQL Data
UI
Component
Query
GET
JSON
Fetching GraphQL Data
{
"data": {
"allPeople": {
"people": [
{
"name": "Luke Skywalker"
},
{
"name": "C-3PO"
},
{
"name": "R2-D2"
},
{
"name": "Darth Vader"
},
{
"name": "Leia Organa"
}
]
}
}
}
curl 
-X POST 
-H "Content-Type: application/json" 
--data '{
"query": "{ allPeople { people { name } } }"
}' 
https://ourapiurl.com/graphql
fetch('https://ourapiurl.com/graphql ', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: '{ allPeople { people { name } } }'
}),
})
.then(res => res.json())
Plain Fetch
Apollo Fetch
UI
Component
Query
GET
JSON
Bind Data to UI with single function call
Apollo Fetch
client.query({
query: gql`
query {
allPeople {
people {
name
}
}
}
`
})
Apollo Fetch
• Batching
• Reduced round trips for all data in view
• Loading state and network status tracking
• Error Handling with error policy options
• Refetch
• Polling Interval
• fetchMore (pagination)
Query Batching
• Sending Multiple queries to
server in one request
• Reduce round trips
• Single request means you can
utilize Data Loader for batching
actual data load from backend
DB
const client = new ApolloClient({
// ... other options ...
shouldBatch: true,
});
Data Storage
Caching Graphs
Repo
Demo-app
Name 1
IDStars
30
Usermrgn-w
Name
ID
30
Repo
Collection
Repo
cool-graph
Name 2
IDStars
12
Hierarchy
Repositories
Caching Graphs
Repo
Demo-app
Name 1
IDStars
30
Usermrgn-w
Name
ID
30
Repo
Collection
Repo
cool-graph
Name 2
IDStars
12
Hierarchy
Repositories
Caching Graphs
Repo
Demo-app
Name 1
IDStars
30
Usermrgn-w
Name
ID
30
Repo
Collection
Repo
cool-graph
Name 2
IDStars
12 Repo
Demo-app
Name 1
IDStars
30
Repo
cool-graph
Name 2
IDStars
12
Hierarchy Normalized
Repositories
User
mrgn-w
Name
ID
30
Repo
Collection
User:30
Repo:1 Repo:2
Repo:1 Repo:2
Repo:1 Repo:2
Caching Graphs
Repo
Demo-app
Name 1
IDStars
30
Usermrgn-w
Name
ID
30
Repo
Collection
Repo
cool-graph
Name 2
IDStars
12
Hierarchy Normalized
Repositories
Usermrgn-w
Name
ID
30
Repo
Collection
User:30
Repo:1 Repo:2
Repo
Demo-app
Name 1
IDStars
30
Repo
cool-graph
Name 2
IDStars
12
Syncing UI State
Repo
Demo-app
Name 1
ID
Stars
30
Repo:1
Repo
cool-graph
Name 2
ID
Stars
12
Repo:2User
mrgn-w
Name
ID
30
Repo
Collection
User:30
Repo:1 Repo:2
UIs are automatically subscribed to observe changes to cached records
and will automatically update
watchQuery method
View Integration
{repo: { id: 2, stars: 15 }}
15
Repo:2
Caching Client
• Manage data in one place
• All queries watch store for changes and update UI
accordingly
• Keep Data + UI consistent
• Avoid unnecessary refetches
UI
Component
UI
Component
Utilizing the Cache
Fetch Policies
cache-first
cache-and-network
network-only
cache-only
cache-first
Tries reading from cache first
If all data is there in cache, returns that data
Only fetch from network if result is not in cache
Query
Cache
Network
Return
Data
network-and-cache
Tries reading from cache first
If the data is in the cache, that data is returned
Will always execute query with network interface, regardless
of if full data is in cache
Query
Cache
Network
Return
Data
network-only
Never return data from cache
Always make a request to the server
Query
Network
Return
Data
cache-only
Never execute using network interface
Always fetch from cache
If data does not exist in cache, error is thrown
Allows you to only interact with data in your local client cache
Query
Cache
Return
Data
Direct Cache Access
readQuery
writeQuery
readFragment
writeFragment
Cache Resolvers
Map of custom ways to resolve data from other parts of
cache
Allows us to tell Apollo Client to check the cache for a
particular object if that object may have been resolved
by another query
Query
Minimize
Query
Send
Result
Normalize
Response
Store
(InMemory
Cache)
Compose
Results
Apollo Client
UI
Components Update
UI
Request
Data
Apollo Client
Further Query Optimization
• Batches queries
• UI responds to updates to rendered data automatically
• Reads from cache where appropriate
…But what about what’s left of the query that gets
sent?
GraphQL Execution Cycle
• Parses query into AST
• Validates if query makes sense given the schema
• Executes the query
GraphQL Execution Cycle
• Parses query into AST
• Validates if query makes sense given the schema
• Executes the query
• Aggregate query strings at build time
• Store Query to DB
• Return an ID to reference stored query
• Pass ID instead of query
• Mapping between GraphQL Documents and generated IDs
• Restricts queries accepted by the server by whitelisting
an allowed set
• Saves bandwidth between client and server by only
sending hash/id instead of full query
• Optimizes execution by not having to parse and validate
over and over on the server
Persisted Queries
• persistgraphql - Build tool
• Crawls client code for graphQL documents
• Creates mapping and IDs
• Writes to JSON file
• In Production mode, only the hash is sent
• Automatic persisted queries via Apollo Engine
• Protocol extension in front of GraphQL server
• Sends hash instead of text
• Server checks if there is a match in registry
• If not found, requests full text from client and saves has
to registry for subsequent requests
Persisted Queries
Network Stack
Query
Minimize
Query
Send
Result
Normalize
Response
Store
(InMemory
Cache)
Compose
Results
Apollo Client
UI
Components Update
UI
Request
Data
Apollo Client
• “Components” for your data!
• Isolate parts of control flow
• Composable
• Middleware and Afterware
Apollo Link
Link
Operation Observable
• query
• Variables
• operationName
• Extensions
• getContext
• setContext
• toKey
Link Link Link Server
Operation
Execution Result
• “Components” for your data!
• Isolate parts of control flow
• Composable
• Middleware and Afterware
Apollo Link
• Adding Auth headers
• Retry failed mutations
• Deduping results
• Error logging
• Deferring expensive query execution
• Split execution chain across different protocols
Apollo Link Uses
Query
Minimize
Query
Send
Result
Normalize
Response
Store
(InMemory
Cache)
Compose
Results
Apollo Client
UI
Components Update
UI
Request
Data
Apollo Link State!
Local
Data
Query
Minimize
Query
Send
Result
Normalize
Response
Store
(InMemory
Cache)
Compose
Results
Apollo Client
UI
Components Update
UI
Request
Data
Universal language for data!
Local
Data
REST
Apollo Link Rest! (in development)
Apollo Client
• Fetch Data
• Cache
• Syncs client state + server
• Query manager
• Network Interface
• Framework Agnostic
• And more! (optimistic UI, SSR, etc)
Why Apollo Client?
Do more with less code
Reduce complexity
Modular Architecture
Flexible
Universal - Can manage ALL data
Highly customizable - No one size fits all, can be
crafted to fit your unique needs
Excellent tools
Apollo Dev Tools
Apollo Dev Tools
Apollo Dev Tools
Apollo-Boost
- Beginner friendly, limited-configuration setup
Match Technology Presents…
Kyle Matthews, Creator of Gatsby
March 29, 6:30PM @ Match HQ
meetup.com/Match-technology-presents
We’re hiring!
lifeatmatch.com

Contenu connexe

Tendances

Tendances (20)

Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Intro to GraphQL
 Intro to GraphQL Intro to GraphQL
Intro to GraphQL
 
Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)
 
An intro to GraphQL
An intro to GraphQLAn intro to GraphQL
An intro to GraphQL
 
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
 
Intro GraphQL
Intro GraphQLIntro GraphQL
Intro GraphQL
 
Graphql
GraphqlGraphql
Graphql
 
Wroclaw GraphQL - GraphQL in Java
Wroclaw GraphQL - GraphQL in JavaWroclaw GraphQL - GraphQL in Java
Wroclaw GraphQL - GraphQL in Java
 
GraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer toolsGraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer tools
 
Better APIs with GraphQL
Better APIs with GraphQL Better APIs with GraphQL
Better APIs with GraphQL
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQL
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Building Modern APIs with GraphQL
Building Modern APIs with GraphQLBuilding Modern APIs with GraphQL
Building Modern APIs with GraphQL
 
GraphQL Fundamentals
GraphQL FundamentalsGraphQL Fundamentals
GraphQL Fundamentals
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs REST
 
Introduction to graphQL
Introduction to graphQLIntroduction to graphQL
Introduction to graphQL
 
GraphQL
GraphQLGraphQL
GraphQL
 
GraphQL
GraphQLGraphQL
GraphQL
 
React & GraphQL
React & GraphQLReact & GraphQL
React & GraphQL
 

Similaire à Getting started with Apollo Client and GraphQL

Architectures, Frameworks and Infrastructure
Architectures, Frameworks and InfrastructureArchitectures, Frameworks and Infrastructure
Architectures, Frameworks and Infrastructure
harendra_pathak
 
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
Vyacheslav Lapin
 
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lightbend
 
Nexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and SprayNexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and Spray
Matthew Farwell
 

Similaire à Getting started with Apollo Client and GraphQL (20)

StackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStackStackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStack
 
Graphql usage
Graphql usageGraphql usage
Graphql usage
 
(ATS6-PLAT04) Query service
(ATS6-PLAT04) Query service (ATS6-PLAT04) Query service
(ATS6-PLAT04) Query service
 
Architectures, Frameworks and Infrastructure
Architectures, Frameworks and InfrastructureArchitectures, Frameworks and Infrastructure
Architectures, Frameworks and Infrastructure
 
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
ITSubbotik - как скрестить ежа с ужом или подводные камни внедрения функциона...
 
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
 
Azure cosmosdb
Azure cosmosdbAzure cosmosdb
Azure cosmosdb
 
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
 
How easy (or hard) it is to monitor your graph ql service performance
How easy (or hard) it is to monitor your graph ql service performanceHow easy (or hard) it is to monitor your graph ql service performance
How easy (or hard) it is to monitor your graph ql service performance
 
Iac d.damyanov 4.pptx
Iac d.damyanov 4.pptxIac d.damyanov 4.pptx
Iac d.damyanov 4.pptx
 
Running Airflow Workflows as ETL Processes on Hadoop
Running Airflow Workflows as ETL Processes on HadoopRunning Airflow Workflows as ETL Processes on Hadoop
Running Airflow Workflows as ETL Processes on Hadoop
 
Serverless GraphQL for Product Developers
Serverless GraphQL for Product DevelopersServerless GraphQL for Product Developers
Serverless GraphQL for Product Developers
 
Dsdt meetup 2017 11-21
Dsdt meetup 2017 11-21Dsdt meetup 2017 11-21
Dsdt meetup 2017 11-21
 
DSDT Meetup Nov 2017
DSDT Meetup Nov 2017DSDT Meetup Nov 2017
DSDT Meetup Nov 2017
 
React inter3
React inter3React inter3
React inter3
 
AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)AngularJS 1.x - your first application (problems and solutions)
AngularJS 1.x - your first application (problems and solutions)
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
GraphQL the holy contract between client and server
GraphQL the holy contract between client and serverGraphQL the holy contract between client and server
GraphQL the holy contract between client and server
 
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and TypescriptMongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
 
Nexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and SprayNexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and Spray
 

Dernier

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Dernier (20)

UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 

Getting started with Apollo Client and GraphQL