Skip to main content

A lightweight python library for implementing GraphQL on Relational DB Tables in few steps using python dicts. The library is built over strawberry-graphql for creating graphQL schema from dataclasses

Project description

GoRDB - GraphQL On Relational DB

A lightweight python library for implementing GraphQL on Relational DB Tables in few steps using python dicts. The library is built over strawberry-graphql for creating graphQL schema from dataclasses

Usage Example

GoRDB makes it easy to implement graph schema and graph queries like one given below on traditional Relational DBs.
Example Graph query on Relational DB having User table and Subscriptions table implemented using GoRDB

{
    User(filterStr:" @#ROWNUM#@ <5 ")
    {
        USERID,
        USERNAME,
        Subscriptions{
            PRODUCTID
        }
    }
}

Installation Steps

Install using the following command
pip install GoRDB

Implementation Steps

Step-1:

Import GoRDB library
from GoRDB import GoRDB

Step-2:

Initialize connection configs for your DB. Here config is shown for Oracle DB, You can set this for any RDB of your choice
import cx_Oracle as orac
orac.init_oracle_client(lib_dir= "./instantclient_19_12")
import pickle
db=pickle.load(open("G:db_config.pickle","rb"))
username=db['username']
pwd=db['pwd']
db_uri=db['uri']

Step-3:

Create function which will receive an connection-id (string) and query (string) as arguments and return query results ( List of tuples) as return
connection_pools_by_connection_id={}
connection_pools_by_connection_id['CUSTOMERDB'] = orac.SessionPool(username, pwd, db_uri,min = 5, max = 20, increment = 5, threaded = True,getmode = orac.SPOOL_ATTRVAL_WAIT)

def query_executor(connection_id,query_str):
    global connection_pools_by_connection_id
    custdb=connection_pools_by_connection_id[connection_id].acquire()
    local_cursor=custdb.cursor()
    local_cursor.execute(query_str)
    query_result=local_cursor.fetchall()   
    return query_result

Step-4:

Add node definition for each node in your graph layer as dict in the format given below.
Nodes define the table in your Relational DB. Node alias is the node name which will be used in your Graph queries
Features define the columns in your RDB table
Edges define the relation between on node to another and corresponding mapping keys. set many_mapping = True if you the edge define one-to-many relationship
Table connection id define the connection identifier which will be passed to query executor function
Query executor define the function which will be called to execute query.


Here for the example shown above we have 2 nodes.

USER_node_dict={
    'node_alias':'User',
    'tbl_name':'CUSTOMERDB.USER_TABLE',
    'tbl_connection_id':'CUSTOMERDB',
    'query_executor':query_executor,
    
    
    'features':[
        {'feature_alias':'USERID','feature_name_in_table':'USERID_SNO'},
        {'feature_alias':'USERNAME','feature_name_in_table':'USERNAME'},
    ],
    
    'edges':[
        {'edge_alias':'Subscriptions','node_feature_alias':'USERID','foreign_node_alias':'Subscriptions','foreignNode_feature_alias':'SUBSCRIBERID','many_mapping':True}
        
    ]
        
}


SUBSCRIPTIONS_node_dict={
    'node_alias':'Subscriptions',
    'tbl_name':'CUSTOMERDB.SUBSCRIBER_PRODUCT_TABLE',
    'tbl_connection_id':'CUSTOMERDB',
    'query_executor':query_executor,
    
    
    'features':[
        {'feature_alias':'SUBSCRIBERID','feature_name_in_table':'SUBSCRIBERID_IDX'},
        {'feature_alias':'PRODUCTID','feature_name_in_table':'PRODUCTID_IDX'},
    ],
    
    'edges':[]
}

Step-5:

Build your GraphQL Schema by passing the list of node definitions dict
schema,node_builds_compiled=GoRDB.build_scheme_from_node_dict([USER_node_dict,SUBSCRIPTIONS_node_dict])

Step-6:

Thats all! Execute your Schema, and await on the response. Now you have succesfully running Graph query engine on you RDB tables

Note: the filterstr corresponds to the "WHERE" part of your nomrmal SQL query. if you want to use some table columns which are not defined in your node definition dict you need to wrap them with ' #@' and '#@ ', but if the column is already defined in the node definition dict this is not need(for example 'USERID' used in the below query)

ding=schema.execute('{User(filterStr:" USERID > 5 and  @#ROWNUM#@ <5 "){USERID,USERNAME,Subscriptions{PRODUCTID}}}')
print(await ding)

Other features

GoRDB have helper function called show_graph which can be used to see the network diagram of your Schema

GoRDB.show_graph([USER_node_dict,SUBSCRIPTIONS_node_dict])

show_graph_example

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

GoRDB-6.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

GoRDB-6-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file GoRDB-6.tar.gz.

File metadata

  • Download URL: GoRDB-6.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.6.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for GoRDB-6.tar.gz
Algorithm Hash digest
SHA256 0687b723e8810a31d4708fbcfc2e93d38718f867678576b7cc1e57f31cb86276
MD5 34ca1b427b95ec5145f67466a9c25076
BLAKE2b-256 6e70ee03038e667c057b29498330dfb2bedec7cfd138bc71ee4cc0fef2fa51ea

See more details on using hashes here.

File details

Details for the file GoRDB-6-py3-none-any.whl.

File metadata

  • Download URL: GoRDB-6-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.6.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for GoRDB-6-py3-none-any.whl
Algorithm Hash digest
SHA256 432c228f5eaf183b4598610c9a53a24009139d6af1dfc80a807d7f08bd74f778
MD5 93b3d281c59d65ee6b06890e9f4f23d4
BLAKE2b-256 b52e3fa0d65f5826d0af74f752c8aff4ea1e9981dfcbdd6a12e4fd1c73c066ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page