Skip to content

Commit

Permalink
Add fields document for DjangoListField
Browse files Browse the repository at this point in the history
  • Loading branch information
jkimbo committed Apr 12, 2020
1 parent a27e8f3 commit 08c48aa
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
83 changes: 83 additions & 0 deletions docs/fields.rst
Original file line number Diff line number Diff line change
@@ -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*
2 changes: 1 addition & 1 deletion docs/filtering.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Filtering
=========

Graphene integrates with
Graphene-Django integrates with
`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
documentation <https://django-filter.readthedocs.io/en/master/guide/usage.html#the-filter>`__
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ For more advanced use, check out the Relay tutorial.
tutorial-relay
schema
queries
fields
mutations
filtering
authorization
Expand Down
4 changes: 4 additions & 0 deletions docs/queries.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _queries-objecttypes:

Queries & ObjectTypes
=====================

Expand Down Expand Up @@ -205,6 +207,8 @@ need to create the most basic class for this to work:
class Meta:
model = Category
.. _django-objecttype-get-queryset:

Default QuerySet
-----------------

Expand Down

0 comments on commit 08c48aa

Please sign in to comment.