Skip to content

Enum name conflict for models used in both ObjectType and InputObjectType #46

Open
@davidkell

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:

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)

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions