When trying to return union type of custom types, the schema generator fails.
Describe the Bug
Two custom types declared, and one that contains the union of them.
T = TypeVar("T")
@strawberry.type
class MySubType:
data: str
@strawberry.type
class MySubType2:
data: int
@strawberry.type
class MyType(Generic[T]):
data: T
@strawberry.type
class Mutation:
@strawberry.field
async def test_mutation(self) -> MyType[Union[MySubType, MySubType2]]:
return MyType(data=MySubType(data="test"))
The startup fails, with the following traceback:
Traceback (most recent call last):
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/graphql/type/definition.py", line 808, in fields
fields = resolve_thunk(self._fields)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
return thunk() if callable(thunk) else thunk
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 509, in
fields=lambda: self.get_graphql_fields(object_type),
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 373, in get_graphql_fields
return _get_thunk_mapping(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 133, in _get_thunk_mapping
thunk_mapping[name_converter(field)] = field_converter(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 314, in from_field
self.from_maybe_optional(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 762, in from_maybe_optional
return GraphQLNonNull(self.fromtype(type))
File "/home/dewitars/programming/dvrt-graphene-tools/dvrt_graphene_tools/schema.py", line 61, in from_type
return super().fromtype(type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 780, in from_type
return self.fromobject(type.__strawberry_definition__)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 476, in from_object
object_type_name = self.config.name_converter.from_type(object_type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 55, in from_type
return self.fromobject(type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 70, in from_object
return self.from_generic(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 136, in from_generic
name = self.get_fromtype(type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 150, in get_fromtype
assert type.graphql_name
AssertionError
But if we are specific, and put the Union in 'MyType', it works just fine:
T = TypeVar("T")
@strawberry.type
class MySubType:
data: str
@strawberry.type
class MySubType2:
data: int
@strawberry.type
class MyType:
data: Union[MySubType, MySubType2]
@strawberry.type
class Mutation:
@strawberry.field
async def test_mutation(self) -> MyType:
return MyType(data=MySubType(data="test"))
System Information
Operating system: Ubuntu 23.04 x86_64
Strawberry version (if applicable): 0.195.2
Additional Context
I can not provide the lines that are related to dvrt-graphene-tools (proprietary), but I can ensure that it just calls the strawberry.schema.schema_converter.GraphQLCoreConverter.from_type function with the provided type.
Upvote & Fund
We're using Polar.sh so you can upvote and help fund this issue.
We receive the funding once the issue is completed & confirmed by you.
Thank you in advance for helping prioritize & fund our backlog.
When trying to return union type of custom types, the schema generator fails.
Describe the Bug
Two custom types declared, and one that contains the union of them.
The startup fails, with the following traceback: Traceback (most recent call last): File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/graphql/type/definition.py", line 808, in fields fields = resolve_thunk(self._fields) File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/graphql/type/definition.py", line 300, in resolve_thunk return thunk() if callable(thunk) else thunk File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 509, in
fields=lambda: self.get_graphql_fields(object_type),
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 373, in get_graphql_fields
return _get_thunk_mapping(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 133, in _get_thunk_mapping
thunk_mapping[name_converter(field)] = field_converter(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 314, in from_field
self.from_maybe_optional(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 762, in from_maybe_optional
return GraphQLNonNull(self.fromtype(type))
File "/home/dewitars/programming/dvrt-graphene-tools/dvrt_graphene_tools/schema.py", line 61, in from_type
return super().fromtype(type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 780, in from_type
return self.fromobject(type.__strawberry_definition__)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/schema_converter.py", line 476, in from_object
object_type_name = self.config.name_converter.from_type(object_type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 55, in from_type
return self.fromobject(type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 70, in from_object
return self.from_generic(
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 136, in from_generic
name = self.get_fromtype(type)
File "/home/dewitars/.pyenv/versions/3.10.5/envs/fax-api/lib/python3.10/site-packages/strawberry/schema/name_converter.py", line 150, in get_fromtype
assert type.graphql_name
AssertionError
But if we are specific, and put the Union in 'MyType', it works just fine:
System Information
Additional Context
I can not provide the lines that are related to
dvrt-graphene-tools
(proprietary), but I can ensure that it just calls thestrawberry.schema.schema_converter.GraphQLCoreConverter.from_type
function with the provided type.Upvote & Fund