Add fields document for DjangoListField

This commit is contained in:
Jonathan Kim 2020-04-12 14:59:52 +01:00
parent 559a245770
commit 68462b7f62
4 changed files with 89 additions and 1 deletions

83
docs/fields.rst Normal file
View File

@ -0,0 +1,83 @@
Fields
======
Graphene-Django provides some useful fields to help integrate Django with your GraphQL
Schema.
DjangoListField
---------------
``DjangoListField`` allows you to define a list of :ref:`DjangoObjectType<queries-objecttypes>`'s. By default it will resolve the default queryset of the Django model.
.. code:: python
from graphene import ObjectType, Schema
from graphene_django import DjangoListField
class RecipeType(DjangoObjectType):
class Meta:
model = Recipe
fields = ("title", "instructions")
class Query(ObjectType):
recipes = DjangoListField(RecipeType)
schema = Schema(query=Query)
The above code results in the following schema definition:
.. code::
schema {
query: Query
}
type Query {
recipes: [RecipeType!]
}
type RecipeType {
title: String!
instructions: String!
}
Custom resolvers
****************
If your ``DjangoObjectType`` has defined a custom
:ref:`get_queryset<django-objecttype-get-queryset>` method, when resolving a
``DjangoListField`` it will be called with either the return of the field
resolver (if one is defined) or the default queryeset from the Django model.
For example the following schema will only resolve recipes which have been
published and have a title:
.. code:: python
from graphene import ObjectType, Schema
from graphene_django import DjangoListField
class RecipeType(DjangoObjectType):
class Meta:
model = Recipe
fields = ("title", "instructions")
@classmethod
def get_queryset(cls, queryset, info):
# Filter out recipes that have no title
return queryset.exclude(title__exact="")
class Query(ObjectType):
recipes = DjangoListField(RecipeType)
def resolve_recipes(parent, info):
# Only get recipes that have been published
return Recipe.objects.filter(published=True)
schema = Schema(query=Query)
DjangoConnectionField
---------------------
*TODO*

View File

@ -1,7 +1,7 @@
Filtering Filtering
========= =========
Graphene integrates with Graphene-Django integrates with
`django-filter <https://django-filter.readthedocs.io/en/master/>`__ (2.x for `django-filter <https://django-filter.readthedocs.io/en/master/>`__ (2.x for
Python 3 or 1.x for Python 2) to provide filtering of results. See the `usage Python 3 or 1.x for Python 2) to provide filtering of results. See the `usage
documentation <https://django-filter.readthedocs.io/en/master/guide/usage.html#the-filter>`__ documentation <https://django-filter.readthedocs.io/en/master/guide/usage.html#the-filter>`__

View File

@ -25,6 +25,7 @@ For more advanced use, check out the Relay tutorial.
tutorial-relay tutorial-relay
schema schema
queries queries
fields
mutations mutations
filtering filtering
authorization authorization

View File

@ -1,3 +1,5 @@
.. _queries-objecttypes:
Queries & ObjectTypes Queries & ObjectTypes
===================== =====================
@ -205,6 +207,8 @@ need to create the most basic class for this to work:
class Meta: class Meta:
model = Category model = Category
.. _django-objecttype-get-queryset:
Default QuerySet Default QuerySet
----------------- -----------------