Skip to main content

SQLAlchemy GraphQL

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

Graphene Support Coming Soon

Installation

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

pip install sqlalchemy-graphql

Usage

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()
esql.register(R)

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()


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


@R.ChildModel.CanBe
class ChildModel(Base):
    __tablename__ = "children"
    id = Column(Integer, primary_key=True)
    name = Column(String)
    parent_id = Column(Integer, ForeignKey("parents.id"), 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.

Examples

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:"{YOUR TARGET ATTRIBUTE}", op:"YOUR OPERATION")
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")
session.add(test_parent_1)
session.add(test_parent_2)
session.commit()

schema = R.Schema(R.Query)

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


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


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

'''results.data
{
    'parentModels': [
        {'name': 'Carolina', 'id': test_parent_2.id}
    ]
}
'''

Metadata

Release files for sqlalchemy_graphql 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlalchemy_graphql 1.3
File Size Uploaded Provenance Actions
sqlalchemy_graphql-1.3.tar.gz 6.0 kB Without provenance Details for sqlalchemy_graphql-1.3.tar.gz

Release files / sqlalchemy_graphql-1.3.tar.gz

Download URL sqlalchemy_graphql-1.3.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3f4b7836291c8ce9a3deee5a3901c8deaa8765cd641e53a96946c8f9df5257dc
BLAKE2b-256 checksum
How to use checksums
4586d607aad53447c7d42f764f341fa1a4e1d9f97103ad82c4bc207285c46883
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for sqlalchemy_graphql 1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page