Skip to content

Commit

Permalink
Create QueryDoc.rst (#56)
Browse files Browse the repository at this point in the history
Added docs for query
  • Loading branch information
ccsv authored Aug 23, 2022
1 parent 2babb8f commit 8d6fb33
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Docs/QueryDoc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Query
-----

Queries require specifying the GraphQL schema and a Query type. For
example Lets have a model ``Document`` with a name field:

.. code:: python
# models.py
from django.db import models
class Document(models.Model):
name = models.CharField(max_length=50, null=False, blank=False)
def __str__(self):
return self.name
For the Query, we define a GraphQL schema and a Query type in
``type_defs`` where they Query contains the name of the Query
``getDocument`` with the input fields (id and name) followed by the
format of the return object ``[Document]``, which returns a list of
Documents. The return object can also be customized by adding an
additional schema type for tasks such as handling returning error
issues.

.. code:: python
from ariadne import gql, QueryType
from ariadne import make_executable_schema
from core.schema import type_defs, query
from django.db.models import Q
type_defs = gql("""
type Document {
id:ID
name: String!
}
type Query {
getDocument(name:String, id:ID): [Document]
}
""")
query = QueryType()
@query.field("getDocument")
def resolve_getDocument(*_, name=None, id=None,):
if name:
filter = Q(name__icontains=name)
return Document.objects.filter(filter)
if id:
filter = Q(id__exact=id)
return Document.objects.filter(filter)
return Document.objects.all()
schema = make_executable_schema(type_defs, query)
| The resolver is a function beginning with ``resolve_`` followed by the
name of the query and the input fields.
| In this example, a filter is applied when name or id is present where
the results will return the Document with the exact id entered or a
list of ``Documents`` containing the entry in ``name``. If no values
are entered, the query will get all objects. The filters are based on
the Django Field lookups.
To modify the ordering of the results, the Query type and the queries in
the resolver can be modified where you can enter ``"name"`` for
ascending order by name and ``"-name"`` for descending.

.. code:: python
type_defs = gql("""
type Document {
id:ID
name: String!
}
type Query {
getDocument(name:String, id:ID, order:String): [Document]
}
""")
query = QueryType()
@query.field("getDocument")
def resolve_getDocument(*_, name=None, id=None, order=None):
if name:
filter = Q(name__icontains=name)
return Document.objects.filter(filter).order_by(order)
if id:
filter = Q(id__exact=id)
return Document.objects.filter(filter).order_by(order)
return Document.objects.all().order_by(order)

0 comments on commit 8d6fb33

Please sign in to comment.