developing graphql apis in django using graphene
play

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


  1. Developing GraphQL APIs in Django using Graphene By Nisarg Shah

  2. Nisarg Shah Crazy Developer HELLO! Undergrad CS Student Software Developer at Tweetozy Co-creator of CoursesAround CONNECT WITH ME! iamnisarg.in nisarg1499 nisargshah14 nisshah1499@gmail.com

  3. Today's Talk MAIN POINTS General View on API REST APIs and GraphQL APIs Understand GraphQL Implementation using Graphene

  4. Building Web Applications • Most web applications use APIs in their backend and build their interface upon that. • Complete business logic in one place SOME POPULAR API PROTOCOLS • SOAP • REST • and many more.....

  5. RESTful APIs • Endpoints GET https /{id}/getProfile PUT https /{id}/talkTitle POST https /{id}/newProfile DELETE https /{profileId} • A piece of code is executed when these APIs are called. • Server returns the response to client

  6. Problems faced in RESTful APIs • Multiple Endpoints • Over Fetching • Under Fetching

  7. What can be the alternative? GraphQL

  8. • Open Source GraphQL • GraphQL is a Query Language • Uses Schema based system • Easy and efficient to use

  9. Why GraphQL? • Client requests the needed data. Client decides the query and according to that data is fetched.

  10. And you know what... • Only one API Endpoint • No over fetching or under fetching • Auto-generation of API documentation

  11. Let's learn about GraphQL • Schema : structure • Mutation : updating data on server • Queries : fetching data • Subscriptions : real time data exchange

  12. Schema • GraphQL Object Type ⚬ Product • Fields ⚬ productId ⚬ productName ⚬ and few listed in pic • Scalar Types ⚬ Int ⚬ String Product Schema ⚬ and many more...

  13. Mutation • Used for changing data on server • Return the response according to your needs • Variables passed can be scalars or ObjectTypes Response from server

  14. Using query variables for inserting data

  15. Query • Get data from server • Ask for specific fields on objects • Design query according to needs Response from server

  16. Subscriptions • Realtime connection to subscription{ server addProduct{ productName, • Client subscribes to an event productPrice, • Server pushes data to client productDesc } when event occurs { } "addProduct" : { • Same syntax as queries and Response from server "productName" : " Toy", mutations "productPrice" : 500, "productDesc" : "New toy" } }

  17. Libraries for building GraphQL APIs in Python GRAPHENE STRAWBERRY ARIADNE 5.6k+ Stars 456 Stars 896 Stars Code-First Approach Code-First Approach Schema-First Approach

  18. Let's Build GraphQL APIs We will use • • •

  19. Environment setup • ⚬ • ⚬ ⚬ ⚬ ⚬ ⚬

  20. • ⚬ • ⚬ •

  21. Add the following in your settings.py file GRAPHENE = { 'SCHEMA' : 'project.schema.schema' }

  22. Few Concepts of Graphene • • • •

  23. ObjectType • A block which is used to define a relation between fields and schema

  24. Schema • Relationship between fields in API

  25. Resolvers • A method that helps to answer queries • • •

  26. Resolvers • • • • query($author: String!, $repoName: String!){ repo(login: $author){ repository(name: $repoName){ forkCount, updatedAt } } }

  27. Scalars • a.k.a : Data types • • • • • •

  28. QUERIES MUTATIONS Fetch the data from Update the data on server server PAGINATION AUTHENTICATION Send a particular Secure your backend bunch of data instead API's using JWT Tokens of complete data

  29. File Structure

  30. Writing FETCHING DATA Queries Used concepts : ObjectType, DjangoObjectType, resolvers, Schema

  31. Writing UPDATING DATA ON SERVER Mutations Used concepts : ObjectType, DjangoObjectType, arguments, mutate

  32. GraphiQL Add this in your urls.py file path('graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True))) View Write your graphql Server response query/mutation here....

  33. Pagination SENDING A BUNCH OF DATA Used concepts : ObjectType, DjangoObjectType, Python slicing

  34. Authentication SECURING USING JWT TOKENS settings.py .project/schema.py

  35. • Created a mutation for creating a user SECURING USING JWT TOKENS • Create a user • Use this mutation while login • Store this token and use for further queries.

  36. user = info.context.user Add this is your queries SECURING USING JWT TOKENS if user.is_anonymous: and mutations raise Exception("Not logged in!!") Add JWT Token in Headers prefixed by "JWT"

  37. SECURING USING JWT TOKENS Mutation without JWT Token

  38. Helpful Resources • GraphQL Website : https://graphql.org/ • GraphQL Blogs : Medium • HowToGraphQL : https://www.howtographql.com/ • Graphene Documentation

  39. django-ecommerce Open Source -graphql 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

  40. THANK YOU CONNECT WITH ME! iamnisarg.in FINAL WORDS nisarg1499 nisargshah14 nisshah1499@gmail.com - NISARG SHAH

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend