graphql-db-api
![codecov](https://codecov.io/gh/cancan101/graphql-db-api/branch/main/graph/badge.svg?token=TOI17GOA2O)
A Python DB API 2.0 for GraphQL APIs
This module allows you to query GraphQL APIs using SQL.
SQLAlchemy support
This module provides a SQLAlchemy dialect.
from sqlalchemy.engine import create_engine
engine_https = create_engine('graphql://host:port/path')
engine_http = create_engine('graphql://host:port/path?is_https=0')
engine_http = create_engine('graphql://:token@host:port/path')
Example Usage
Querying Connections
from sqlalchemy import create_engine
from sqlalchemy import text
engine = create_engine('graphql://swapi-graphql.netlify.app/.netlify/functions/index')
query = "select name, homeworld__name from 'allPeople?include=homeworld'"
with engine.connect() as connection:
for row in connection.execute(text(query)):
print(row)
Querying Lists
We can mark a given GQL query as being a List when we query that "Table" using a query parameter:
from sqlalchemy import create_engine
from sqlalchemy import text
engine = create_engine('graphql://pet-library.moonhighway.com/')
query = "select id, name from 'allPets?is_connection=0'"
with engine.connect() as connection:
for row in connection.execute(text(query)):
print(row)
alternatively, we can set that at the Engine
level:
from sqlalchemy import create_engine
from sqlalchemy import text
engine = create_engine(
'graphql://pet-library.moonhighway.com/',
list_queries=["allPets"],
)
query = "select id, name from allPets"
with engine.connect() as connection:
for row in connection.execute(text(query)):
print(row)
Superset support
In order to use with Superset, install this package and then use the graphql
protocol in the SQLAlchemy URI like: graphql://swapi-graphql.netlify.app/.netlify/functions/index
. We install a db_engine_spec
so Superset should recognize the driver.
Roadmap