Query ORACLE Database Data Using GraphQL-Python
By: Mohammad H. Abdelqader
Tags: Oracle, GraphQL, API, Python

How To Guide

Create graphoracle project in django

First we need to create django project. I created project called graphoracle

(base) D:\django-graphql>django-admin startproject graphoracle

The command will create required libraries and create django project.

After project created we can run django server

(base) D:\django-graphql>python manage.py runserver

To test, we can access http://localhost:8000 address. The following page must return

You can find project files in github repo

Modify settings.py file for graphoracle project

We need to add the following settings for project:

Add Oracle database connection setting

we specify the database server ,port, service name ,username and password in this setting

Add graphene package and schema path

We must added the graphene_django in INSTALLED_APPS list. Also, we define the SCHEMA that we will used inside GRAPHENE setting

Create hr app in graphoracle project

The next step is to create hr application inside our project which will contain our models and schemas

(base) D:\django-graphql\graphoracle>python manage.py startapp hr

(base) D:\django-graphql\graphoracle>cd hr

(base) D:\django-graphql\graphoracle\hr>dir

Volume in drive D is DataDisk

Volume Serial Number is 88F9-D7BD Directory of D:\django-graphql\graphoracle\hr 11/19/2019 09:49 AM <DIR> .

11/19/2019 09:49 AM <DIR> ..

11/19/2019 09:49 AM 66 admin.py

11/19/2019 09:49 AM 84 apps.py

11/19/2019 09:49 AM <DIR> migrations

11/19/2019 09:49 AM 60 models.py

11/19/2019 09:49 AM 63 tests.py

11/19/2019 09:49 AM 66 views.py

11/19/2019 09:49 AM 0 __init__.py

6 File(s) 339 bytes

3 Dir(s) 23,212,933,120 bytes free

Inspect database tables using inspectdb tool

This is a great tool that will get all tables from database and build class for each table and define filed for each column.

(base) D:\django-graphql\graphoracle> python manage.py inspectdb > hr\models.py

After inspect finish, we must check models that created. sometimes we need to make changes depend in our tables

Following is sample of models.py file

Migrate graphoracle project with database

We will run two django scripts:

makemigrations: This script will generate the required SQL commands that need to run in database

migrate This will execute the generated SQL commands. When run this script the django will create some tables inside database. These tables used from django project

(base) D:\django-graphql\graphoracle>python manage.py makemigrations

No changes detected (base) D:\django-graphql\graphoracle>python manage.py migrate

Operations to perform:

Apply all migrations: admin, auth, contenttypes, sessions

Running migrations:

Applying contenttypes.0001_initial... OK

Applying auth.0001_initial... OK

Applying admin.0001_initial... OK

Applying admin.0002_logentry_remove_auto_add... OK

Applying admin.0003_logentry_add_action_flag_choices... OK

Applying contenttypes.0002_remove_content_type_name... OK

Applying auth.0002_alter_permission_name_max_length... OK

Applying auth.0003_alter_user_email_max_length... OK

Applying auth.0004_alter_user_username_opts... OK

Applying auth.0005_alter_user_last_login_null... OK

Applying auth.0006_require_contenttypes_0002... OK

Applying auth.0007_alter_validators_add_error_messages... OK

Applying auth.0008_alter_user_username_max_length... OK

Applying auth.0009_alter_user_last_name_max_length... OK

Applying auth.0010_alter_group_name_max_length... OK

Applying auth.0011_update_proxy_permissions... OK

Applying sessions.0001_initial... OK

Add hr app to settings.py file

In this step we will add hr application in our project.

In GraphQL, a Type is an object that may contain multiple fields. Each field is calculated through resolvers, that returns a value. A collection of types is called a schema. Every schema has a special type called query for getting data from the server.

Create GraphQL schema class

We need to create schema.py file inside hr application directory then create DjangoObjectType ( a custom type available in Graphene Django) for each database object defined inside models.py

Also, we need to create Query class which contains resolver method for each schema.

Following are example from schema.py file

Create schema class in graphoracle project

Why creating another schema class with Query class inside? This class just inherits the query defined before. This way, you are able to keep every part of the schema isolated in the apps.

The following is the schema.py file.

Enable GraphiQL

GraphiQl is an interactive web tool that used to test and play with GraphQL.

To Enable GraphiQL in our project we need to add graphiql url inside url.py file inside graphoracle project

Query data from Oracle table using GraphQL

Finally we can query data from oracle table using GraphQL. We use GraphiQL tool to run query