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
760
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
77
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
910
Málaga Python Meetup
amatellanes
0
140
Other Decks in Programming
See All in Programming
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
layerx_20241129.pdf
kyoheig3
2
290
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
fs2-io を試してたらバグを見つけて直した話
chencmd
0
220
良いユニットテストを書こう
mototakatsu
4
1.7k
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Refactor your code - refactor yourself
xosofox
1
260
testcontainers のススメ
sgash708
1
120
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
53
13k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
How to Ace a Technical Interview
jacobian
276
23k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
The Invisible Side of Design
smashingmag
298
50k
We Have a Design System, Now What?
morganepeng
51
7.3k
Building Your Own Lightsaber
phodgson
103
6.1k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Writing Fast Ruby
sferik
628
61k
Become a Pro
speakerdeck
PRO
26
5k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
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!