• Stars
    star
    971
  • Rank 45,425 (Top 1.0 %)
  • Language
    Python
  • License
    MIT License
  • Created over 7 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Graphene SQLAlchemy integration

Version 3.0 is in beta stage. Please read #348 to learn about progress and changes in upcoming beta releases.


Graphene Logo Graphene-SQLAlchemy

Build Status PyPI version GitHub release (latest by date including pre-releases) codecov

A SQLAlchemy integration for Graphene.

Installation

For installing Graphene, just run this command in your shell.

pip install --pre "graphene-sqlalchemy"

Examples

Here is a simple SQLAlchemy model:

from sqlalchemy import Column, Integer, String

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class UserModel(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    last_name = Column(String)

To create a GraphQL schema for it, you simply have to write the following:

import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType

class User(SQLAlchemyObjectType):
    class Meta:
        model = UserModel
        # use `only_fields` to only expose specific fields ie "name"
        # only_fields = ("name",)
        # use `exclude_fields` to exclude specific fields ie "last_name"
        # exclude_fields = ("last_name",)

class Query(graphene.ObjectType):
    users = graphene.List(User)

    def resolve_users(self, info):
        query = User.get_query(info)  # SQLAlchemy query
        return query.all()

schema = graphene.Schema(query=Query)

Then you can simply query the schema:

query = '''
    query {
      users {
        name,
        lastName
      }
    }
'''
result = schema.execute(query, context_value={'session': db_session})

You may also subclass SQLAlchemyObjectType by providing abstract = True in your subclasses Meta:

from graphene_sqlalchemy import SQLAlchemyObjectType

class ActiveSQLAlchemyObjectType(SQLAlchemyObjectType):
    class Meta:
        abstract = True

    @classmethod
    def get_node(cls, info, id):
        return cls.get_query(info).filter(
            and_(cls._meta.model.deleted_at==None,
                 cls._meta.model.id==id)
            ).first()

class User(ActiveSQLAlchemyObjectType):
    class Meta:
        model = UserModel

class Query(graphene.ObjectType):
    users = graphene.List(User)

    def resolve_users(self, info):
        query = User.get_query(info)  # SQLAlchemy query
        return query.all()

schema = graphene.Schema(query=Query)

Full Examples

To learn more check out the following examples:

Contributing

See CONTRIBUTING.md

More Repositories

1

graphene

GraphQL framework for Python
Python
7,978
star
2

graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
Python
4,235
star
3

gql

A GraphQL client in Python
Python
1,474
star
4

flask-graphql

Adds GraphQL support to your Flask application.
Python
1,316
star
5

graphql-core

A Python 3.6+ port of the GraphQL.js reference implementation of GraphQL.
Python
498
star
6

graphql-core-legacy

GraphQL base implementation for Python (legacy version – see graphql-core for the current one)
Python
375
star
7

graphene-mongo

Graphene MongoEngine integration
Python
285
star
8

graphql-ws

GraphQL websockets
Python
271
star
9

graphene-pydantic

Integrate GraphQL with your Pydantic models
Python
220
star
10

swapi-graphene

GraphQL Starwars API using Graphene and Django
Python
172
star
11

sanic-graphql

Adds GraphQL support to your Sanic app.
Python
149
star
12

graphql-relay-py

A library to help construct a graphql-py server supporting react-relay
Python
145
star
13

aiohttp-graphql

Adds GraphQL support to your aiohttp app.
Python
118
star
14

graphene-gae

GraphQL Support for Google AppEngine [DEPRECATED - Looking for maintainers]
Python
117
star
15

graphql-server

This is the core package for using GraphQL in a custom server easily
Python
114
star
16

gql-next

A Python GraphQL Client library providing ability to validate and make type-safe GraphQL calls
Python
76
star
17

graphene-tornado

Python
52
star
18

graphene-federation

Federation implementation for Graphene.
Python
40
star
19

webob-graphql

GraphQL integration for WebOb based frameworks: Pyramid, Pylons...
Python
28
star
20

GraphQL-SublimeText

GraphQL language syntax for SublimeText
17
star
21

graphene-python.org

Graphene-Python.org official website
JavaScript
6
star