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* -