mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-12 17:22:20 +03:00
Add fields document for DjangoListField
This commit is contained in:
parent
559a245770
commit
68462b7f62
83
docs/fields.rst
Normal file
83
docs/fields.rst
Normal 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*
|
|
@ -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>`__
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user