diff --git a/docs/fields.rst b/docs/fields.rst new file mode 100644 index 000000000..1a8afc3fb --- /dev/null +++ b/docs/fields.rst @@ -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`'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` 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* diff --git a/docs/filtering.rst b/docs/filtering.rst index 0d37f460d..dbbab9d2e 100644 --- a/docs/filtering.rst +++ b/docs/filtering.rst @@ -1,7 +1,7 @@ Filtering ========= -Graphene integrates with +Graphene-Django integrates with `django-filter `__ (2.x for Python 3 or 1.x for Python 2) to provide filtering of results. See the `usage documentation `__ diff --git a/docs/index.rst b/docs/index.rst index 602f8dd1e..7f4f06589 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -25,6 +25,7 @@ For more advanced use, check out the Relay tutorial. tutorial-relay schema queries + fields mutations filtering authorization diff --git a/docs/queries.rst b/docs/queries.rst index 36cdab1fd..4b3f71802 100644 --- a/docs/queries.rst +++ b/docs/queries.rst @@ -1,3 +1,5 @@ +.. _queries-objecttypes: + Queries & ObjectTypes ===================== @@ -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 -----------------