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

rest in peace REST

By Abhishek Mishra