Skip to main content

GraphQL extension for dealing with SQLAlchemy

Project description

SQLAlchemy GraphQL is a plugin for GraphQL Epoxy that provides universal functions for SQLAlchemy models being used for GraphQL

Graphene Support Coming Soon


SQLAlchemy Sphinx is available on pypi under the package name sqlalchemy-graphql, you can get it by running:

pip install sqlalchemy-graphql


The first step is registering your type registry. This adds all the features into your registry without you having to do any work. It leverages GraphQL aliasing.

from epoxy import TypeRegistry

from sqlalchemy_graphql.epoxy import EpoxySQLAlchemy

R = TypeRegistry()
esql = EpoxySQLAlchemy()

Once this is done, your registry now has a new Interface called FuncBase, which will be the one of the interfaces all your graphql models will use.

Here we’ll define the SQLAlchemy models using epoxy’s Registry decorators.

from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref, sessionmaker

from youe_application import R
engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()

class ParentModel(Base):
    __tablename__ = "parents"
    id = Column(Integer, primary_key=True)
    name = Column(String)

class ChildModel(Base):
    __tablename__ = "children"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    parent_id = Column(Integer, ForeignKey(""), nullable=False)
    parent = relationship("ParentModel", backref=backref("children", lazy="dynamic", cascade="all,delete-orphan"))

And finally defining the GraphQL models.

The first things that need to be done is during any relational, or sqlalchemy model query, we need to include the esql.quey_args. I created a global varaible below, but you can add them however you wish.

from your_application import R, esql
from your_application.your_sqlalchemy_models import ChildModel as BaseChildModel, ParentModel as BaseParentModel, session

from sqlalchemy_graphql.epoxy.utils import add_query_args
from sqlalchemy_graphql.epoxy.query import resolve_sqlalchemy

model_args = add_query_args({"id": R.Int, "name": R.String, "ids": R.Int.List}, esql.query_args)

class ParentModel(R.Implements.FuncBase):
    id = R.Int
    name = R.String
    children = R.ChildModel.List(args=model_args)

    def resolve_children(self, obj, args, info):
        return resolve_sqlalchemy(obj, args, info, BaseChildModel, query=obj.children)

class ChildModel(R.Implements.FuncBase):
    id = R.Int
    name = R.String
    parent = R.ParentModel(args=model_args)

    def resolve_parent(self, obj, args, info):
        return resolve_sqlalchemy(obj, args, info, BaseParentModel, query=obj.parent)

class Query(R.ObjectType):
    parent_model = R.ParentModel(args=model_args)
    child_model = R.ChildModel(args=model_args)
    parent_models = R.ParentModel.List(args=model_args)
    child_models = R.ChildModel.List(args=model_args)

    def resolve_parent_model(self, obj, args, info):
        query = session.query(BaseParentModel)
        return resolve_sqlalchemy(obj, args, info, BaseParentModel, query=query, single=True)

    def resolve_child_model(self, obj, args, info):
        query = session.query(BaseChildModel)
        return resolve_sqlalchemy(obj, args, info, BaseChildModel, query=query, single=True)

    def resolve_parent_models(self, obj, args, info):
        query = session.query(BaseParentModel)
        return resolve_sqlalchemy(obj, args, info, BaseParentModel, query=query)

    def resolve_child_models(self, obj, args, info):
        query = session.query(BaseChildModel)
        return resolve_sqlalchemy(obj, args, info, BaseChildModel)

You’ll notice in the resolves for the Query, we’re using the helper function resolve_sqlalchemy, proided by sqlalchemy-graphql to resolve any query arguements that are going to be used.

All that needs to be passed in is a Base query that has the SQLAlchemy model as the first argument, and you’re good to go.


The tests provided has a ton of examples, but here is some basic queries you can now do with your universal func.

You can essentially do anything that the SQLAlchemy func offers you to do

The basic formating is:

func(field:"id", op:"min")}
func(field:"id", op:"max")}
func(field:"count", op:"sum")}
test_parent_1 = ParentModel(name="Adriel")
test_parent_2 = ParentModel(name="Carolina")

schema = R.Schema(R.Query)

query = '{parentModel {idSum: func(field:"id", op:"sum")}}'
results = graphql(schema, query)
value = +
assert['parentModel']['idSum'] == value

query = '{parentModels {distinctName: count(distinct:"name")}}'
results = graphql(schema, query)
# == {'parentModels': [{'distinctName': 2}]}

query = '{parentModels (first: 1, after:"Adriel", order:["name"]){id, name}}'
results = graphql(schema, query)

    'parentModels': [
        {'name': 'Carolina', 'id':}

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

sqlalchemy_graphql-1.3.tar.gz (6.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page