Graphene SQLAlchemy Integration

2.3.0 · active · verified Mon Apr 13

Graphene SQLAlchemy integration allows developers to quickly and easily create a GraphQL API that seamlessly interacts with a SQLAlchemy-managed database. It is fully compatible with SQLAlchemy 1.4 and 2.0. The current stable version is 2.3.0, but version 3.0 is in release candidate stage with significant updates, and the project has an active development cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a SQLAlchemy model, create a `SQLAlchemyObjectType` from it, and then build a Graphene schema that can query the data. It's crucial to provide an active SQLAlchemy session to the GraphQL execution context for resolvers to function correctly.

import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# 1. Define SQLAlchemy Model
Base = declarative_base()

class UserModel(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    email = Column(String, unique=True)

    def __repr__(self):
        return f"<User(name='{self.name}', email='{self.email}')>"

# 2. Configure Database Session
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

# Add some initial data
user1 = UserModel(name='Alice', email='alice@example.com')
user2 = UserModel(name='Bob', email='bob@example.com')
session.add_all([user1, user2])
session.commit()

# 3. Create Graphene Object Type from SQLAlchemy Model
class User(SQLAlchemyObjectType):
    class Meta:
        model = UserModel
        interfaces = (graphene.relay.Node,)
        # Optionally expose specific fields or exclude some
        # only_fields = ("name", "email")
        # exclude_fields = ("id",)

# 4. Define Query Type
class Query(graphene.ObjectType):
    node = graphene.relay.Node.Field()
    all_users = graphene.List(User)
    user_by_name = graphene.Field(User, name=graphene.String(required=True))

    def resolve_all_users(self, info):
        # Graphene-SQLAlchemy provides get_query to construct a query
        # for the model associated with the SQLAlchemyObjectType
        query = User.get_query(info)
        return query.all()

    def resolve_user_by_name(self, info, name):
        query = User.get_query(info)
        return query.filter(UserModel.name == name).first()

# 5. Create Schema
schema = graphene.Schema(query=Query)

# 6. Execute a Query
query = '''
    query {
        allUsers {
            name
            email
        }
        userByName(name: "Alice") {
            name
            email
        }
    }
'''
result = schema.execute(query, context_value={'session': session})

print(result.data)
# Expected output might look like:
# {'allUsers': [{'name': 'Alice', 'email': 'alice@example.com'}, {'name': 'Bob', 'email': 'bob@example.com'}], 'userByName': {'name': 'Alice', 'email': 'alice@example.com'}}

view raw JSON →