Issue when trying to create use a Union type inside a converter
See original GitHub issueI was trying to write a converter function for Wagtail’s Stream Field and I run into the following error:
Exception Type: ImportError at /graphql
Exception Value: Could not import 'mysite.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: 'GraphQLScalarType' object has no attribute 'graphene_type'.
Full Traceback is at the bottom. Here’s the code that throws the error:
from wagtail.wagtailcore.fields import StreamField
from graphene.types import Union, String
from graphene_django.converter import convert_django_field
class StreamFieldTypesUnion(Union):
class Meta:
types = (String,)
@convert_django_field.register(StreamField)
def convert_stream_field(field, registry=None):
return StreamFieldTypesUnion()
from what I’ve understood the error happens when the schema is being created, it is trying to get the GrapheneType here: https://github.com/graphql-python/graphene/blob/master/graphene/types/typemap.py#L213
but the type not defined yet (in this case String
).
Is there anyway to prevent this error? I’ve tried with Dynamic
without luck.
Full Traceback:
Environment:
Request Method: GET
Request URL: http://localhost:8000/graphql
Django Version: 1.11.5
Python Version: 3.6.0
Installed Applications:
['home',
'search',
'wagtail.wagtailforms',
'wagtail.wagtailredirects',
'wagtail.wagtailembeds',
'wagtail.wagtailsites',
'wagtail.wagtailusers',
'wagtail.wagtailsnippets',
'wagtail.wagtaildocs',
'wagtail.wagtailimages',
'wagtail.wagtailsearch',
'wagtail.wagtailadmin',
'wagtail.wagtailcore',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'graphene_django',
'graphene_utils',
'django_extensions']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware']
Traceback:
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene_django/settings.py" in import_from_string
74. module = importlib.import_module(module_path)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/importlib/__init__.py" in import_module
126. return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/patrick/Documents/Github/patrick91/wagtail-ql/backend/mysite/schema.py" in <module>
15. schema = graphene.Schema(query=Query)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/schema.py" in __init__
44. self.build_typemap()
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/schema.py" in build_typemap
105. self._type_map = TypeMap(initial_types, auto_camelcase=self.auto_camelcase, schema=self)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in __init__
66. super(TypeMap, self).__init__(types)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/typemap.py" in __init__
16. self.update(reduce(self.reducer, types, OrderedDict()))
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in reducer
74. return self.graphene_reducer(map, type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in graphene_reducer
105. return GraphQLTypeMap.reducer(map, internal_type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/typemap.py" in reducer
79. field_map = type.fields
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/pyutils/cached_property.py" in __get__
16. value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/definition.py" in fields
180. return define_field_map(self, self._fields)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/definition.py" in define_field_map
189. field_map = field_map()
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in construct_fields_for_type
237. map = self.reducer(map, field.type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in reducer
74. return self.graphene_reducer(map, type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in graphene_reducer
80. return self.reducer(map, type.of_type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in reducer
74. return self.graphene_reducer(map, type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in graphene_reducer
105. return GraphQLTypeMap.reducer(map, internal_type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/typemap.py" in reducer
79. field_map = type.fields
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/pyutils/cached_property.py" in __get__
16. value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/definition.py" in fields
180. return define_field_map(self, self._fields)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/definition.py" in define_field_map
189. field_map = field_map()
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in construct_fields_for_type
237. map = self.reducer(map, field.type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in reducer
74. return self.graphene_reducer(map, type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in graphene_reducer
105. return GraphQLTypeMap.reducer(map, internal_type)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/typemap.py" in reducer
71. for t in type.types:
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/pyutils/cached_property.py" in __get__
16. value = obj.__dict__[self.func.__name__] = self.func(obj)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/definition.py" in types
354. return define_types(self, self._types)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphql/type/definition.py" in define_types
359. types = types()
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene/types/typemap.py" in types
215. assert internal_type.graphene_type == objecttype
During handling of the above exception ('GraphQLScalarType' object has no attribute 'graphene_type'), another exception occurred:
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/django/views/generic/base.py" in view
62. self = cls(**initkwargs)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene_django/views.py" in __init__
70. schema = graphene_settings.SCHEMA
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene_django/settings.py" in __getattr__
116. val = perform_import(val, attr)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene_django/settings.py" in perform_import
60. return import_from_string(val, setting_name)
File "/Users/patrick/.virtualenvs/wagtail-ql/lib/python3.6/site-packages/graphene_django/settings.py" in import_from_string
78. raise ImportError(msg)
Exception Type: ImportError at /graphql
Exception Value: Could not import 'mysite.schema.schema' for Graphene setting 'SCHEMA'. AttributeError: 'GraphQLScalarType' object has no attribute 'graphene_type'.
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (11 by maintainers)
Top Results From Across the Web
Why do union types break like this? - Stack Overflow
When using a type alias, typescript complains about usage of a property that exists in one the types, even after explicitly stating which...
Read more >Handbook - Unions and Intersection Types - TypeScript
How to use unions and intersection types in TypeScript. ... for tools which let you compose or combine existing types instead of creating...
Read more >TypeScript discriminated union and intersection types
To convert a union type into a discriminated union type, we use a common property across our types. This property can be any...
Read more >React state meets Typescript's Union type - Level Up Coding
If you're not familiar with it, the Union type is the basic Typescript tool we use to describe a value that is “one...
Read more >Refactoring with TypeScript Discriminated Union Types and ...
Applying the types in the right use cases can make our code cleaner ... Now, if someone try to add a Create permission...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@patrick91 how did u do it can show example?
Right. And since the Union is geared towards the ObjectType and not scalars, I think you’ll have better luck once you have those ObjectTypes for the blocks. You might test it out with just a few quick ones and make sure that solves the problem you’re seeing.
On Fri, Sep 15, 2017, 5:48 PM Patrick Arminio notifications@github.com wrote: