strawberry-graphql / strawberry-django

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

Defining related fields on an interface throws a type mismatch error #195

Closed fireteam99 closed 1 year ago

fireteam99 commented 2 years ago

Describe the Bug

When defining an interface with that contains any related field, an error is thrown where the type is identified to be a DjangoModelType but is (correctly) a Strawberry type.

In the example below, we are passing in CardNode (a strawberry type) as the related field, but error thinks it is a DjangoModelType.

Code:

@strawberry.interface
class MyInterface(MyBaseClass):
  card: "CardNode"

@strawberry.django.type(Card)
class CardNode:
  name: auto

Error:


❌ Interface field MyInterface.card expects type CardNode! but MyInterface.card is type DjangoModelType.```
## System Information

 - Operating system: macOS
 - Strawberry version (if applicable): 0.133.5
 - Strawberry Django version: 0.5.3

## Additional Context
Strawberry discord thread: https://discordapp.com/channels/689806334337482765/1029471127963172894

<!-- POLAR PLEDGE BADGE START -->
## Upvote & Fund

- We're using [Polar.sh](https://polar.sh/strawberry-graphql) so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.

<a href="https://polar.sh/strawberry-graphql/strawberry-graphql-django/issues/195">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/strawberry-graphql/strawberry-graphql-django/issues/195/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/strawberry-graphql/strawberry-graphql-django/issues/195/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->
bellini666 commented 2 years ago

Hi @fireteam99 ,

Is MyBaseClass your django base model?

What I usually do is to have an abstract model mixin and define an interface out of it with a code like this:

class SomeModelMixin(models.Model):
    class Meta:
        abstract = True

    some_field = models.CharField(...)

@strawberry.django.type(is_interface=True)
class SomeModelInterface:
    some_field: strawberry.auto

@strawberry.django.type(ConcreteModel)
class ConcreteModelType(SomeModelInterface):
    ...

I'm using strawberry-django-plus's @interface for that, which is basically a shortcut. I don't remember if I had any special handling for it, so if the example above doesn't work we can probably try to port the code that handles it to this repo :)

fireteam99 commented 2 years ago

Hi @bellini666, MyBaseClass is a django graphene type that maps to a django base model. The MyInterface interface I am try to create maps to an abstract django model which inherits from the MyBaseClass model.

I tried your suggestion with the is_interface arg but got a different error:

@strawberry.interface
class MyInterface(AbstractDjangoModel, is_interface=True):
  card: "CardNode"

@strawberry.django.type(Card)
class CardNode:
  name: auto

Error:

AttributeError: 'NoneType' object has no attribute 'expandtabs'
bellini666 commented 2 years ago

@fireteam99 actually you should write that like this:

@strawberry.django.type(AbstractDjangoModel, is_interface=True)
class MyInterface:
  card: "CardNode"

@strawberry.django.type(Card)
class CardNode:
  name: auto
fireteam99 commented 2 years ago

Hmmm so when I tried @strawberry.django.type(is_interface=True) it complained: TypeError: type() missing 1 required positional argument: 'model'

But when I tried to pass my abstract Django model as the type @strawberry.django.type(MyAbstractModel, is_interface=True) I got the expandtabs error again:

AttributeError: 'NoneType' object has no attribute 'expandtabs'
bellini666 commented 2 years ago

@fireteam99 sorry, in a hurry I wrote it wrong. The model should be passed as the first argument to the strawberry.django.type function. I edited my response with the correct solution

fireteam99 commented 2 years ago

@bellini666 ah gotcha. I tried that but I'm still getting the expandtabs error...

bellini666 commented 1 year ago

Hi @fireteam99 ,

Its been a while, and since then strawberry django went through a major refactor with a lot of fixes and improvements. Does this issue still happen?

fireteam99 commented 1 year ago

@bellini666 thanks for the heads up! Will update and report back

fireteam99 commented 1 year ago

Hey @bellini666, I'm happy to report that this issue appears to be fixed! I tested on versions:

strawberry-graphql = "==0.199.2"
strawberry-graphql-django = "==0.16.0"