graphql-python / graphql-python/graphene-pydantic

Enum name conflict for models used in both ObjectType and InputObjectType

Open
#46 2 comments 5 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
249
Forks
47
PR merge metrics
No merged PRs in 30d

Description

If you try to build an ObjectType and InputObjectType for pydantic models with the same enum, it tries to create the same enum twice. (This is not an issue with re-using the same enum in multiple ObjectTypes though.)

Here's a reproducible example:

```python
import enum
import pydantic
import graphene
from graphene_pydantic import PydanticObjectType, PydanticInputObjectType

class Color(enum.Enum):
red = 1
green = 2
blue = 3

class ColorModel(pydantic.BaseModel):
color: Color

class ColorType(PydanticObjectType):
class Meta:
model = ColorModel

class ColorInputType(PydanticInputObjectType
):
class Meta:
model = ColorModel

class CreateColor(graphene.Mutation):
class Arguments:
data = graphene.Argument(ColorInputType)

ok = graphene.String()

def mutate(self, info, data):
return ColorModel(**data)

class Query(graphene.ObjectType):
color = graphene.Field(ColorType)

class Mutation(graphene.ObjectType):
createColor = CreateColor.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)
```

And the error:

```
95 _type = map[type._meta.name]
96 if isinstance(_type, GrapheneGraphQLType):
---> 97 assert _type.graphene_type == type, (
98 "Found different types with the same name in the schema: {}, {}."
99 ).format(_type.graphene_type, type)

AssertionError: Found different types with the same name in the schema: Color, Color.
```

The workaround is just to manually define the enum type, but thought it would be helpful to know.

For others who got this error:

```python
ColorEnumType = graphene.Enum.from_enum(Color)

class ColorType(PydanticObjectType):
class Meta:
model = ColorModel

color = graphene.Field(ColorEnumType)

class ColorInputType(PydanticInputObjectType
):
class Meta:
model = ColorModel

color = graphene.Argument(ColorEnumType)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.