rest in peace {REST} โฐ๏ธ
The rise of GraphQL ๐
{
"Agenda ๐ " : [
}
"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"
What this talk is, and is not !
โ Trip around bird's eye view on REST & GraphQL
โ Python Graphene
๐ โโ๏ธ Graphene internals
๐ โโ๏ธ GraphQL tutorial
Let's start with APIs

{REST} or "so-called REST"


๐ {REST}
๐ท Focuses on resources through URI's
๐ท Got Methods / Verbs :
- GET
- POST
- PUT
- DELETE
๐ท 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

REST APIs
return
resources


Multiple endpoints
>
>
>


Hitting millions* of endpoints......
"RESTful APIs are optimized for servers."
ยฏ\_(ใ)_/ยฏ

BFF


backends
frontends

for


Netflix's BFF Pattern
>
>
>


REST provides benefits.
...But
comes with
๐ฐ

{REST} ๐
๐ Multiple endpoints (multiple round trips)
๐ Weak typing
๐ Query complexity (n+1)
๐ Over/Under-fetching of data
๐ Tooling & introspection
What's the solution ?
So, there are many !!
๐คนโโ๏ธ





โจ
Why GraphQL !?
What makes it different?
๐ค
๐ง

Buzz...
"despite the snark,the buzz is important"
RESTaurant




https://RESTaurant.com/sandwich


GraphQL Cafe




Query{
Sandwich{
bread
salami
tomato
}
}


โ

โ๏ธ
โ๏ธ
โ๏ธ
โ๏ธ
โ๏ธ
Single request, Single endpoint
Specification...NOT implementation
More control over data
Relational queries
Strongly typed !
๐ฏ
๐
๐บ
๐ฐ
โ
LIFE EASIER
in short

enter
GraphQL

๐ {GraphQL}
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

Front-End Folks
think SQL
not Neo4j
๐ ๏ธ Building blocks of GraphQL ๐๏ธโ๏ธ
-
Schema (types & relation)
-
Query (read)
-
Mutation (write)
-
create
-
update
-
delete
-
-
Subscription (realtime pub-sub)


Query
Response
Full Schema
Schema ! Schema ! Schema !
- Adds capability for your API to define how client fetch & update data
- Represents contract between client and server
- Collection of GraphQL types with root types
Root Types
type Query {
...
}
type Mutation {
...
}
type Subscription {
...
}
Libraries for GraphQL

Ariadne


Strawberry

Graphene

Graphene
- Provides tool to implement a GraphQL API in python using a code-first approach
- Instead of writing GraphQL SDL, we write Python code to describe the data
- Integration with most web frameworks and ORM's
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!

๐จโ๐ณ ๐ณ
Use cases of GraphQL
- GraphQL with connected DB
- GraphQL with integrated / 3rd party API (REST)
- The Hybrid approach
Conclusion :
- GraphQL is query language for API whereas REST is pattern
- GraphQL is for complex problems - heavy data exchange
- GraphQL is new REST, it's not *Really* end of REST
Depends on your use case and approach

Resources
*All about API's* -
