DEV Community

Cover image for Introduction to GraphQL using Python
Suhail
Suhail

Posted on

Introduction to GraphQL using Python

GraphQL is a data query language developed internally by Facebook in 2012 before being publicly released in 2015. It provides an alternative to REST and ad-hoc web service architectures.

Graphene-Python

Graphene-Python is a library for building GraphQL APIs in Python easily, its main goal is to provide a simple but extendable API for making developers’ lives easier.

When to use GraphQL?

If you are creating a data-driven application of at least moderate complexity, GraphQL should absolutely merit primary consideration.

Why Use GraphQL?

  • GraphQL APIs have a strongly typed schema
  • Solves the problem of over-fetching and under-fetching
  • GraphQL enables rapid product development
  • Composing GraphQL APIs (Schema Stitching)
  • Rich open-source ecosystem and an amazing community

GraphQL Ecosystem

GraphQL Ecosystem has three main classes:
Query — way to fetch data in a read-only manner from your GraphQL API (analogy of GET request in REST API).
Mutation — way to change(create/update/delete) data on your server (analogy of POST request in REST API).
Subscription — way to get a real-time feed of data from your server (something like traditional Web Sockets)

GraphQL Schema

It can be compared to a dictionary in Python or object in Javascript, if it is possible to say, where the key is the name of a field and value is a type of this field.

Supported data types in GraphQL

Scalars(Int, Float, String, Boolean, ID) List, Enum, Non-null are few of the most commonly used data types in GraphQL.
You can find out more info about data types in GraphQL here.

Your First GraphQL App! (with GraphQL and Python)

Create a project directory, navigate to the same and create a schema.py file.

$ mkdir python-graphene
$ cd python-graphene && touch schema.py

Configure the pipenv environment and install the graphene module.

$ pipenv shell
(python-graphene)$ pipenv install graphene

Let's write our first app now that we have the environment set up with the required module in place.

Line Numbers 5–13: The Query class is a special ObjectType that sub-classes from graphene.ObjectType which defines the fields that are the entry-point for your API which in the above example is user and is_admin.

Line Number 6–9: In the above snippet which is for the resolver function in graphene, this helps fetch the required data to be returned when querying for that parameter. These functions should be prepended with resolve and should follow snake case styling. In the above example, these functions are resolve_user and resolve_is_admin.

Resolver functions, in general, is a collection of functions that generate a response for your GraphQL query. In simple terms, a resolver acts as a GraphQL query handler.

Line Number 16: The schema defines the types and relationships between Fields in your API. A Schema is created by supplying the root ObjectType of each operation, query (*mandatory), mutation and subscription. In this case, we have just supplied the Query operation.

Line Number 18:: Finally, in order to call the schema that we have created in the above example, we have the query string which is within comments and is passed with schema.execute and the same is converted to a JSON and printed to the standard out.

Running the schema.py file within the environment that was created should result in the below output

$ python 1_schema.py 
{
  "user": "Benjamin Button",
  "isAdmin": true
}

Top comments (0)