Skip to content

Commit

Permalink
Fix issue with unnamed unions and generics (#3099)
Browse files Browse the repository at this point in the history
* Fix issue with unnamed unions and generics

* Remove unused resolvers

* Ignore 3.12 in black for now
  • Loading branch information
patrick91 authored Sep 18, 2023
1 parent af05822 commit d4cecb1
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ repos:
rev: 23.7.0
hooks:
- id: black
exclude: ^tests/\w+/snapshots/
exclude: ^(tests/\w+/snapshots/|tests/python_312/)

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.280
Expand Down
31 changes: 31 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Release type: patch

This release fixes an issue that would prevent using generics with unnamed
unions, like in this example:

```python
from typing import Generic, TypeVar, Union
import strawberry

T = TypeVar("T")


@strawberry.type
class Connection(Generic[T]):
nodes: list[T]


@strawberry.type
class Entity1:
id: int


@strawberry.type
class Entity2:
id: int


@strawberry.type
class Query:
entities: Connection[Union[Entity1, Entity2]]
```
5 changes: 1 addition & 4 deletions strawberry/schema/name_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ def get_from_type(self, type_: Union[StrawberryType, type]) -> str:
elif isinstance(type_, EnumDefinition):
name = type_.name
elif isinstance(type_, StrawberryUnion):
# TODO: test Generics with unnamed unions
assert type_.graphql_name

name = type_.graphql_name
name = type_.graphql_name if type_.graphql_name else self.from_union(type_)
elif isinstance(type_, StrawberryList):
name = self.get_from_type(type_.of_type) + "List"
elif isinstance(type_, StrawberryOptional):
Expand Down
55 changes: 55 additions & 0 deletions tests/python_312/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,58 @@ def real(self) -> Abstract:

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



def test_generics_via_anonymous_union():
@strawberry.type
class Edge[T]:
cursor: str
node: T

@strawberry.type
class Connection[T]:
edges: list[Edge[T]]

@strawberry.type
class Entity1:
id: int

@strawberry.type
class Entity2:
id: int

@strawberry.type
class Query:
entities: Connection[Union[Entity1, Entity2]]

schema = strawberry.Schema(query=Query)

expected_schema = textwrap.dedent(
"""
type Entity1 {
id: Int!
}
union Entity1Entity2 = Entity1 | Entity2
type Entity1Entity2Connection {
edges: [Entity1Entity2Edge!]!
}
type Entity1Entity2Edge {
cursor: String!
node: Entity1Entity2!
}
type Entity2 {
id: Int!
}
type Query {
entities: Entity1Entity2Connection!
}
"""
).strip()

assert str(schema) == expected_schema
56 changes: 56 additions & 0 deletions tests/schema/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,62 @@ def example(self) -> List[Union[Edge[int], Edge[str]]]:
}


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

@strawberry.type
class Edge(Generic[T]):
cursor: str
node: T

@strawberry.type
class Connection(Generic[T]):
edges: list[Edge[T]]

@strawberry.type
class Entity1:
id: int

@strawberry.type
class Entity2:
id: int

@strawberry.type
class Query:
entities: Connection[Union[Entity1, Entity2]]

schema = strawberry.Schema(query=Query)

expected_schema = textwrap.dedent(
"""
type Entity1 {
id: Int!
}
union Entity1Entity2 = Entity1 | Entity2
type Entity1Entity2Connection {
edges: [Entity1Entity2Edge!]!
}
type Entity1Entity2Edge {
cursor: String!
node: Entity1Entity2!
}
type Entity2 {
id: Int!
}
type Query {
entities: Entity1Entity2Connection!
}
"""
).strip()

assert str(schema) == expected_schema


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

Expand Down

0 comments on commit d4cecb1

Please sign in to comment.