Repository: strawberry-graphql/strawberry

Issue:
Unable to use Unions with Generics
## Describe the Bug

Not sure if it is a bug or something non supported but when passing a union to a generic type strawberry is unable to initialize the schema.

The following example would work using a single strawberry type with Connection, but it fails when using an union 


```python
from typing import Generic, TypeVar, Union
import strawberry

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


Entities = Union[Entity1, Entity2]


@strawberry.type
class Query:
    @strawberry.field
    def entities(self) -> Connection[Entities]:
        return Connection(
            edges=[
                Edge(
                    cursor="1",
                    node=Entity1(id=1),
                ),
                Edge(
                    cursor="2",
                    node=Entity2(id=2),
                ),
            ],
        )


schema = strawberry.Schema(Query)
print(schema.execute_sync("{ entities { __typename } }"))

```
error
```
raise cls(f"{self.name} fields cannot be resolved. {error}") from error
TypeError: Query fields cannot be resolved.
```


## System Information

 - Operating system:
 - Strawberry version (if applicable): 0.208.1


<!-- POLAR PLEDGE BADGE START -->
## Upvote & Fund

- We're using [Polar.sh](https://polar.sh/strawberry-graphql) 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.

<a href="https://polar.sh/strawberry-graphql/strawberry/issues/3098">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/strawberry-graphql/strawberry/issues/3098/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/strawberry-graphql/strawberry/issues/3098/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->



Discussion:
hi @farzeni this seems to due to the union not having a name 😊

To fix it you can change this

```diff
-Entities = Union[Entity1, Entity2]
+Entities = Annotated[Union[Entity1, Entity2], strawberry.union(name="Entities")]
```

I'll see if I can make it work without passing a name 😊

Resolving patch:
diff --git a/strawberry/schema/name_converter.py b/strawberry/schema/name_converter.py
--- a/strawberry/schema/name_converter.py
+++ b/strawberry/schema/name_converter.py
@@ -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):


Test patch:
diff --git a/tests/python_312/test_generics.py b/tests/python_312/test_generics.py
--- a/tests/python_312/test_generics.py
+++ b/tests/python_312/test_generics.py
@@ -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
diff --git a/tests/schema/test_generics.py b/tests/schema/test_generics.py
--- a/tests/schema/test_generics.py
+++ b/tests/schema/test_generics.py
@@ -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")