Developing GraphQL APIs in Django using Graphene By Nisarg Shah - - PowerPoint PPT Presentation

developing graphql apis in django using graphene
SMART_READER_LITE
LIVE PREVIEW

Developing GraphQL APIs in Django using Graphene By Nisarg Shah - - PowerPoint PPT Presentation

Developing GraphQL APIs in Django using Graphene By Nisarg Shah Nisarg Shah Crazy Developer HELLO! Undergrad CS Student Software Developer at Tweetozy Co-creator of CoursesAround CONNECT WITH ME! iamnisarg.in nisarg1499 nisargshah14


slide-1
SLIDE 1

By Nisarg Shah

Developing GraphQL APIs in Django using Graphene

slide-2
SLIDE 2

CONNECT WITH ME! iamnisarg.in nisarg1499 nisargshah14 HELLO!

Undergrad CS Student Software Developer at Tweetozy Co-creator of CoursesAround

Nisarg Shah

Crazy Developer

nisshah1499@gmail.com

slide-3
SLIDE 3

Today's Talk

MAIN POINTS

General View on API REST APIs and GraphQL APIs Understand GraphQL Implementation using Graphene

slide-4
SLIDE 4

SOME POPULAR API PROTOCOLS

  • SOAP
  • REST
  • and many more.....

Building Web Applications

  • Most web applications use APIs in their backend and

build their interface upon that.

  • Complete business logic in one place
slide-5
SLIDE 5
  • Endpoints

GET https /{id}/getProfile

PUT https /{id}/talkTitle POST https /{id}/newProfile DELETE https /{profileId}

RESTful APIs

  • A piece of code is executed when these APIs are called.
  • Server returns the response to client
slide-6
SLIDE 6

Problems faced in RESTful APIs

  • Multiple Endpoints
  • Over Fetching
  • Under Fetching
slide-7
SLIDE 7

What can be the alternative?

GraphQL

slide-8
SLIDE 8

GraphQL

  • Open Source
  • GraphQL is a Query

Language

  • Uses Schema based

system

  • Easy and efficient to use
slide-9
SLIDE 9

Why GraphQL?

  • Client requests the

needed data. Client decides the query and according to that data is fetched.

slide-10
SLIDE 10

And you know what...

  • Only one API Endpoint
  • No over fetching or

under fetching

  • Auto-generation of

API documentation

slide-11
SLIDE 11

Let's learn about GraphQL

  • Schema : structure
  • Mutation : updating data on server
  • Queries : fetching data
  • Subscriptions : real time data exchange
slide-12
SLIDE 12

Schema

  • GraphQL Object Type

⚬ Product

  • Fields

⚬ productId ⚬ productName ⚬ and few listed in pic

  • Scalar Types

⚬ Int ⚬ String ⚬ and many more...

Product Schema

slide-13
SLIDE 13
  • Used for changing data on

server

  • Return the response

according to your needs

  • Variables passed can be

scalars or ObjectTypes

Mutation

Response from server

slide-14
SLIDE 14

Using query variables for inserting data

slide-15
SLIDE 15
  • Get data from server
  • Ask for specific fields on
  • bjects
  • Design query according to

needs

Query

Response from server

slide-16
SLIDE 16
  • Realtime connection to

server

  • Client subscribes to an event
  • Server pushes data to client

when event occurs

  • Same syntax as queries and

mutations

Subscriptions

Response from server

subscription{ addProduct{ productName, productPrice, productDesc } } { "addProduct" : { "productName" : " Toy", "productPrice" : 500, "productDesc" : "New toy" } }

slide-17
SLIDE 17

Libraries for building GraphQL APIs in Python

STRAWBERRY

456 Stars

Code-First Approach

ARIADNE

896 Stars

Schema-First Approach

GRAPHENE

5.6k+ Stars

Code-First Approach

slide-18
SLIDE 18

Let's Build GraphQL APIs

We will use

slide-19
SLIDE 19

Environment setup

⚬ ⚬ ⚬ ⚬

slide-20
SLIDE 20
slide-21
SLIDE 21

Add the following in your settings.py file

GRAPHENE = { 'SCHEMA' : 'project.schema.schema' }

slide-22
SLIDE 22

Few Concepts of Graphene

slide-23
SLIDE 23

ObjectType

  • A block which is used to

define a relation between fields and schema

slide-24
SLIDE 24

Schema

  • Relationship between

fields in API

slide-25
SLIDE 25
  • Resolvers
  • A method that helps

to answer queries

slide-26
SLIDE 26

Resolvers

  • query($author: String!, $repoName: String!){

repo(login: $author){ repository(name: $repoName){ forkCount, updatedAt } } }

slide-27
SLIDE 27

Scalars

  • a.k.a : Data types
slide-28
SLIDE 28

AUTHENTICATION

Secure your backend API's using JWT Tokens

PAGINATION

Send a particular bunch of data instead

  • f complete data

MUTATIONS

Update the data on server

QUERIES

Fetch the data from server

slide-29
SLIDE 29

File Structure

slide-30
SLIDE 30

Used concepts : ObjectType, DjangoObjectType, resolvers, Schema

FETCHING DATA

Writing Queries

slide-31
SLIDE 31

Used concepts : ObjectType, DjangoObjectType, arguments, mutate

UPDATING DATA ON SERVER

Writing Mutations

slide-32
SLIDE 32

path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True)))

GraphiQL View

Add this in your urls.py file

Write your graphql query/mutation here.... Server response

slide-33
SLIDE 33

Used concepts : ObjectType, DjangoObjectType, Python slicing

SENDING A BUNCH OF DATA

Pagination

slide-34
SLIDE 34

SECURING USING JWT TOKENS

Authentication

.project/schema.py settings.py

slide-35
SLIDE 35

SECURING USING JWT TOKENS

  • Created a mutation for creating a user
  • Create a user
  • Use this mutation while login
  • Store this token and use for further

queries.

slide-36
SLIDE 36

SECURING USING JWT TOKENS

user = info.context.user if user.is_anonymous: raise Exception("Not logged in!!")

Add this is your queries and mutations Add JWT Token in Headers prefixed by "JWT"

slide-37
SLIDE 37

SECURING USING JWT TOKENS

Mutation without JWT Token

slide-38
SLIDE 38
  • GraphQL Website : https://graphql.org/
  • GraphQL Blogs : Medium
  • HowToGraphQL : https://www.howtographql.com/
  • Graphene Documentation

Helpful Resources

slide-39
SLIDE 39

Open Source Project

Github Repository : https://github.com/nisarg1499/django-ecom merce-graphql Building boiler plate of ecommerce by implementing GraphQL APIs in django Currently 3 active contributors

django-ecommerce

  • graphql
slide-40
SLIDE 40

THANK YOU

FINAL WORDS

  • NISARG SHAH

CONNECT WITH ME! iamnisarg.in nisarg1499 nisargshah14 nisshah1499@gmail.com