https://github.com/a-y-khan/graphene_flask_example.git
Build GraphQL APIs with Graphene
Thanks to our Sponsors!
Partners
Premier
Logo:
About Me
@aylakhanaylaykhanayla.khan@gmail.com
● Software Engineer, Data Engineering Team
@ Recursion Pharmaceuticals
● Previously: Data Engineer
@ ARUP Laboratories, University of Utah
Build GraphQL APIs with Graphene
What is GraphQL?
● Open-source data query &
modification language for APIs
○ Declarative data fetching
○ Connected data
● Developed by Facebook,
hosted by GraphQL
Foundation
Hello World in Graphene: Query
Hello World in Graphene: Execute Query
root
field
Hello World in Graphene: Results
What is GraphQL?
● Query language
○ Describes data requirements, actions
■ CRUD operation: query, mutation
● Strong type system
○ Schema definition language (SDL)
○ Static validation
What is GraphQL?
● Subscription
○ “Real-time” connection to server
■ Typically websockets
○ Event-based
■ Trigger on mutation
■ Pushes changed data payload
GraphQL Specification
● GraphQL schema language
■ Scalars, list, union, enum, interface
○ Type introspection
● Execution semantics
○ queries: parallel
○ mutations: serial
Why Use GraphQL?
● Requested data only
○ No over- or underfetching
● Single endpoint
● Combine data from multiple sources
● Describes data relationships
Why Use GraphQL?
● Deprecation
● Pagination, slicing
● Event-based subscriptions
● Strongly typed
● Detailed errors
● In-browser IDE
Why Not Use GraphQL?
● GraphQL, database schema duplication
● Harder schema design
● Increased query complexity
● Harder rate limiting
Why Not Use GraphQL?
● Expensive data operations on server
○ Superfluous database queries
○ Expensive joins in resolver functions
● Caching more complex
● REST is simpler, mature
What is Graphene?
● Python library to build GraphQL API
○ Schema described in Python code
■ Translate to SDL
○ Build GraphQL service with Flask,
Django
○ Execution engine
Graphene Flask App
/graphql
sqlite3 DB
ORM
API Description
+
Query,
Mutation
Graphene: Flask (app.py)
dev
only
SQL ORM Model (models.py)
ORM Model to GraphQL (schema.py)
Schema (schema.py)
Mutation
Actions
Types
GraphiQL
Schema: Query
(schema.py)
root
field
payload
not nullable
field
Schema: Query
(schema.py)
Schema: Query (schema.py)
Query: Connected Data
Query: Connected Data (schema.py, models.py)
REST vs GraphQL
GET /students/{id}/name
JSON
GET /students/{id}/house
JSON
GET /students/{id}/houses/{id}/name
JSON
GET /students/{id}/houses/{id}/crest
JSON
CLIENT
SERVER
CLIENT
SERVER
POST /graphql
JSON
Static Validation
Deprecation
Deprecation (models.py)
Deprecation (schema.py)
Combine Types (schema.py)
Combine Types: Union (schema.py)
Combine Types
(schema.py)
fragment
Graphene with Relay Framework
● Reference, refetch objects by ID
● List sorting, slicing, pagination, cursors
● Simplifies, structures mutations
● Reduces boilerplate!
Schema: Connection (schema.py)
● List of edges
○ Connection between related nodes
○ Slicing, pagination etc.
Schema: Node (schema.py)
● Data type from model
● Globally unique IDs
● Edges are node list
Schema: Connection (schema.py)
Schema: Connection (schema.py)
Schema: Mutation (schema.py)
Schema: (Create) Mutation (schema.py)
Schema: (Create) Mutation (schema.py)
Schema: (Create) Mutation (schema.py)
Schema: (Update) Mutation (schema.py)
Schema: (Update) Mutation (schema.py)
Schema: (Delete) Mutation (schema.py)
Schema: (Delete) Mutation (schema.py)
Type Introspection
Type Introspection
https://github.com/a-y-khan/graphene_flask_example.git
Build GraphQL APIs with Graphene
Resources
● Learn GraphQL: https://bit.ly/2tH0L5M
● How to GraphQL: https://bit.ly/2FVyXjY
● GraphQL Playground: https://bit.ly/2VGzSeQ
● GraphQL Pagination: https://bit.ly/2UiP9Co
● GraphQL Subscriptions: https://bit.ly/2EIY35k
● GraphQL vs REST: https://bit.ly/2IU83OE, https://bit.ly/2lxUkzN
● Why use GraphQL?: https://bit.ly/2XGGdso
● GraphiQL: GraphQL’s Killer App: https://bit.ly/2tUjmfg
● Using GitHub’s (GraphiQL) GraphQL Explorer: https://bit.ly/31S4lcy
● Graphene: https://bit.ly/2H3j8ev
● Relay: https://bit.ly/2Tyll7s
● Graphene Django: https://bit.ly/2EBzxmO, https://bit.ly/2VBoBfP

Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)