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
750
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
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
Ethereum_.pdf
nekomatu
0
460
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
受け取る人から提供する人になるということ
little_rubyist
0
230
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
520
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Jakarta EE meets AI
ivargrimstad
0
520
Click-free releases & the making of a CLI app
oheyadam
2
110
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Better Code Design in PHP
afilina
PRO
0
120
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.9k
GraphQLとの向き合い方2022年版
quramy
43
13k
GitHub's CSS Performance
jonrohan
1030
460k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Designing for humans not robots
tammielis
250
25k
Designing the Hi-DPI Web
ddemaree
280
34k
Optimizing for Happiness
mojombo
376
70k
Producing Creativity
orderedlist
PRO
341
39k
Agile that works and the tools we love
rasmusluckow
327
21k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Building an army of robots
kneath
302
43k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
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!