logo

Get in touch

Awesome Image Awesome Image

Python Development February 27, 2023

Developing Scalable APIs with GraphQL and Python

Written by Dharmesh Patel

1.3K

Introduction of Graphql

GraphQL is a query language for APIs that was developed by Facebook in 2012. It provides a more efficient, powerful, and flexible way of querying and updating data on the server compared to REST. GraphQL has become increasingly popular in recent years, and many programming languages, including Python, now have GraphQL libraries and frameworks available.

Graphql stands for Graph Query Language, but unlike other query languages such as SQL, it is not a language for communicating directly with a database, but rather a language that defines a contract through which a client communicates with an API server. The GraphQL specification is an open standard that describes the rules and characteristics of the language. It also provides instructions for executing a GraphQL query.A GraphQL API is defined with a single endpoint, usually the /GraphQL endpoint, which can access the full capabilities of the GraphQL server. Since GraphQL is an application layer technology and is transport agnostic, it can be served over any protocol, but it is most commonly served over HTTP. 

A GraphQL API is defined with a single endpoint, usually the /GraphQL endpoint, which can access the full capabilities of the GraphQL server. Since GraphQL is an application layer technology and is transport agnostic, it can be served over any protocol, but it is most commonly served over HTTP. 

Graphene Python 

  • Python is considered among the most beloved general-purpose programming because of its ease of utilization and simplicity.
  • Graphene is among the best libraries to create the endpoints of GraphQL like python. it is dynamically advanced. It contains entire helper libraries for ORM of Django, SQLAlchemy, and MongoDB. It is considerably effortless to achieve something simple.
  • In this tutorial we are focusing on how to implement  GraphQL with python using the Graphene library. 

Installation

  • Enter the following command to create a new virtual environment with the name “myenv” (you can replace “myenv” with a name of your choice):

    python -m venv myenv

  • Once the virtual environment is created, you can activate it by entering the following command.

    source myenv/bin/activate

  • After activating environment run :

    pip install graphene

  • You can use graphene by importing in script like :-

    import graphene

Create Schema

  • GraphQL schema is present at the base of each API of GraphQL. 
  • It helps in describing the types, fields, objects for the exposed API. 

Explanation

  • In the above image of code, we have imported the graphene library and defined a class as myWebsite that inherits the Object Type class of the  graphene library. This Object Type acts as a building block utilized in order to define the relationship between the fields in the schema and the way of retrieving their data.
  • Inside the class we have defined different fields and used the String() of the graphene library to describe the types of fields; however each field could be another object we have described of several other lists, scalars, enums, and many more.

Explanation

  • Above snippet of code, website1 is an object type that we support querying against.
  • In website1 use Field functionality of graphene which takes schema object and type of  my_url as parameter.
  • my_url is in a parameter that we will pass along to the resolution function (resolve_website).
  • Then website1 calls the resolve_website function by each request. 
  • In the resolve_website function it calls extract() function and stores data in the extracted variable.
  • Then pass extracted data in my_Website fields and return it. 

  • The last step is to create an instance of Graphene.Schema, which we will pass to the server in order to describe the new API we have created.
  •  We have created the schema for the project successfully. 

Server

  • We have written the schema now and we can begin serving it over HTTP with the help of flask and flask-graphql.
  • Installation
    • Pip install flask
    • Pip install flask-graphql
  • Create my_server.py file.

Explanation

  • In the above image we have imported the required libraries in addition to the file named my_schema that we created earlier. We have then used the Flask() function specifying the parameter as __name__ to create the application. We have also added different rules for the URL using the add_url_rule() function. Where we have specified different parameters and used the run() function at last to execute the application.
  • Run the server using  python my_server.py.

Client

  • Create my_client.py. 

  • In the above image we have imported the requests library.
  • my_query is the query which is sent to the server.
  • We have defined a variable as my_response, which will store the data returned in the form of a response from the server.
  • You can also customize the content of the my_query variable in order to retrieve various fields or even utilize things such as aliases in order to retrieve more than one object at a time. 

OUTPUT

Introspection

  • Among the most powerful aspects of GraphQL. Its servers support introspection. 
  • Introspection allows both humans as well as automated tools in order to understand the available objects and operations.
  • For example, while running the example we built, we can navigate to Localhost:5000 and utilize GraphQL in order to test the new API directly.
  • This capability is not restricted to GraphQL. Let us consider a simple example where we ask about the available queries exposed by the sample service. 

The server reply would be:

Conclusion

Python coupled with GraphQL offers several advantages including enhanced efficiency, improved performance, and increased flexibility when retrieving data. With GraphQL, clients can precisely specify the data they require, leading to less data transferred across the network, resulting in quicker responses and less server load.

Adopting GraphQL with Python is an excellent option for creating scalable and contemporary APIs. Nonetheless, it might not be the optimal choice for all projects, depending on the particular use case and project requirements.

Bringing Software Development Expertise to Every
Corner of the World

United States

India

Germany

United Kingdom

Canada

Singapore

Australia

New Zealand

Dubai

Qatar

Kuwait

Finland

Brazil

Netherlands

Ireland

Japan

Kenya

South Africa