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

Skip optimised check when returning generic types #2955

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Release type: patch

This release fixes a bug where returning a generic type from a field
that was returning an interface would throw an error.
16 changes: 11 additions & 5 deletions strawberry/schema/schema_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
StrawberryList,
StrawberryOptional,
StrawberryType,
get_object_definition,
has_object_definition,
)
from strawberry.types.info import Info
Expand Down Expand Up @@ -435,11 +436,16 @@
obj: Any, info: GraphQLResolveInfo, abstract_type: GraphQLAbstractType
) -> Union[Awaitable[Optional[str]], str, None]:
if isinstance(obj, interface.origin):
return obj.__strawberry_definition__.name
else:
# Revert to calling is_type_of for cases where a direct subclass
# of the interface is not returned (i.e. an ORM object)
return default_type_resolver(obj, info, abstract_type)
type_definition = get_object_definition(obj, strict=True)

# TODO: we should find the correct type here from the
# generic
if not type_definition.is_generic:
return obj.__strawberry_definition__.name

# Revert to calling is_type_of for cases where a direct subclass
# of the interface is not returned (i.e. an ORM object)
return default_type_resolver(obj, info, abstract_type)

Check warning on line 448 in strawberry/schema/schema_converter.py

View check run for this annotation

Codecov / codecov/patch

strawberry/schema/schema_converter.py#L448

Added line #L448 was not covered by tests

return resolve_type

Expand Down
45 changes: 45 additions & 0 deletions tests/schema/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,3 +1180,48 @@ def foo(self) -> GenericObject[str]:
"repr": "foo",
}
}


def test_generic_interface_extra_types():
T = TypeVar("T")

@strawberry.interface
class Abstract:
x: str = ""

@strawberry.type
class Real(Generic[T], Abstract):
y: T

@strawberry.type
class Query:
@strawberry.field
def real(self) -> Abstract:
return Real[int](y=0)

schema = strawberry.Schema(Query, types=[Real[int]])

assert (
str(schema)
== textwrap.dedent(
"""
interface Abstract {
x: String!
}

type IntReal implements Abstract {
x: String!
y: Int!
}

type Query {
real: Abstract!
}
"""
).strip()
)

query_result = schema.execute_sync("{ real { __typename x } }")

assert not query_result.errors
assert query_result.data == {"real": {"__typename": "IntReal", "x": ""}}
Loading