graphene-pydantic icon indicating copy to clipboard operation
graphene-pydantic copied to clipboard

Enum name conflict for models used in both ObjectType and InputObjectType

Open davidkell opened this issue 5 years ago • 2 comments

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:

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:

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)

davidkell avatar Nov 05 '20 19:11 davidkell

Can we update the docs (readme) to reflect this gotcha?

trollboy avatar Mar 19 '21 20:03 trollboy

I cant see any errors for your snippet, I'm missing something? (I use this repository's poetry.lock and I use Python3.10)

conao3 avatar Jan 09 '23 10:01 conao3