Skip to content

Commit 04fe299

Browse files
changelingProjectCheshire
authored andcommitted
Corrected docs/queries.rst. (#633)
* Corrected typos in docs/queries.rst. * Add basic resolvers to Relay Full example in docs/queries.rst. Added basic resolvers to Full example in Relay section. * Remove question and question resolver. * Add query example to queries.rst. Added query example in Relay section. Minor clean-up.
1 parent 96908be commit 04fe299

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

docs/queries.rst

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Full example
3030
3131
class Query:
3232
questions = graphene.List(QuestionType)
33-
question = graphene.Field(Question, question_id=graphene.String())
33+
question = graphene.Field(QuestionType, question_id=graphene.String())
3434
3535
def resolve_questions(self, info, **kwargs):
3636
# Querying a list
@@ -243,6 +243,8 @@ There is one additional import and a single line of code needed to adopt this:
243243

244244
Full example
245245
~~~~~~~~~~~~
246+
See the `Relay documentation <https://docs.graphene-python.org/en/latest/relay/nodes/>`__ on
247+
the core graphene pages for more information on customizing the Relay experience.
246248

247249
.. code:: python
248250
@@ -254,7 +256,7 @@ Full example
254256
class QuestionType(DjangoObjectType):
255257
class Meta:
256258
model = Question
257-
interaces = (relay.Node,)
259+
interfaces = (relay.Node,)
258260
259261
260262
class QuestionConnection(relay.Connection):
@@ -263,8 +265,68 @@ Full example
263265
264266
265267
class Query:
266-
question = graphene.Field(QuestionType)
267268
questions = relay.ConnectionField(QuestionConnection)
268269
269-
See the `Relay documentation <https://docs.graphene-python.org/en/latest/relay/nodes/>`__ on
270-
the core graphene pages for more information on customing the Relay experience.
270+
def resolve_questions(root, info, **kwargs):
271+
return Question.objects.all()
272+
273+
274+
You can now execute queries like:
275+
276+
277+
.. code:: python
278+
279+
{
280+
questions (first: 2, after: "YXJyYXljb25uZWN0aW9uOjEwNQ==") {
281+
pageInfo {
282+
startCursor
283+
endCursor
284+
hasNextPage
285+
hasPreviousPage
286+
}
287+
edges {
288+
cursor
289+
node {
290+
id
291+
question_text
292+
}
293+
}
294+
}
295+
}
296+
297+
Which returns:
298+
299+
.. code:: python
300+
301+
{
302+
"data": {
303+
"questions": {
304+
"pageInfo": {
305+
"startCursor": "YXJyYXljb25uZWN0aW9uOjEwNg==",
306+
"endCursor": "YXJyYXljb25uZWN0aW9uOjEwNw==",
307+
"hasNextPage": true,
308+
"hasPreviousPage": false
309+
},
310+
"edges": [
311+
{
312+
"cursor": "YXJyYXljb25uZWN0aW9uOjEwNg==",
313+
"node": {
314+
"id": "UGxhY2VUeXBlOjEwNw==",
315+
"question_text": "How did we get here?"
316+
}
317+
},
318+
{
319+
"cursor": "YXJyYXljb25uZWN0aW9uOjEwNw==",
320+
"node": {
321+
"id": "UGxhY2VUeXBlOjEwOA==",
322+
"name": "Where are we?"
323+
}
324+
}
325+
]
326+
}
327+
}
328+
}
329+
330+
Note that relay implements :code:`pagination` capabilities automatically, adding a :code:`pageInfo` element, and including :code:`cursor` on nodes. These elements are included in the above example for illustration.
331+
332+
To learn more about Pagination in general, take a look at `Pagination <https://graphql.org/learn/pagination/>`__ on the GraphQL community site.

0 commit comments

Comments
 (0)