GraphQL with Python — Part 1

Imranshakeel Khan
3 min readFeb 16, 2022

Graphene is the best library for Integrating GraphQL in Python. It’s actively developed. It has pretty complete helper libraries for SQLAlchemy, Django’s ORM, and MongoDB. It’s easy to get something simple going. That said, Graphene’s documentation leaves a lot to be desired. It’s easy to get something simple going in GraphQL using their documentation, but getting something hardened, production ready, and capable is another story.

If you’re starting out in Graphene, go to their website and get through the tutorial. I’ll not repeat it here.

then stay here. For this post I’ll be using Flask and SQLAlchemy. Django is relatively simple to get going, and SQLAlchemy is a bit more involved, so I think it’ll be more illustrative.

Packages Needed

Following packages are required to configure GraphQL in Python

pip install graphene_sqlalchemy
pip install flask_graphql
pip install “graphene>=2.0”
pip install — upgrade pip enum34

Why GraphQL?

For me, GraphQL lets my backend and frontend developers work together with less friction about data contracts. You still have to have them, and you still have to optimize for them, but there is an opportunity to “start with the kitchen sink.” and iterate. You’ll begin with a non-performant but correct query and work toward a performant one that exposes the data that is needed by your apps and integration partners. It’s just a smoother process in general.

I personally don’t use GraphQL for mutations, so I won’t be writing about them in this post. I still use REST and RPC-style endpoints for those. Our developers don’t like the syntax for mutations, and to them it seems easier to write buggy code with mutations vs. REST. If you want to use mutations, I’d suggest you start with Marc-Andre Giroux’s post on graphql mutation design and branch out from there.

Shortly after GraphQL came out, Relay became a popular way to structure GraphQL schemas. Relay organizes GraphQL results as nodes and edges. It’s a bit more involved to parse, but it’s useful for us because it also defines a standard, automatic and efficient way to paginate results.

In Nutshell Following are the major reasons why GraphQL Comes in picture

  1. Prevents Over-Fetching and Under-Fetching Issues
  2. Prevents Multiple and Useless Roundtrips To Server
  3. Prevents Cache during HTTP GET Requests
  4. User knows what he will get in response

But it does not mean that GraphQL is absolute replacement of REST Architecture. REST is absolutely stands with its importance. In many scenarios REST works much better than GraphQL. REST started to emerge in 2000 and due to its features it is still the considered best practice for designing API. Many technologies appeared which could be considered replacement of rest but REST Stands where it is. Following is the diagram to give you the best idea about history of REST and Other Technologies

When GraphQL is not Ideal

  1. When you need to upload files from API
  2. When Web Caching Is Needed
  3. When user wants to work very complex Queries
  4. When Server-Driven approach is required
  5. When User wants to work with Endpoints
  6. When HTTP Verbs are required for Communication

GRAPHQL In Action

At the end of the tutorial, your first query looks like this:

{
myWorkflows{
all {
node {
id
Workflowname
workflowList {
Type
}
}
}
}
}

Following is the code to Build GraphQL Class

class Query(graphene.ObjectType):
node = relay.Node.Field()

workflows = relay.Node.Field(workflows)

workflows = SQLAlchemyConnectionField(workflowConn)
workflowTypes = SQLAlchemyConnectionField(TypesConn, sort=None)

Then we can change our GraphQL query to hit employee directly:

{
workflow(id:"11112") {
name
types{
Id
}
}
}

Following will be the expected Result

{
"data": {
"workflows": {
"name": "Workflow1",
"type": {
"name": "WorkflowType"
}
}
}
}

Setting Up Playground For Queries

In Main.py need to do following settings

from flask import Flask
from flask_graphql import GraphQLView
from GraphSQLSchema import schemaSQL

app = Flask(__name__)
app.debug = True

app.add_url_rule(
'/graphql',
view_func=GraphQLView.as_view(
'graphql',
schema=schemaSQL,
graphiql=True,


)

)

Type http://127.0.0.0/graphql following interface will appear

Upcoming:

  1. GraphQL Mutation
  2. GraphQL Advance Implementation
  3. Implementing Security with GraphQL

--

--