Most modern applications are developed in a decoupled manner. The front end and backend are separate applications, often connected via an application programming interface (API). This makes knowledge of APIs and API development paramount.
With a robust API, a backend system can serve several frontend clients. They could be web or mobile-based clients.
This guide assumes you have a basic understanding of APIs and Django web framework and at least intermediate-level proficiency in Python and GraphQL.
An introductory guide on Django can be found here.
Ariadne GraphQL is an interpretation of graphql for Python. It was developed by the software house Mirumee. Its main aim was to address the shortcomings of graphene.
To install Ariadne and the GraphQL core, run the command below on your terminal.
1pip install ariadne graphql-core
For this exercise, you will develop a Django app that displays data from the default Sqlite3 database. The data is a list of schools and their populations.
To get started, start a new Django project by running the below command.
1django-admin startproject school_list
Next, create an app and name it school_stats using the below command.
1python3 manage.py startapp school_stats
The app is now set up. What remains is to develop the school model, the Ariadne GraphQL schema that consists of queries and mutations and to configure the URL to show the GraphQL playground interface.
The code block below shows the model and what fields make up the database table.
Copy the code block into the models.py
file.
1from django.db import models
2
3class School(models.Model):
4 school_name = models.CharField(max_length=20)
5 school_population = models.IntegerField(default=100)
6 added_on = models.DateTimeField(auto_now_add=True)
7
8 def __str__(self):
9 return self.school_name
The model needs data. To add the data, you will need to access the admin panel. This requires superuser access. The next step is to create a superuser account using the following command.
1python manage.py createsuperuser
This will be an interactive process where you will give essential details and register your user.
For the app and model to be visible, they need to be registered with the Django project and admin.py
.
To register the app and also Ariadne, a third-party app, add their names to the list of installed apps in settings.py
.
Copy the list below to your settings.py
and replace the existing list.
1INSTALLED_APPS = [
2 'django.contrib.admin',
3 'django.contrib.auth',
4 'django.contrib.contenttypes',
5 'django.contrib.sessions',
6 'django.contrib.messages',
7 'django.contrib.staticfiles',
8 'school_stats',
9 "ariadne.contrib.django",
10]
To ensure the School
model is visible on the admin pane, register it in the admin.py
file.
Add the code block below to your admin.py
file.
1from django.contrib import admin
2from .models import School
3admin.site.register(School)
The first step in developing the GraphQL part is developing the schema. In your schema, you will have
a mutation for adding schools, a School
input object, a query for viewing schools, and a School
object that will be returned as a result of the query.
Since the schema is simple, you can do an in-file schema.
The code block below defines the schema and assigns the type_defs
variable.
Within the school_stats
app, create a file and name it resolver.py
. Add the following block.
1type_defs= """
2
3type Query{
4 all_schools: [School]
5}
6
7type School {
8 id: ID
9 school_name: String!
10 school_population: Int!
11
12}
13type Mutation{
14 add_school(input: SchoolInput): SchoolResults
15
16}
17input SchoolInput {
18 school_name: String
19 school_population: Int
20}
21 type SchoolResults {
22 created: Boolean!
23 school: School
24 err: String
25}
26
27"""
To set up the query, you need to return data from the model. This requires a query resolver for the query all_schools
created in the schema.
In your resolver.py
file, add the following function.
1from ariadne import QueryType, make_executable_schema, MutationType
2from .models import School
3query = QueryType()
4
5@query.field('all_schools')
6def resolve_schools(*_):
7 return School.objects.all()
Similar to the queries, set up a resolver for the mutation that will create a record in the database.
1mutation = MutationType()
2
3@mutation.field('add_school')
4def resolve_add_school(_,info, input):
5
6 school = School.objects.create(school_name=input['school_name'], school_population=input['school_population'])
7 return {'created': True, 'school': school, 'err': None}
To execute the schema and make it available for use in the GraphQL playground, add the following line of code.
1schema = make_executable_schema(type_defs, query, mutation)
To configure the URL, configure the main project's urls.py
file to direct any traffic to the school_stats app using the path
function, as in the code block below.
1from django.urls import path, include
2from django.contrib import admin
3urlpatterns = [
4 path('admin/', admin.site.urls),
5 path('', include('school_stats.urls')),
6
7]
Next, create a urls.py
file and load the graphQL view and schema to the default URL.
1from django.urls import path, include
2from ariadne.contrib.django.views import GraphQLView
3from .resolver import schema
4urlpatterns = [
5 path('graphql/', GraphQLView.as_view(schema=schema), name='graphql'),
6 path('', GraphQLView.as_view(schema=schema), name='graphql'),
7]
When all has been set up, the final step is migrating the changes and running the server. To make migrations, run the command:
1python manage.py makemigrations
To migrate the model changes into the default database, run the command:
1python manage.py migrate
To start the server and run the app, run the command:
1python manage.py runserver
In the GraphQL playground, execute the below mutation to create a record. To create more records, change the variables.
1mutation {
2 add_school(input: {school_name:"SchoolZ", school_population:330})
3 {
4 created
5 school{
6 school_name
7 school_population
8 }
9 err
10 }
11}
Mutation Result
1query{
2 all_schools{
3 school_name
4 school_population
5 }
6}
Query Result
The vital skill of API development can be applied in job positions such as backend developer, API/middleware developer, and full stack developer.
Ariadne GraphQL is a relatively new package, and it has some interesting use cases. To further build on these basics, follow these Django integration and Flask integration guides to learn how to use Django, Flask and Ariadne GraphQL to develop a microservice.