You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorial-plain.rst
+3-60Lines changed: 3 additions & 60 deletions
Original file line number
Diff line number
Diff line change
@@ -417,67 +417,10 @@ Getting single objects
417
417
So far, we have been able to fetch list of objects and follow relation. But what about single objects?
418
418
419
419
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``
420
421
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
-
classCategoryType(DjangoObjectType):
431
-
classMeta:
432
-
model = Category
433
-
434
-
435
-
classIngredientType(DjangoObjectType):
436
-
classMeta:
437
-
model = Ingredient
438
-
439
-
440
-
classQuery(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
-
defresolve_all_categories(self, info, **kwargs):
453
-
return Category.objects.all()
454
-
455
-
defresolve_all_ingredients(self, info, **kwargs):
456
-
return Ingredient.objects.all()
457
-
458
-
defresolve_category(self, info, **kwargs):
459
-
id= kwargs.get('id')
460
-
name = kwargs.get('name')
461
-
462
-
ifidisnotNone:
463
-
return Category.objects.get(pk=id)
464
-
465
-
if name isnotNone:
466
-
return Category.objects.get(name=name)
467
-
468
-
returnNone
469
-
470
-
defresolve_ingredient(self, info, **kwargs):
471
-
id= kwargs.get('id')
472
-
name = kwargs.get('name')
473
-
474
-
ifidisnotNone:
475
-
return Ingredient.objects.get(pk=id)
476
-
477
-
if name isnotNone:
478
-
return Ingredient.objects.get(name=name)
479
-
480
-
returnNone
422
+
.. literalinclude:: schema.py
423
+
:emphasize-lines: 19-21,25-27,36-58
481
424
482
425
Now, with the code in place, we can query for single objects.
0 commit comments