|
| 1 | +Getting Started |
| 2 | +==== |
| 3 | + |
| 4 | +Welcome to the graphene-sqlalchemy documentation! |
| 5 | +Graphene is a powerful Python library for building GraphQL APIs, |
| 6 | +and SQLAlchemy is a popular ORM (Object-Relational Mapping) |
| 7 | +tool for working with databases. When combined, graphene-sqlalchemy |
| 8 | +allows developers to quickly and easily create a GraphQL API that |
| 9 | +seamlessly interacts with a SQLAlchemy-managed database. |
| 10 | +It is fully compatible with SQLAlchemy 1.4 and 2.0. |
| 11 | +This documentation provides detailed instructions on how to get |
| 12 | +started with graphene-sqlalchemy, including installation, setup, |
| 13 | +and usage examples. |
| 14 | + |
| 15 | +Installation |
| 16 | +------------ |
| 17 | + |
| 18 | +To install :code:`graphene-sqlalchemy`, just run this command in your shell: |
| 19 | + |
| 20 | +.. code:: bash |
| 21 | +
|
| 22 | + pip install --pre "graphene-sqlalchemy" |
| 23 | +
|
| 24 | +Examples |
| 25 | +-------- |
| 26 | + |
| 27 | +Here is a simple SQLAlchemy model: |
| 28 | + |
| 29 | +.. code:: python |
| 30 | +
|
| 31 | + from sqlalchemy import Column, Integer, String |
| 32 | + from sqlalchemy.ext.declarative import declarative_base |
| 33 | +
|
| 34 | + Base = declarative_base() |
| 35 | +
|
| 36 | + class UserModel(Base): |
| 37 | + __tablename__ = 'user' |
| 38 | + id = Column(Integer, primary_key=True) |
| 39 | + name = Column(String) |
| 40 | + last_name = Column(String) |
| 41 | +
|
| 42 | +To create a GraphQL schema for it, you simply have to write the |
| 43 | +following: |
| 44 | + |
| 45 | +.. code:: python |
| 46 | +
|
| 47 | + import graphene |
| 48 | + from graphene_sqlalchemy import SQLAlchemyObjectType |
| 49 | +
|
| 50 | + class User(SQLAlchemyObjectType): |
| 51 | + class Meta: |
| 52 | + model = UserModel |
| 53 | + # use `only_fields` to only expose specific fields ie "name" |
| 54 | + # only_fields = ("name",) |
| 55 | + # use `exclude_fields` to exclude specific fields ie "last_name" |
| 56 | + # exclude_fields = ("last_name",) |
| 57 | +
|
| 58 | + class Query(graphene.ObjectType): |
| 59 | + users = graphene.List(User) |
| 60 | +
|
| 61 | + def resolve_users(self, info): |
| 62 | + query = User.get_query(info) # SQLAlchemy query |
| 63 | + return query.all() |
| 64 | +
|
| 65 | + schema = graphene.Schema(query=Query) |
| 66 | +
|
| 67 | +Then you can simply query the schema: |
| 68 | + |
| 69 | +.. code:: python |
| 70 | +
|
| 71 | + query = ''' |
| 72 | + query { |
| 73 | + users { |
| 74 | + name, |
| 75 | + lastName |
| 76 | + } |
| 77 | + } |
| 78 | + ''' |
| 79 | + result = schema.execute(query, context_value={'session': db_session}) |
| 80 | +
|
| 81 | +
|
| 82 | +It is important to provide a session for graphene-sqlalchemy to resolve the models. |
| 83 | +In this example, it is provided using the GraphQL context. See :ref:`querying` for |
| 84 | +other ways to implement this. |
| 85 | + |
| 86 | +You may also subclass SQLAlchemyObjectType by providing |
| 87 | +``abstract = True`` in your subclasses Meta: |
| 88 | + |
| 89 | +.. code:: python |
| 90 | +
|
| 91 | + from graphene_sqlalchemy import SQLAlchemyObjectType |
| 92 | +
|
| 93 | + class ActiveSQLAlchemyObjectType(SQLAlchemyObjectType): |
| 94 | + class Meta: |
| 95 | + abstract = True |
| 96 | +
|
| 97 | + @classmethod |
| 98 | + def get_node(cls, info, id): |
| 99 | + return cls.get_query(info).filter( |
| 100 | + and_(cls._meta.model.deleted_at==None, |
| 101 | + cls._meta.model.id==id) |
| 102 | + ).first() |
| 103 | +
|
| 104 | + class User(ActiveSQLAlchemyObjectType): |
| 105 | + class Meta: |
| 106 | + model = UserModel |
| 107 | +
|
| 108 | + class Query(graphene.ObjectType): |
| 109 | + users = graphene.List(User) |
| 110 | +
|
| 111 | + def resolve_users(self, info): |
| 112 | + query = User.get_query(info) # SQLAlchemy query |
| 113 | + return query.all() |
| 114 | +
|
| 115 | + schema = graphene.Schema(query=Query) |
| 116 | +
|
| 117 | +More complex inhertiance using SQLAlchemy's polymorphic models is also supported. |
| 118 | +You can check out :doc:`inheritance` for a guide. |
0 commit comments