Skip to content

Commit e51e602

Browse files
athulProjectCheshire
authored andcommitted
Updated Tutorial with Highlights (#801)
1 parent def6b15 commit e51e602

File tree

2 files changed

+61
-60
lines changed

2 files changed

+61
-60
lines changed

docs/schema.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import graphene
2+
3+
from graphene_django.types import DjangoObjectType
4+
5+
from cookbook.ingredients.models import Category, Ingredient
6+
7+
8+
class CategoryType(DjangoObjectType):
9+
class Meta:
10+
model = Category
11+
12+
13+
class IngredientType(DjangoObjectType):
14+
class Meta:
15+
model = Ingredient
16+
17+
18+
class Query(object):
19+
category = graphene.Field(CategoryType,
20+
id=graphene.Int(),
21+
name=graphene.String())
22+
all_categories = graphene.List(CategoryType)
23+
24+
25+
ingredient = graphene.Field(IngredientType,
26+
id=graphene.Int(),
27+
name=graphene.String())
28+
all_ingredients = graphene.List(IngredientType)
29+
30+
def resolve_all_categories(self, info, **kwargs):
31+
return Category.objects.all()
32+
33+
def resolve_all_ingredients(self, info, **kwargs):
34+
return Ingredient.objects.all()
35+
36+
def resolve_category(self, info, **kwargs):
37+
id = kwargs.get('id')
38+
name = kwargs.get('name')
39+
40+
if id is not None:
41+
return Category.objects.get(pk=id)
42+
43+
if name is not None:
44+
return Category.objects.get(name=name)
45+
46+
return None
47+
48+
def resolve_ingredient(self, info, **kwargs):
49+
id = kwargs.get('id')
50+
name = kwargs.get('name')
51+
52+
if id is not None:
53+
return Ingredient.objects.get(pk=id)
54+
55+
if name is not None:
56+
return Ingredient.objects.get(name=name)
57+
58+
return None

docs/tutorial-plain.rst

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -417,67 +417,10 @@ Getting single objects
417417
So far, we have been able to fetch list of objects and follow relation. But what about single objects?
418418

419419
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.
420+
Add the **Highlighted** lines to ``cookbook/ingredients/schema.py``
420421

421-
.. code:: python
422-
423-
import graphene
424-
425-
from graphene_django.types import DjangoObjectType
426-
427-
from cookbook.ingredients.models import Category, Ingredient
428-
429-
430-
class CategoryType(DjangoObjectType):
431-
class Meta:
432-
model = Category
433-
434-
435-
class IngredientType(DjangoObjectType):
436-
class Meta:
437-
model = Ingredient
438-
439-
440-
class Query(object):
441-
category = graphene.Field(CategoryType,
442-
id=graphene.Int(),
443-
name=graphene.String())
444-
all_categories = graphene.List(CategoryType)
445-
446-
447-
ingredient = graphene.Field(IngredientType,
448-
id=graphene.Int(),
449-
name=graphene.String())
450-
all_ingredients = graphene.List(IngredientType)
451-
452-
def resolve_all_categories(self, info, **kwargs):
453-
return Category.objects.all()
454-
455-
def resolve_all_ingredients(self, info, **kwargs):
456-
return Ingredient.objects.all()
457-
458-
def resolve_category(self, info, **kwargs):
459-
id = kwargs.get('id')
460-
name = kwargs.get('name')
461-
462-
if id is not None:
463-
return Category.objects.get(pk=id)
464-
465-
if name is not None:
466-
return Category.objects.get(name=name)
467-
468-
return None
469-
470-
def resolve_ingredient(self, info, **kwargs):
471-
id = kwargs.get('id')
472-
name = kwargs.get('name')
473-
474-
if id is not None:
475-
return Ingredient.objects.get(pk=id)
476-
477-
if name is not None:
478-
return Ingredient.objects.get(name=name)
479-
480-
return None
422+
.. literalinclude:: schema.py
423+
:emphasize-lines: 19-21,25-27,36-58
481424

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

0 commit comments

Comments
 (0)