How to develop a Flask, GraphQL, Graphene, MySQL, and Docker starter kit


How to develop a Flask, GraphQL, Graphene, MySQL, and Docker starter kit

Up until now, I have mostly been developing new Flask projects from scratch. As most projects tend to consist of similar folder structures, it gets really mundane setting up the identical base projects repeatedly over a period of time.

I have been using Docker for some time now, and with the capability that it enables, we are able to create a starter-kit image with required core functionalities implemented, that can be served almost instantaneously.

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:

Let’s begin!

For this tutorial, we will need:

Setting up Flask

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.

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

Application Factories Pattern

To future proof ourselves, and to make our project scalable, we will be following the Application Factory Pattern. I will not go too deep into the details of “Why” in this article. But the gist of it would be that it provides us with

We will start by creating our Flask app file and implementing the basic flask functionality.

The create_app() function is a simple example of how the Application Factories Pattern is implemented.

With reference to the Flask-Script documentations, we will also add the following codes to our ./manage.py file.

After setting up these files, we can start our application by running the command python ./manage.py runserver.

At this point, let’s do a pip freeze >> requirements.txt .

It’s always a good habit to do pip freeze after installing a new package and you are certain that you will be using it in your new project. It allows you to reinstall all the required dependencies in a different development environment.

Docker implementation

Before you proceed further, do ensure that you have Docker installed.

If you are unfamiliar with Docker, I highly recommend that you give this article by Jeff Hale a read.

Now that we have a basic “Hello World” Flask application setup, we can follow up by containerizing our application with Docker.

Setting up Docker

Start by creating a file called Dockerfile in our root folder. This file contains the instruction for Docker to execute while building up our Flask project image. It allows us to spin up similar instances with a single command.

With this, we can actually start running our application through the Docker container.

After running the commands, you can once again access the flask server at http://localhost:5000.

Setting up Docker-Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With it, we can serve multiple services at once as well as link multiple Docker images with each other.

We can configure a YAML file docker-compose.yml in our root folder that contains all the information of our multiple services, and how each image links with each other.

Then, with a single command, we can create and start all the services from the configuration.

We can set up our Flask service to link to whichever database service that we require, such as PostgreSQL, NoSQL, etc. In this case, we will go with MySQL.

With this configuration, when we run the command docker-compose up, Docker will serve both our Flask application, as well as a connected MySQL service. We can then start adding code to integrate our Flask application with the MySQL service.

Integrating MySQL with Flask

We will first have to install the required dependencies

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 elegantly separate our environment variables, as well as store our constant variables as required.

We can then further update our application file to set up and teardown our database 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. However, there will not be any SQL actions being executed as we do not have any models set up yet.

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.

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.

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

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.

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.

This content was originally published here.

Other FinTech Healines