I'm seeing an issue with the cookbook-plain example. Here are the steps to reproduce:
git clone https://github.com/graphql-python/graphene-django.git
cd graphene-django/examples/cookbook-plain
virtualenv env
source env/bin/activate
pip install -r requirements.txt
./manage.py migrate
./manage.py loaddata ingredients
./manage.py runserver
open http://127.0.0.1:8000/graphql
Use the following query within GraphiQL browser:
request:
query {
allIngredients {
id
name
}
}
response:
{
"errors": [
{
"message": "resolve_all_ingredients() missing 2 required positional arguments: 'context' and 'info'",
"locations": [
{
"line": 2,
"column": 3
}
]
}
],
"data": {
"allIngredients": null
}
}
The tutorial code seems to be outdated. To make it work for you, in cookbook/ingredients/schema.py replace all function parameters that are available in this file with (self, context, **kwargs). For quick copy&paste use:
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(graphene.AbstractType):
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, context, **kwargs):
return Category.objects.all()
def resolve_all_ingredients(self, context, **kwargs):
# We can easily optimize query count in the resolve method
return Ingredient.objects.select_related('category').all()
def resolve_category(self, context, **kwargs):
id = args.get('id')
name = args.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, context, **kwargs):
id = args.get('id')
name = args.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
There are more issues with the examples as they basically feature version 1 of graphene-django and not version 2.
Another way to deal with this is to provide default values for the id and name arguments in resolve_ingredient like so:
def resolve_ingredient(self, context, id=None, name=None):
if id is not None:
return Ingredient.objects.get(pk=id)
if name is not None:
return Ingredient.objects.get(name=name)
return None
Most helpful comment
The tutorial code seems to be outdated. To make it work for you, in
cookbook/ingredients/schema.pyreplace all function parameters that are available in this file with(self, context, **kwargs). For quick copy&paste use:There are more issues with the examples as they basically feature version 1 of graphene-django and not version 2.