Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davidkell opened this issue Nov 5, 2020 · 2 comments

Comments

@davidkell
Copy link
Contributor

davidkell commented Nov 5, 2020

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)
@trollboy
Copy link
Contributor

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

@conao3
Copy link
Contributor

conao3 commented Jan 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants