Return union of Django objects
See original GitHub issueHi,
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.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:10 (7 by maintainers)
Top Results From Across the Web
How can I find the union of two Django querysets?
Each returns a different subset of the model's objects, based on a different property of the object. Is there any way to get...
Read more >QuerySet API reference | Django documentation
Django provides a range of QuerySet refinement methods that modify either the types of results returned by the QuerySet or the way its...
Read more >5. How to do union of two querysets from same or different ...
The UNION operator is used to combine the result-set of two or more querysets. The querysets can be from the same or from...
Read more >How can I find the union of two Django querysets - Edureka
I've got a Django model with two custom manager methods. Each returns a different subset of the model's objects, based on a different...
Read more >Django ORM - How to perform a UNION query on a database
The Django ORM series covers a range of common functions that you will perform on a database with Django. In this tutorial we...
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 Free
Top 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
I’d just like to suggest that I think having a supported, explicit mechanism to choose the variant, unrelated to any Django support, would be useful as the first solution to this problem (I assume
_type_definition
is a private member). If we can return an object that includes both the value and the chosen variant, then an automatic variant inference could be built on top of that.@patrick91 no, I was thinking that you could implement some logic in the strawberry-django resolver to look up the right StrawberryType from the Django instance. Something like this:
What do you think?