logo-StatusNeo
d

WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

Image Alt

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-

An optimistic Engineer passionate about technology, science, astrology, and photography. Visit this to know more- https://utkarshshukla.herokuapp.com/

Add Comment