Hi,
Return an union of Django models isn't supported at the moment (strawberry-graphql==0.74.1
and strawberry-graphql-django==0.2.5
).
This example will fail:
Contributor = strawberry.union("Contributor", types=(Tenant, ShippingCompany))
@strawberry_django.field
def contributor(self) -> Optional[Contributor]:
return getattr(self.contributor, "shippingcompany", self.contributor)
with this error:
The type "<class 'users.models.ShippingCompany'>" cannot be resolved for the field "contributor" , are you using a strawberry.field?
One workaround is to set _type_definition
on the retuned type like this:
@strawberry_django.field
def contributor(self) -> Optional[Contributor]:
if hasattr(self.contributor, "shippingcompany"):
self.contributor.shippingcompany._type_definition = ShippingCompany._type_definition
return self.contributor.shippingcompany
self.contributor._type_definition = Tenant._type_definition # type: ignore [attr-defined]
return self.contributor
It would be great if we could return the Django objects directly.
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