d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

GraphQL Using Python – Graphene Part-1

Introduction

  • GraphQL is a query language developed by Facebook in 2012 and was made open-source in 2015.
  • GraphQL is a server-side language for executing the queries.
  • GraphQL is considered the biggest competitor for REST API’s as it improves data retrieval and boosts the fetching power.
  • GraphQL enables users to get only the required dataset and no other unused data, which makes it much useful and economical compared to REST API.

Graphene

Graphene is a python library for creating GraphQL APIs in python easily and with very little code. In Graphene, we follow the code first approach as compare to the schema first approach in Apollo Server or Ariadne. Graphene can be easily integrated with popular web frames such as Django, Flask and bottle, so it makes it very easy to integrate GraphQL with all these frameworks.

Demo

Requirements-
  • Python>=3
  • Grapghene>-2.0

Steps

  • Install requirements, for graphene =>. pip/pip3 install graphene==2.0
  • Create a class Query that will inherit from graphene
  • Define two parameters book_name and book_author with both of type string, remember to use grapghene data types only
  • Create a resolver for both of them so that they can be fetched while calling the query
  • Resolvers are created for every field that can be query, every field has its resolver or we can say that every field lie in a resolver whenever a particular field is passed in a query, the resolver of that particular field is called.
  • Create a schema that is by using graphene, we can directly use graphene.Schema and can pass Query inside it.
  • In case of scehma it will always be camelCasing by default so you need to change it if you want it to be of any other type. auto_camelcase is the keyword to that
  • Now we can simply write our query and execute it, to see the result.
  • Here it is our first GraphQL query has been executed.
  • To change the name of response or have only specific parameters we need to only change the query nothing else.
  • Backend can be maded fix by this and we need to only change the query from the frontend to play with the response.

So that’s it from the first part, I will be adding more blogs on this series and in the next part we will talk about mutations that is the CRUD operations in GraphQL.

References-

  • https://graphql.org/
  • https://docs.graphene-python.org/

Still Curious? Visit my website to know more!

For more interesting Blogs Visit- Utkarsh Shukla Author

I am a versatile professional who specializes in empowering businesses with digital solutions. With expertise in software engineering consulting, I provide valuable guidance and support to companies seeking to enhance their technological capabilities. Additionally, I excel as a host(Professionals Unplugged), author, poet, and photographer. This diverse skill set allows me to creatively express my thoughts and ideas through various mediums. As a host, I engage audiences and facilitate insightful conversations. As an author, my writings offer valuable insights and knowledge. As a poet, I capture emotions and experiences through the power of words. And as a photographer, I have a keen eye for capturing captivating visuals. Overall, my wide range of skills and experiences make me a valuable asset in the world of digital solutions and creative expression

Add Comment