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

No way to resolve type of non-strawberry types returned for an interface #1405

Closed
AlecRosenbaum opened this issue Nov 5, 2021 · 3 comments
Closed

Comments

@AlecRosenbaum
Copy link
Contributor

AlecRosenbaum commented Nov 5, 2021

This comment is correct:

def resolve_type(
obj: Any,
info: GraphQLResolveInfo,
type_: Union[GraphQLInterfaceType, GraphQLUnionType],
) -> GraphQLObjectType:
# TODO: this will probably break when passing dicts
# or even non strawberry types
resolved_type = self.type_map[
obj.__class__._type_definition.name
].implementation

It's correctly reflected in the docs, but still does pose a pretty bad limitation for implementations that resolve to non-strawberry types.

The following test fails (using a similar example to #1150):

def test_interface_type_resolution():
    @strawberry.interface
    class Node:
        id: int

    @strawberry.type
    class Anime(Node):
        name: str

    @dataclass
    class AnimeORM:
        id: int
        name: str

    @strawberry.type
    class Query:
        @strawberry.field
        def node(self) -> Node:
            return AnimeORM(id=1, name="One Piece")  # type: ignore

    schema = strawberry.Schema(query=Query)

    query = "{ node { __typename, id } }"
    result = schema.execute_sync(query)

    assert not result.errors
    assert result.data == {"node": {"__typename": "Anime", "id": 1}}
    # errors with: "type object 'AnimeORM' has no attribute '_type_definition'"

In graphene, the converter allows you to define is_type_of classmethods on concrete types or a resolve_type classmethod on the interface. I think the equivalent within strawberry would be to optionally include is_type_of here, and to gracefully fail type resolution for non-strawberry objects (by returning None) within resolve_type.

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
@AlecRosenbaum
Copy link
Contributor Author

I was wrong about the graceful failure on resolve_type. graphene doesn't provide a resolve_type function by default. It seem that resolve_type takes precedence over is_type_of if it's present, regardless of what it returns.

If I understand correctly, it seems that graphene forces the presence of a user-defined is_type_of function on implementing types, a resolve_type function on the interface, or will generate an is_type_of function for you based on a list of types provided within the metadata (still explicit / opt-in).

Should strawberry require explicit type resolution for duck-typed objects?

@AlecRosenbaum
Copy link
Contributor Author

Not sure why this closed...

@AlecRosenbaum AlecRosenbaum reopened this Nov 15, 2021
@pheki
Copy link

pheki commented Aug 1, 2024

I think this was fixed by #1406 (at least that was intended to fix this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants