strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
420 stars 122 forks source link

GenericRelation not supported? #57

Open edouardruiz opened 3 years ago

edouardruiz commented 3 years ago

Hi,

It seems that Django GenericRelation isn't handled correctly. Using @straberry_django.type decorator with one of my models results in a KeyError: <class 'django.contrib.contenttypes.fields.GenericRelation'> and it's missing in the field_type_map in strawberry_django.fields.types.

My current workaround is to define a field with a custom resolver.

Am I missing something and if not is there any plan to support this type of field?

Upvote & Fund

Fund with Polar

la4de commented 3 years ago

We haven't looked at GenericRelation which means that it is not supported yet. Would you like to contribute? :)

bellini666 commented 5 months ago

To add to this, GenericRelation should be supported, even by the optimizer. A way to make it work is to define a common "interface" between those types and type the return value with that interface. That is needed because GraphQL enforces typing very strictly and it can't be told to "return anything"

An example would be:

@strawberry_django.interface(CommonAbstractModelBetweenModels)
class SomeInterface:
    ...

@strawberry_djang.type(Foo)
class FooType(SomeInterface):
    ...

@strawberry_django.type(Bar)
class BarType(SomeInterface):
    ...

@strawberry_django.type(Parent)
class ParentType:
    generic_relation: list[SomeInterface]

Will keep this open and mark it as documentation to add an example at the docs