Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to GraphQL
Search
Adrián Matellanes
September 23, 2017
Programming
4
770
Introduction to GraphQL
Learn about GraphQL, how it works, and how to use it.
Adrián Matellanes
September 23, 2017
Tweet
Share
More Decks by Adrián Matellanes
See All by Adrián Matellanes
Test-Driven Development
amatellanes
2
110
Practical Monitoring
amatellanes
0
78
Making code compatible with Python 2 and 3
amatellanes
0
1.2k
Python Gotchas
amatellanes
3
1.5k
Cómo construir un API del que tus padres se sientan orgullosos
amatellanes
4
1.2k
Unicode: WTF?
amatellanes
1
920
Málaga Python Meetup
amatellanes
0
140
Other Decks in Programming
See All in Programming
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
400
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
200
Rubyでつくるパケットキャプチャツール
ydah
0
160
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
270
KubeCon NA 2024の全DB関連セッションを紹介
nnaka2992
0
120
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
快速入門可觀測性
blueswen
0
490
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
6
1.4k
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
7.7k
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
1.8k
Flatt Security XSS Challenge 解答・解説
flatt_security
0
710
Featured
See All Featured
Designing Experiences People Love
moore
139
23k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Become a Pro
speakerdeck
PRO
26
5.1k
4 Signs Your Business is Dying
shpigford
182
22k
A Tale of Four Properties
chriscoyier
157
23k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
A designer walks into a library…
pauljervisheath
205
24k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
The Invisible Side of Design
smashingmag
299
50k
How to train your dragon (web standard)
notwaldorf
89
5.8k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Navigating Team Friction
lara
183
15k
Transcript
Introduction to GraphQL
GET https://2017.es.pycon.org/speakers/me { "name": "Adrián Matellanes", "roles": [ "Lead API
Developer", "Málaga Python Organizer" ], "worksAt": "Ebury", "twitter": "@_amatellanes", "github": "github.com/amatellanes" }
What’s GraphQL?
A programming language
A special type of database
A library
A Query Language for APIs
Why GraphQL?
Due to the nature of REST
REST Problems
GET /characters/1 { "name": "Jon Snow" }
"url": "http://anapioficeandfire.com/api/characters/583", "name": "Jon Snow", "culture": "Northmen", "born": "In 283
AC", "titles": [ "Lord Commander of the Night's Watch" ], "aliases": [ "Lord Snow", "Ned Stark's Bastard", "The Snow of Winterfell", "The Bastard of Winterfell", "Lord Crow" ], "allegiances": [ "http://anapioficeandfire.com/api/houses/362" ], "books": [ "http://anapioficeandfire.com/api/books/5" ], "povBooks": [ "http://anapioficeandfire.com/api/books/1", "http://anapioficeandfire.com/api/books/2", "http://anapioficeandfire.com/api/books/3", "http://anapioficeandfire.com/api/books/8"
Round Trip and Repeat Trip Times
None
/episodes/1
/episodes/1 /characters/345 /characters/563 /characters/802 /characters/441 /characters/674
/episodes/1 /characters/345 /characters/563 /characters/802 /characters/441 /characters/674 /reviews/3512
Over/Under Fetching
/episodes/1?fields=characters(name,playedBy)
/episode_with_all_stuff /episode_with_amazing_pictures /episode_with_stuff_i_need_today /episode_with_stuff_for_mobile_app /episode_with_actors_and_reviews_and_images /episode_with_stuff_for_mobile_app_v2 /episode_with_actors_and_reviews_but_no_images
/episode_with_all_stuff /episode_with_amazing_pictures /episode_with_stuff_i_need_today /episode_with_stuff_for_mobile_app /episode_with_stuff_for_mobile_app_v2 /episode_with_actors_and_reviews_and_images /episode_with_actors_and_reviews_but_no_images
None
https://code.facebook.com/projects/
Created July 1, 2015 Updated September 20, 2017 381 Forks
6,078 Stars 302 Commits 82 Open Issues https://github.com/facebook/graphql
None
“You will receive what you ask for no more, no
less.” - Mark Allen
None
None
None
None
None
None
None
None
None
None
None
GraphQL vs. REST
REST Model
Client /books /characters /houses Databases Services External APIs
GraphQL Model
Client Databases Services External APIs /graphql/
Anatomy of a GraphQL
{ me { name } }
{ me { name } } { "me": { "name":
"Jon Snow" } }
Scalar types
Int Float String Boolean ID Date enum
GraphQL Schema Definition Language
type Query { allCharacters: [Character]! character(id: ID!): Character }
type Query { allCharacters: [Character]! character(id: ID!): Character } Root
Type
type Query { allCharacters: [Character]! character(id: ID!): Character } Root
Fields
type Query { allCharacters: [Character]! character(id: ID!): Character } Arguments
type Query { allCharacters: [Character]! character(id: ID!): Character } Return
Types
type Character { name: String! house: House! } type House
{ name: String! words: String! members: [Character]! }
type Character { name: String! house: House! } type House
{ name: String! words: String! members: [Character]! } Object Type
type Character { name: String! house: House! } type House
{ name: String! words: String! members: [Character]! } Fields
type Character { name: String! house: House! } type House
{ name: String! words: String! members: [Character]! } Return Types
Queries for fetching data from the server
query CharactersNameAndHouse { allCharacters { name house { words }
} }
query CharactersNameAndHouse { allCharacters { name house { words }
} } Operation Type
query CharactersNameAndHouse { allCharacters { name house { words }
} } Operation Name
query CharactersNameAndHouse { allCharacters { name house { words }
} } Selection Set
query CharactersNameAndHouse { allCharacters { name house { words }
} } Fields
query CharactersNameAndHouse { allCharacters { name house { words }
} } { "data": { "allCharacters": [ { "name": "Daenerys Targaryen", "house": { "words": "Fire and Blood" } }, { "name": "Jon Snow", "house": { "words": "Winter Is Coming" } } ] } }
query { character(name: "Hodor") { name house { words }
} } Arguments
query CharacterByName($name: String!) { character(name: $name) { name } }
{ "name": "Hodor" } Variables
query { character(name: "Hodor") { ...CharacterFragment } } fragment CharacterFragment
on Character { name house { words } } Fragments
{ character(name: "Hodor") { ... on Character { name house
{ words } } } } Inline Fragments
Mutations for manipulating data and fetch the updated result
type Mutation { createCharacter(name: String!): Character }
mutation CharacterMutation { createCharacter(name: "Sansa Stark") { name } }
{ "data": { "createCharacter": { "name": "Sansa Stark" } } }
Subscriptions for real-time updates
type Subscription { newCharacter: Character! }
subscription NewCharacterSubscription { newCharacter { name } } { "data":
{ "newCharacter": { "name": "Tyrion Lannister" } } }
Query Execution
{ getHouse(id: 5) { name words members { name }
} }
name words members getHouse(id: 5) { name, words, members }
[ { name }, { name } ] name name
QUERY name words members getHouse(id: 5) { name, words, members
} [ { name }, { name } ] name name
QUERY getHouse(id: 5) { name, words, members } HOUSE name
words members [ { name }, { name } ] name name
QUERY getHouse(id: 5) { name, words, members } HOUSE name
words members [ { name }, { name } ] CHARACTER name name
Clients with Super Powers
Introspection Queries
{ "data": { "__type": { "name": "Book", "kind": "OBJECT", "fields":
[ { "name": "title", "type": { "name": "String" } } ] } } } { __type(name: "Book") { name kind fields { name type { name } } } }
None
GraphiQL
None
Graphene GraphQL framework for Python
Created September 24, 2015 Updated September 20, 2017 229 Forks
2,356 Stars 1,219 Commits 87 Open Issues https://github.com/graphql-python/graphene
None
None
"It’s important to understand that it isn’t all or nothing.
GraphQL is in our future, but it isn’t our exclusive future."
Thank you!