Skip to content

Commit 686613d

Browse files
committed
docs: extend docs and add autodoc api docs
1 parent 185a662 commit 686613d

File tree

6 files changed

+171
-3
lines changed

6 files changed

+171
-3
lines changed

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
API Reference
2+
====
3+
4+
.. automodule::graphene_sqlalchemy

docs/index.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Contents:
66
.. toctree::
77
:maxdepth: 0
88

9-
tutorial
9+
starter
10+
inheritance
1011
tips
1112
examples
13+
tutorial
14+
api

docs/inheritance.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Inheritance Examples
33

44
Create interfaces from inheritance relationships
55
------------------------------------------------
6-
.. note:: If you're using `AsyncSession`, please check the chapter `Eager Loading & Using with AsyncSession`_.
6+
.. note:: If you're using `AsyncSession`, please check the section `Eager Loading & Using with AsyncSession`_.
77
SQLAlchemy has excellent support for class inheritance hierarchies.
88
These hierarchies can be represented in your GraphQL schema by means
99
of interfaces_. Much like ObjectTypes, Interfaces in

docs/relay.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Relay
2+
====
3+
4+
:code:`graphene-sqlalchemy` comes with pre-defined
5+
connection fields to quickly create a functioning relay API.
6+
Using the :code:`SQLAlchemyConnectionField`, you have access to relay pagination,
7+
sorting and filtering (filtering is coming soon!).
8+
9+
To be used in a relay connection, your :code:`SQLAlchemyObjectType` must implement
10+
the :code:`Node` interface from :code:`graphene.relay`. This handles the creation of
11+
the :code:`Connection` and :code:`Edge` types automatically.
12+
13+
The following example creates a relay-paginated connection:
14+
15+
16+
17+
.. code:: python
18+
19+
class Pet(Base):
20+
__tablename__ = 'pets'
21+
id = Column(Integer(), primary_key=True)
22+
name = Column(String(30))
23+
pet_kind = Column(Enum('cat', 'dog', name='pet_kind'), nullable=False)
24+
25+
26+
class PetNode(SQLAlchemyObjectType):
27+
class Meta:
28+
model = Pet
29+
interfaces=(Node,)
30+
31+
32+
class Query(ObjectType):
33+
all_pets = SQLAlchemyConnectionField(PetNode.connection)
34+
35+
To disable sorting on the connection, you can set :code:`sort` to :code:`None` the
36+
:code:`SQLAlchemyConnectionField`:
37+
38+
39+
.. code:: python
40+
41+
class Query(ObjectType):
42+
all_pets = SQLAlchemyConnectionField(PetNode.connection, sort=None)
43+

docs/starter.rst

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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.

docs/tips.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tips
44

55
Querying
66
--------
7-
7+
.. _querying:
88
In order to make querying against the database work, there are two alternatives:
99

1010
- Set the db session when you do the execution:

0 commit comments

Comments
 (0)