strawberry-graphql / strawberry-django

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

Field with resolver (strawberry_django.field(resolver=resolver)) returns incorrect type #411

Closed guizesilva closed 11 months ago

guizesilva commented 11 months ago

Describe the Bug

When using strawberry_django.field with resolve in this style,

def resolver(root: "FruitType") -> str:
    return f"{root.id} - {root.name}"

@strawberry_django.type(model=Fruit)
class FruitType:
    id: int
    name: str
    id_name: str = strawberry_django.field(resolver=resolver)

the type returned is going to be StrawberryDjangoField instead of str. This happens only for resolvers with arguments. When the resolver uses no arguments, the correct type is returned.

This is similar to this issue reported at strawberry-graphql/strawberry

As proposed by the above issue, a fix could be replacing this (https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/strawberry_django/fields/field.py#L387):

@overload
def field(
    *,
    ...
    resolver: Callable[[], _T],
    ...
) -> _T: ...

for this

@overload
def field(
    *,
    ...
    resolver: Callable[..., _T],
    ...
) -> _T: ...

Another options is to take the same approach used by strawberry here

_RESOLVER_TYPE = Union[
    StrawberryResolver[T],
    Callable[..., T],
    Callable[..., Coroutine[T, Any, Any]],
    Callable[..., Awaitable[T]],
    "staticmethod[Any, T]",
    "classmethod[Any, Any, T]",
]

@overload
def field(
    *,
    ...
    resolver: _RESOLVER_TYPE[T],
    ...
) -> _T: ...

System Information

Additional Context

I can create a PR with the preferred solution if this is indeed an issue.

Upvote & Fund

Fund with Polar

bellini666 commented 11 months ago

Hey @guizesilva ,

Feel free to create the PR for that :)

And I think the best option is to use the same approach as strawberry does