Skip to content

Commit 8b7c124

Browse files
committed
Add some documentation on sorting
1 parent 9dd9fdf commit 8b7c124

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

docs/tips.rst

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
Tips
33
====
44

5-
Tips
6-
====
7-
85
Querying
96
--------
107

@@ -30,3 +27,61 @@ For make querying to the database work, there are two alternatives:
3027
If you don't specify any, the following error will be displayed:
3128

3229
``A query in the model Base or a session in the schema is required for querying.``
30+
31+
Sorting
32+
-------
33+
34+
By default the SQLAlchemyConnectionField sorts the result elements over the primary key(s).
35+
The query has a `sort` argument which allows to sort over a different column(s)
36+
37+
Given the model
38+
39+
.. code:: python
40+
41+
class Pet(Base):
42+
__tablename__ = 'pets'
43+
id = Column(Integer(), primary_key=True)
44+
name = Column(String(30))
45+
pet_kind = Column(Enum('cat', 'dog', name='pet_kind'), nullable=False)
46+
47+
48+
class PetNode(SQLAlchemyObjectType):
49+
class Meta:
50+
model = Pet
51+
52+
53+
class PetConnection(Connection):
54+
class Meta:
55+
node = PetNone
56+
57+
58+
class Query(ObjectType):
59+
allPets = SQLAlchemyConnectionField(PetConnection)
60+
61+
some of the allowed queries are
62+
63+
- Sort in ascending order over the `name` column
64+
65+
.. code::
66+
67+
allPets(sort: name_asc){
68+
edges {
69+
node {
70+
name
71+
}
72+
}
73+
}
74+
75+
- Sort in descending order over the `per_kind` column and in ascending order over the `name` column
76+
77+
.. code::
78+
79+
allPets(sort: [pet_kind_desc, name_asc]) {
80+
edges {
81+
node {
82+
name
83+
petKind
84+
}
85+
}
86+
}
87+

0 commit comments

Comments
 (0)