strawberry-graphql / strawberry-django

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

Failing to create types from subclassed types #279

Closed moritz89 closed 1 year ago

moritz89 commented 1 year ago

Describe the Bug

The following code worked in 0.9.5 but broke in the 0.10.2 version:

import strawberry_django as gql

@gql.type
class TextChoice:
    """Maps Django's enum choices."""

    value: str
    label: str

@gql.type
class DataPointTextChoice(TextChoice):
    """Maps EnumClass and whether the enum is parametrized."""

    is_parametrized: bool

During runtime the following error is thrown:

  File "/.../my_project/types.py", line 56, in <module>
    class DataPointTextChoice(TextChoice):
TypeError: function() argument 'code' must be code, not str

System Information

Additional Context

...

Upvote & Fund

Fund with Polar

bellini666 commented 1 year ago

Hi @moritz89 ,

For curiosity, what is gql in there? I know strawberry-django-plus had a shortcut called gql, but in there gql.type would be the same as strawberry.type (i.e. not related to the django integration)

Can you provide more information regarding the context so that I can look into it?

moritz89 commented 1 year ago

Ah yes, to minimize the diff I simply changed the from strawberry_django_plus import gql to import strawberry_django as gql. Updated the description as well

moritz89 commented 1 year ago

I also tried running the example app, but it results in start errors (with commit 644df4e):

  File "/home/moritz/.cache/pypoetry/virtualenvs/strawberry-graphql-django-Rxi-z_BV-py3.10/lib/python3.10/site-packages/strawberry_django/__init__.py", line 1, in <module>
    from . import auth, filters, mutations, ordering, relay
  File "/home/moritz/.cache/pypoetry/virtualenvs/strawberry-graphql-django-Rxi-z_BV-py3.10/lib/python3.10/site-packages/strawberry_django/auth/__init__.py", line 1, in <module>
    from .mutations import login, logout, register
  File "/home/moritz/.cache/pypoetry/virtualenvs/strawberry-graphql-django-Rxi-z_BV-py3.10/lib/python3.10/site-packages/strawberry_django/auth/mutations.py", line 10, in <module>
    from strawberry_django.mutations import mutations, resolvers
  File "/home/moritz/.cache/pypoetry/virtualenvs/strawberry-graphql-django-Rxi-z_BV-py3.10/lib/python3.10/site-packages/strawberry_django/mutations/__init__.py", line 1, in <module>
    from .mutations import create, delete, input_mutation, mutation, update
  File "/home/moritz/.cache/pypoetry/virtualenvs/strawberry-graphql-django-Rxi-z_BV-py3.10/lib/python3.10/site-packages/strawberry_django/mutations/mutations.py", line 16, in <module>
    from strawberry.extensions.field_extension import FieldExtension
ModuleNotFoundError: No module named 'strawberry.extensions.field_extension'
bellini666 commented 1 year ago

Ah yes, to minimize the diff I simply changed the from strawberry_django_plus import gql to import strawberry_django as gql. Updated the description as well

The translations of gql it self is not straight forward, only gql.django is mostly. For example, gql.type is strawberry.type and not strawberry.django.type (which was gql.django.type)

In your example, gql.type is strawberry_django.type, meaning you are not using it correctly: it expects a model as the first argument, i.e. @gql.type(SomeModel)

Now, if you were trying to use those as a basic strawberry object, then you would need to change import import to import strawberry as gql

I also tried running the example app, but it results in start errors (with commit 644df4e):

Oh, the example app probably needs to be updated! Thanks for pointing it out :)