-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added docs for query
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |