When trying to return union type of custom types, the schema generator fails.
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.from_type(type))
File "/home/dewitars/programming/dvrt-graphene-tools/dvrt_graphene_tools/schema.py", line 61, in from_type
return super().from_type(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.from_object(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.from_object(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_from_type(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_from_type
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"))
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.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too