question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return union of Django objects

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
radixcommented, Aug 12, 2022

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.

1reaction
jkimbocommented, Sep 1, 2021

@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:

class StrawberryDjangoField(StrawberryField):
	def get_result(self, source, info, arguments):
		if self.resolved_type is Union:
			model_instance = super.get_result(source, info, arguments)
			strawberry_type = get_type_from_instance(model_instance, options=self.resolved_type.types)  # this function will need to be implemented
			return strawberry_type.from_instance(model_instance)
		...

What do you think?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found