This document provides an introduction and overview of GraphQL, including examples of how GraphQL compares to REST for fetching data from an API. Key points include:
- GraphQL is a new API query language created by Facebook that allows clients to fetch and update data in a declarative way.
- GraphQL allows clients to fetch multiple related data types in one request, whereas REST typically requires multiple endpoints and requests.
- The document demonstrates building a blogging app example using REST versus GraphQL to fetch user, post, and follower data.
- Other GraphQL concepts covered include mutations for writing data, query variables, subscriptions for real-time updates, and exploring GraphQL with a playground.
-
5. What’s GraphQL?
• new API standard by Facebook
• query language for APIs
• declarative way of fetching & updating data
@nikolasburk
6. Mary
Mary’s posts:
Learn GraphQL Today
Why GraphQL is better than
REST
React & GraphQL - A declarative
love story
Relay vs Apollo - GraphQL
Last three followers:
John, Alice, Sarah
Example: Blogging App
7. Example: Blogging App with REST
/users/<id>
/users/<id>/posts
/users/<id>/followers
3 API endpoints
8. 1 Fetch user data
/users/<id>/users/<id>
/users/<id>/posts
/users/<id>/followers
{
“user”: {
“id”: “er3tg439frjw”
“name”: “Mary”,
“address”: { … },
“birthday”: “July 26, 1982”
}
}
HTTP GET
Mary
Mary’s posts:
Last three followers:
9. 2
/users/<id>
/users/<id>/posts
/users/<id>/followers
Fetch posts
HTTP GET
{
“posts”: [{
“id”: “ncwon3ce89hs”
“title”: “Learn GraphQL today”,
“content”: “Lorem ipsum … ”,
“comments”: [ … ],
}, {
“id”: “dsifr3as0vds”
“title”: “React & GraphQL - A declarative love story”,
“content”: “Lorem ipsum … ”,
“comments”: [ … ],
}, {
“id”: “die5odnvls1o”
“title”: “Why GraphQL is better than REST”,
“content”: “Lorem ipsum … ”,
“comments”: [ … ],
}, {
“id”: “dovmdr3nvl8f”
“title”: “Relay vs Apollo - GraphQL clients”,
“content”: “Lorem ipsum … ”,
“comments”: [ … ],
}]
}
Mary
Mary’s posts:
Learn GraphQL Today
Why GraphQL is better than REST
React & GraphQL - A declarative
love story
Relay vs Apollo - GraphQL clients
Last three followers:
10. /users/<id>
/users/<id>/posts
/users/<id>/followers
HTTP GET
{
“followers”: [{
“id”: “leo83h2dojsu”
“name”: “John”,
“address”: { … },
“birthday”: “January 6, 1970”
},{
“id”: “die5odnvls1o”
“name”: “Alice”,
“address”: { … },
“birthday”: “May 1, 1989”
}{
“id”: “xsifr3as0vds”
“name”: “Sarah”,
“address”: { … },
“birthday”: “November 20, 1986”
}
…
]
}
Mary
Mary’s posts:
Learn GraphQL Today
Why GraphQL is better than REST
React & GraphQL - A declarative
love story
Relay vs Apollo - GraphQL clients
Last three followers:
John, Alice, Sarah
Fetch followers3
12. Mary’s posts:
Last three followers:
Fetch everything with a single request1
HTTP POST
query {
User(id: “er3tg439frjw”) {
name
posts {
title
}
followers(last: 3) {
name
}
}
}
13. Mary’s posts:
Last three followers:
Mary
Learn GraphQL Today
Why GraphQL is better than REST
React & GraphQL - A declarative
love story
Relay vs Apollo - GraphQL clients
John, Alice, Sarah
Fetch everything with a single request1
HTTP POST
{
“data”: {
“User”: {
“name”: “Mary”,
“posts”: [
{ title: “Learn GraphQL today” },
{ title: “React & GraphQL - A declarative love story” }
{ title: “Why GraphQL is better than REST” }
{ title: “Relay vs Apollo - GraphQL Clients” }
],
“followers”: [
{ name: “John” },
{ name: “Alice” },
{ name: “Sarah” },
]
}
}
}
24. It’s your turn!
Create your GraphQL server
$ npm install -g graphcool
$ graphcool init --schema https://graphqlbin.com/chat.graphql
Open a GraphQL Playground
$ graphcool playground
25. It’s your turn!
1. Create one new Person object
2. Create one Message for that Person
3. Send a query that filters for messages that
contains a certain string
31. It’s your turn! again
1. Create one new Person object
2. Create one Message for that Person
3. Send a query that filters for messages that
contains a certain string
with Query Variables
32. GraphQL Subscriptions ⚡
• event-based realtime updates
• clients subscribe to specific events
• usually implemented with websockets
@nikolasburk
35. It’s your turn! one more time
1. Create a subscription to listen for Person objects being
created, updated or deleted - then in a second tab send one
of these three mutations and observe the subscription tab
2. Create a subscription to listen for Message objects being
created - then in a second tab create a new Message and
observe the subscription tab
38. GraphQL Clients
• provide nice API for sending queries &
subscriptions
• take caring of managing the cache
• more handy features like optimistic UI, lower-level
netw