I'm currently learning GraphQL via the tutorial on graphene, however I got this error message when I tried to run:
python3 manage.py graphql_schema
Error:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 382, in run_from_argv
parser = self.create_parser(argv[0], argv[1])
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/base.py", line 355, in create_parser
self.add_arguments(parser)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/management/commands/graphql_schema.py", line 39, in add_arguments
default=graphene_settings.SCHEMA,
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/settings.py", line 110, in __getattr__
val = perform_import(val, attr)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/settings.py", line 54, in perform_import
return import_from_string(val, setting_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/settings.py", line 68, in import_from_string
module = importlib.import_module(module_path)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/lebeier/Documents/cookbook/schema.py", line 3, in <module>
import ingredients.schema
File "/Users/lebeier/Documents/cookbook/ingredients/schema.py", line 33, in <module>
class Query(AbstractType):
File "/Users/lebeier/Documents/cookbook/ingredients/schema.py", line 38, in Query
all_ingredients = DjangoFilterConnectionField(IngredientNode)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/fields.py", line 20, in __init__
self.filterset_class = get_filterset_class(filterset_class, **meta)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/utils.py", line 32, in get_filterset_class
return custom_filterset_factory(**meta)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/filterset.py", line 127, in custom_filterset_factory
'Meta': meta_class
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/graphene_django/filter/filterset.py", line 55, in __new__
new_class = super(GrapheneFilterSetMetaclass, cls).__new__(cls, name, bases, attrs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django_filters/filterset.py", line 224, in __new__
filters[order_by_field] = new_class.get_ordering_filter(opts, filters)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django_filters/filterset.py", line 375, in get_ordering_filter
"'order_by' is not compatible with the 'fields' dict syntax. Use OrderingFilter instead."
AssertionError: 'order_by' is not compatible with the 'fields' dict syntax. Use OrderingFilter instead.
In my ingredient/schema.py:
# cookbook/ingredients/schema.py
from graphene import relay, ObjectType, AbstractType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Category, Ingredient
# Graphene will automatically map the Category model's fields onto the CategoryNode.
# This is configured in the CategoryNode's Meta class (as you can see below)
class CategoryNode(DjangoObjectType):
class Meta:
model = Category
filter_fields = ['name', 'ingredients']
filter_order_by_field = ['name']
interfaces = (relay.Node, )
class IngredientNode(DjangoObjectType):
class Meta:
model = Ingredient
# Allow for some more advanced filtering here
filter_fields = {
'name': ['exact', 'icontains', 'istartswith'],
'notes': ['exact', 'icontains'],
'category': ['exact'],
'category__name': ['exact'],
}
order_by_field = ['name', 'category__name']
interfaces = (relay.Node, )
class Query(AbstractType):
category = relay.Node.Field(CategoryNode)
all_categories = DjangoFilterConnectionField(CategoryNode)
ingredient = relay.Node.Field(IngredientNode)
all_ingredients = DjangoFilterConnectionField(IngredientNode)
django-filter version 0.15.3
I have tried to clone the example from the official graphene repo and it worked fine on python2.7. However in python3.5, the same code failed to work. I am suspecting that something has changed in django-filter that resulted in incompatibility. In python2.7, django-filter version is 0.11.0. Does anyone know how to make it work in 0.15.3?
This was a reported as an issue on github and is now fixed: https://github.com/graphql-python/graphene-django/issues/8
Upgrade to graphene 1.1 and you should be good to go.