GraphQL With Python Flask

GraphQL, Graphene, Flask, and Mysql

GraphQL With Python Flask

Before we get started, let’s talk about our objective first.

We are trying to develop a starter-kit project that runs on Flask, that has the following functionalities:

  • GraphQL support

  • Database integration

Prerequisites

For this tutorial, we will need:

- Python
- Pip
- Virtualenv
- Docker

Setting up Flask

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. — Flask Documentation

Setting up a barebones Flask project is really simple. However, since there’s quite a bit of functionality that we want to be built in, the following steps will take slightly longer than usual.

Start by creating a virtual environment.

$ virtualenv venv
$ source venv/bin/activate

Next, we can install the required packages. As to not confuse everyone, I’ll include only the required packages for each step respectively.

$ pip install flask flask_script
$ mkdir -p app/__init__.py
$ touch manage.py

Integrating MySQL with Flask

We will first have to install the required dependencies

$ pip install flask-sqlalchemy pymysql

We can now start to set up our Flask app to connect with our MySQL service. For that, we will have to add a config.py file and also update our app/__init__.py file.

Using a config file, we are able to separate our environment variables, as well as store our constant variables as required.

With this, when we run the docker-compose up, the application will start running and it will be connected with the MySQL service.

Setting up GraphQL

GraphQL is a query language for APIs and runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need, and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

$ pip install graphene Flask-GraphQL

Let’s do a breakdown of the packages that we just installed.

Flask-GraphQL

Flask-GraphQL is a wrapper layer that adds GraphQL support to the Flask application.

We’ll have to add a /graphql route for the client to access the APIs. To do that, we can simply update our app/__init__.py file.

from flask_graphql import GraphQLView
def create_app():
    ...    from app.schema import schema    app.add_url_rule(
        '/graphql',
        view_func=GraphQLView.as_view(
            'graphql',
            schema=schema,
            graphiql=True  # for having the GraphiQL interface
        )
    )

In this case, we have imported GraphQLView from the Flask-GraphQL module and have used it to implement our /graphql route.

Graphene

Next, we will be using the Graphene-Python package. Graphene is an open-source library that allows developers to build simple yet extendable APIs with GraphQL in Python.

If you have noticed from our latest app/__init__.py file, we are actually missing an app/schema.py file for our Flask application to work. A GraphQL schema is at the core of any GraphQL server implementation and describes the functionality available to the clients which connect to it.

In this case, we shall create a simple schema so that our application will be able to run without any error.

from graphene import ObjectType, Stringclass ExampleQuery(ObjectType):
    hello = String()

    def resolve_hello(self, info):
        return "Hello"
class RootQuery(ExampleQuery, ObjectType):
    pass
schema = Schema(query=RootQuery)

With this, try running your application. When you access http://localhost:5000/graphql, you should be able to see this page. If you are familiar with GraphQL, this page should be very familiar to you. It’s an interface that allows you to enter GraphQL queries.

Conclusion

I hope this article will give you a better understanding and overview of how you can build your Python application with GraphQL. This can also be done with the Django framework.

I have also added a Github repository for reference here.