Skip to content

Updated Basic-Tutorial with Highlights ✨✨✨ #801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import graphene

from graphene_django.types import DjangoObjectType

from cookbook.ingredients.models import Category, Ingredient


class CategoryType(DjangoObjectType):
class Meta:
model = Category


class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient


class Query(object):
category = graphene.Field(CategoryType,
id=graphene.Int(),
name=graphene.String())
all_categories = graphene.List(CategoryType)


ingredient = graphene.Field(IngredientType,
id=graphene.Int(),
name=graphene.String())
all_ingredients = graphene.List(IngredientType)

def resolve_all_categories(self, info, **kwargs):
return Category.objects.all()

def resolve_all_ingredients(self, info, **kwargs):
return Ingredient.objects.all()

def resolve_category(self, info, **kwargs):
id = kwargs.get('id')
name = kwargs.get('name')

if id is not None:
return Category.objects.get(pk=id)

if name is not None:
return Category.objects.get(name=name)

return None

def resolve_ingredient(self, info, **kwargs):
id = kwargs.get('id')
name = kwargs.get('name')

if id is not None:
return Ingredient.objects.get(pk=id)

if name is not None:
return Ingredient.objects.get(name=name)

return None
63 changes: 3 additions & 60 deletions docs/tutorial-plain.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,67 +417,10 @@ Getting single objects
So far, we have been able to fetch list of objects and follow relation. But what about single objects?

We can update our schema to support that, by adding new query for ``ingredient`` and ``category`` and adding arguments, so we can query for specific objects.
Add the **Highlighted** lines to ``cookbook/ingredients/schema.py``

.. code:: python

import graphene

from graphene_django.types import DjangoObjectType

from cookbook.ingredients.models import Category, Ingredient


class CategoryType(DjangoObjectType):
class Meta:
model = Category


class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient


class Query(object):
category = graphene.Field(CategoryType,
id=graphene.Int(),
name=graphene.String())
all_categories = graphene.List(CategoryType)


ingredient = graphene.Field(IngredientType,
id=graphene.Int(),
name=graphene.String())
all_ingredients = graphene.List(IngredientType)

def resolve_all_categories(self, info, **kwargs):
return Category.objects.all()

def resolve_all_ingredients(self, info, **kwargs):
return Ingredient.objects.all()

def resolve_category(self, info, **kwargs):
id = kwargs.get('id')
name = kwargs.get('name')

if id is not None:
return Category.objects.get(pk=id)

if name is not None:
return Category.objects.get(name=name)

return None

def resolve_ingredient(self, info, **kwargs):
id = kwargs.get('id')
name = kwargs.get('name')

if id is not None:
return Ingredient.objects.get(pk=id)

if name is not None:
return Ingredient.objects.get(name=name)

return None
.. literalinclude:: schema.py
:emphasize-lines: 19-21,25-27,36-58

Now, with the code in place, we can query for single objects.

Expand Down