I had a strawberry.union
kind of union type declaration, and been trying to migrate to the updated Annotated Union kind of declaration, but that change then results in a TypeError.
The original code something looks like this:
SomeStuff = strawberry.union(
"SomeStuff",
types=(
Resource,
Process,
),
)
and according to the docs it should be:
SomeStuff = Annotated[Union[Resource, Process], strawberry.union("SomeStuff")]
This latter then results in the following error when the code starts to resolve the schema, let's say a Thing
has a field of SomeStuff
type:
../../../.pyenv/versions/3.10.11/envs/llm-ct/lib/python3.10/site-packages/graphql/type/definition.py:946: in fields
raise cls(f"{self.name} fields cannot be resolved. {error}") from error
E TypeError: Thing fields cannot be resolved. Unexpected type 'typing.Annotated[typing.Union[app.models.graphql.resource.Resource, app.models.graphql.process.Process], <strawberry.union.StrawberryUnion object at 0x10b00b0a0>]'
Any idea what I might be doing wrong or missing? Does anything else have to change as well, besides the definition of SomeStuff
?
The definition of Thing
is something like this, not sure if the lazy loading has any effect on the outcome:
@strawberry.interface
class Thing:
name: str
@strawberry.field
async def observed_object(
self, info: Info
) -> Annotated[
"SomeStuff",
strawberry.lazy("app.models.graphql.stuffs"),
]:
# [...snip...]
(Lazy loading is needed as the real case has some circularity in dependencies).
I wonder the problem is really here, that in the union-type definition SomeStuff
would be now incorrect, reading the "resolving an union" section in the docs. But that would be quite a boilerplate (in the real setup I have more than 2 types that are unioned).
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