The rise of GraphQL 🌈
"About me 👨🔬", "what the talk is, and is not!", "Quick walk-through on REST", "What's wrong with REST ?", "Solution to solve REST issues - GraphQL", "Why GraphQL ?", "About GraphQL & it's building blocks", "How to GraphQL in Python", "E.g.- basic schema and querying using graphene", "Best practices & resources for GraphQL", "Conclusion"
Abhishek
👨💻 Software Eng. at Saama Research Lab
🐍 Pythonista, GDG Chennai & PyCon IN ♥️
👨👩👧👦 Community first person 💛
connect with me:
🚫 not "10x Engineer"
✅ Trip around bird's eye view on REST & GraphQL
✅ Python Graphene
🙅♂️ Graphene internals
🙅♂️ GraphQL tutorial
🔷 Focuses on resources through URI's
🔷 Got Methods / Verbs :
🔷 Gets JSON for you
🔷 Stateless architectural style fo web service development
https://api.saamaconf.com/2021/speakers
GET
[
verb
endpoint
https://api.pyconin.com/2019/speakers
GET
Multiple endpoints
>
>
>
Hitting millions* of endpoints......
backends
frontends
for
Netflix's BFF Pattern
>
>
>
👉 Multiple endpoints (multiple round trips)
👉 Weak typing
👉 Query complexity (n+1)
👉 Over/Under-fetching of data
👉 Tooling & introspection
"despite the snark,the buzz is important"
https://RESTaurant.com/sandwich
Query{
Sandwich{
bread
salami
tomato
}
}
Single request, Single endpoint
Specification...NOT implementation
More control over data
Relational queries
Strongly typed !
DSL on top of your backend data fetching logic
Created by Facebook & went open source in 2015
Query language for your API
Strongly typed-Schema driven
Self described & introspectable
Before GraphQL
After GraphQL
Schema (types & relation)
Query (read)
Mutation (write)
create
update
delete
Subscription (realtime pub-sub)
Query
Response
type Query {
...
}
type Mutation {
...
}
type Subscription {
...
}
pip install "graphene>=2.0"
Query
Response
from graphene import ObjectType, String, Schema
class Query(ObjectType):
# this defines a Field `hello` in our Schema with a single Argument `name`
hello = String(name=String(default_value="stranger"))
goodbye = String()
# our Resolver method takes the GraphQL context (root, info) as well as
# Argument (name) for the Field and returns data for the query Response
def resolve_hello(root, info, name):
return f'Hello {name}!'
def resolve_goodbye(root, info):
return 'See ya!'
schema = Schema(query=Query)
type Query {
hello(name: String = "stranger"): String
goodbye: String
}
Basic Schema 👉
Code 👇
Querying 🔎
# we can query for our field (with the default argument)
query_string = '{ hello }'
result = schema.execute(query_string)
print(result.data['hello'])
# "Hello stranger"
# or passing the argument in the query
query_with_argument = '{ hello(name: "GraphQL") }'
result = schema.execute(query_with_argument)
print(result.data['hello'])
# "Hello GraphQL!"
Hello stranger!
Hello GraphQL!
*All about API's* -