Learn about GraphQL, how it works, and how to use it.
Introduction to GraphQL
View Slide
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
/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
https://code.facebook.com/projects/
Created July 1, 2015Updated September 20, 2017381 Forks6,078 Stars302 Commits82 Open Issueshttps://github.com/facebook/graphql
“You will receive what you ask for no more, no less.”- Mark Allen
GraphQL vs. REST
REST Model
Client/books /characters /housesDatabases Services External APIs
GraphQL Model
ClientDatabases Services External APIs/graphql/
Anatomy of a GraphQL
{me {name}}
{me {name}}{"me": {"name": "Jon Snow"}}
Scalar types
IntFloatStringBooleanIDDateenum
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 datafrom the server
query CharactersNameAndHouse {allCharacters {namehouse {words}}}
query CharactersNameAndHouse {allCharacters {namehouse {words}}}Operation Type
query CharactersNameAndHouse {allCharacters {namehouse {words}}}Operation Name
query CharactersNameAndHouse {allCharacters {namehouse {words}}}Selection Set
query CharactersNameAndHouse {allCharacters {namehouse {words}}}Fields
query CharactersNameAndHouse {allCharacters {namehouse {words}}}{"data": {"allCharacters": [{"name": "Daenerys Targaryen","house": {"words": "Fire and Blood"}},{"name": "Jon Snow","house": {"words": "Winter Is Coming"}}]}}
query {character(name: "Hodor") {namehouse {words}}}Arguments
query CharacterByName($name: String!) {character(name: $name) {name}}{"name": "Hodor"}Variables
query {character(name: "Hodor") {...CharacterFragment}}fragment CharacterFragment on Character {namehouse {words}}Fragments
{character(name: "Hodor") {... on Character {namehouse {words}}}}Inline Fragments
Mutations for manipulating dataand 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) {namewordsmembers {name}}}
name words membersgetHouse(id: 5){ name, words, members }[ { name }, { name } ]name name
QUERYname words membersgetHouse(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") {namekindfields {nametype {name}}}}
GraphiQL
GrapheneGraphQL framework for Python
Created September 24, 2015Updated September 20, 2017229 Forks2,356 Stars1,219 Commits87 Open Issueshttps://github.com/graphql-python/graphene
"It’s important to understand that it isn’t all or nothing.GraphQL is in our future, but it isn’t our exclusivefuture."
Thank you!