Repository: strawberry-graphql/strawberry

Issue:
Cannot create type with multiple Unions
```python
from typing import Union

import strawberry


@strawberry.type
class CoolType:
    @strawberry.type
    class UnionA1:
        value: int

    @strawberry.type
    class UnionA2:
        value: int

    @strawberry.type
    class UnionB1:
        value: int

    @strawberry.type
    class UnionB2:
        value: int

    field1: Union[UnionA1, UnionA2]
    field2: Union[UnionB1, UnionB2]


schema = strawberry.Schema(query=CoolType)
```

```.pytb
Traceback (most recent call last):
  File "/home/ignormies/.config/JetBrains/PyCharm2020.1/scratches/scratch.py", line 28, in <module>
    schema = strawberry.Schema(query=CoolType)
  File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/strawberry/schema.py", line 25, in __init__
    super().__init__(
  File "/home/ignormies/.local/share/virtualenvs/gql-bf-XGX4szKA-py3.8/lib/python3.8/site-packages/graphql/type/schema.py", line 239, in __init__
    raise TypeError(
TypeError: Schema must contain uniquely named types but contains multiple types named '_resolver'.
```

Removing either `field1` or `field2` allows the schema to be created


Discussion:
Oh nice, thanks for the report. This is fixed in the rewrite I'm doing -> #352.

If it is a deal breaker I can do another PR to fix it, so you don't have have to wait for the rewrite :)

PS.Is there a reason why the types are defined inside the CoolType class? :)
> I can do another PR to fix it, so you don't have have to wait for the rewrite

That would be quite appreciated. I was trying to figure out how I was going to find a workaround for this issue.

> Is there a reason why the types are defined inside the CoolType class?

I just have a practice where I try to keep my namespaces minimal. In hindsight, it wasn't necessary for this example, but the code I was making the example from had it going on, where the analogous nested types are only used with the parent type. I prefer doing `[module.]CoolType.UnionA1` instead of just `[module.]UnionA1`, if `UnionA1` will only _ever_ be used for `CoolType`.

Hopefully that's not too confusing... 🙃
> I just have a practice where I try to keep my namespaces minimal.

oh I see, I'd use python modules for that, but that's my preference :) But I understand the thinking about the type being only used with the parent type.

I'll try to see if I can make a quick PR now to fix the issue with current master :)


Resolving patch:
diff --git a/strawberry/type.py b/strawberry/type.py
--- a/strawberry/type.py
+++ b/strawberry/type.py
@@ -38,6 +38,8 @@ def _resolver(root, info):
 
         return field_resolver
 
+    _resolver.__name__ = field_name
+
     return _resolver
 
 


Test patch:
diff --git a/tests/test_union.py b/tests/test_union.py
--- a/tests/test_union.py
+++ b/tests/test_union.py
@@ -284,3 +284,49 @@ class B:
 
     with pytest.raises(ValueError, match=r"Cannot use union type directly"):
         Result()
+
+
+def test_multiple_unions():
+    @strawberry.type
+    class CoolType:
+        @strawberry.type
+        class UnionA1:
+            value: int
+
+        @strawberry.type
+        class UnionA2:
+            value: int
+
+        @strawberry.type
+        class UnionB1:
+            value: int
+
+        @strawberry.type
+        class UnionB2:
+            value: int
+
+        field1: Union[UnionA1, UnionA2]
+        field2: Union[UnionB1, UnionB2]
+
+    schema = strawberry.Schema(query=CoolType)
+
+    query = """
+        {
+            __type(name:"CoolType") {
+                name
+                description
+                fields {
+                    name
+                }
+            }
+        }
+    """
+
+    result = graphql_sync(schema, query)
+
+    assert not result.errors
+    assert result.data["__type"] == {
+        "description": None,
+        "fields": [{"name": "field1"}, {"name": "field2"}],
+        "name": "CoolType",
+    }