Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields on generic interfaces cannot be generic #3602

Open
thearchitector opened this issue Aug 23, 2024 · 2 comments
Open

Fields on generic interfaces cannot be generic #3602

thearchitector opened this issue Aug 23, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@thearchitector
Copy link
Contributor

thearchitector commented Aug 23, 2024

Describe the Bug

Generics that have been decorated with @strawberry.interface are unable to define fields with generic return types. However, the same field seems to work fine if the generic is decorated as a type instead.

The following code:

from typing import Generic, TypeVar

import strawberry

T = TypeVar("T")


@strawberry.interface
class GenericInterface(Generic[T]):
    obj: strawberry.Private[T]

    @strawberry.field
    def as_list(self) -> list[T]:
        return [self.obj]


@strawberry.type
class GenericObject(GenericInterface[int]):
    pass


@strawberry.type
class Query:
    @strawberry.field
    def numbers(self) -> GenericObject:
        return GenericObject(obj=1)


res = strawberry.Schema(query=Query).execute_sync(
    """
        query {
            numbers {
                asList
            }
        }
    """
)
print(res)

produces the error:

TypeError: GenericInterface fields cannot be resolved. The type "<strawberry.type.StrawberryList object at 0x7e3c02eaece0>" is generic, but no type has been passed

If you replace @strawberry.interface with @strawberry.type, the error goes away and the query returns what I would expect:

ExecutionResult(data={'numbers': {'asList': [1]}}, errors=None, extensions={})

System Information

  • Operating system: Linux
  • Strawberry version (if applicable): 0.237.3

Upvote & Fund

  • We're using Polar.sh 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.
Fund with Polar
@thearchitector thearchitector added the bug Something isn't working label Aug 23, 2024
@patrick91
Copy link
Member

Just had a quick look at this and it might not be that easy to do, at least not in a couple of hours :D but we should support this use case 😊

@thearchitector
Copy link
Contributor Author

thearchitector commented Aug 24, 2024

Just had a quick look at this and it might not be that easy to do, at least not in a couple of hours :D but we should support this use case 😊

thanks for looking at it! i agree, an interface generic seems like a really natural/perfect use case for interfaces. so much so that i figured this was a bug rather than a missing feature haha, given that it works for @types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants