question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enums are generating duplicated types

See original GitHub issue

I think I read this issue in the past, graphene-django is currently throwing the next exception on my end:

“Found different types with the same name in the schema: currency, currency”

The currency field is just a field with enums

class Test:
    CURRENCY = (
        ("EUR", "EUR"),
        ("USD", "USD"),
    )

   currency = models.CharField(
        "Operation currency",
        max_length=5,
        choices=CURRENCY,
        default=CURRENCY[0]
    )

There is a serializer for this model:

class TestSerializer(serializers.ModelSerializer):
    class Meta:
        model = Test
        fields = [
            'id',
            'currency',
        ]

The schema is using the Test model

class TestNode(DjangoObjectType):
    class Meta:
        """Transaction GraphQL Type Metaclass
        Modifies the basic behavior of the type
        """
        model = Test
        filter_fields = {
            'currency': ['exact'],
        }
        interfaces = (node.Relay, )

and there are 2 mutations, one for creation based on the serializer and a custom one:

class CreateTestMutation(SerializerMutation):
    class Meta:
        serializer_class = TestSerializer
        model_operations = [
            'create',
        ]

class UpdateTestinput(InputObjectType):
    currency = graphene.String()

class UpdateTestMutation(graphene.Mutation):
    id = graphene.String()
    currency = graphene.String()

    class Arguments:
        """Arguments for the given mutation
        It defines which arguments are mandatory for the
        given mutation. The id is the one that will be used
        for searching and the input is the required object 
        for the update
        """
        id = graphene.String(required=True)
        input = UpdateTestInput(required=True)

    def mutate(self, info, id, input=None):
        test = Test.objects.get(pk=str(id))
        for key, value in input.items():
            setattr(transaction, key, value)
        test.save()
        # Notice we return an instance of this mutation
        return UpdateTestMutation(
            id=id,
            currency=transaction.currency,
        )

Now, this is currently throwing the next exception when using the latest release (2.6.0): Found different types with the same name in the schema: currency, currency.

On 2.5.0 this error does not happen.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
alhajeecommented, Aug 12, 2020

I had the same problem but this was how i solved it: You must not call your Enum class more than once in your schema

Models.py

class EnumAccountTypes(models.TextChoices):
	PERSONAL = 'PERSONAL', 'Personal'
	BUSINESS = 'BUSINESS', 'Business'
	COURIER = 'COURIER', 'Courier'
	DRIVER = 'DRIVER', 'Driver'

Schema.py

from .models import EnumAccountTypes as EnumAccountInputTypes

EnumAccountTypes = graphene.Enum.from_enum(EnumAccountInputTypes)

class CreateUser(graphene.Mutation):
  # Return object
  user = graphene.Field(UserType)
  # Arguments
  class Arguments:
''' other arguments '''
    account_type = graphene.Argument(
      EnumAccountTypes
    )

  def mutate(self, info, account_type):

    user = User(
      account_type=account_type
    )
    user.save()

    return CreateUser(user=user)

1reaction
moritz89commented, Dec 6, 2020

Regarding the Enum conversion, I see it as a bug, that the string of the member variable instead of the first element in the tuple is used. i.e., given the following enum:

class EnumAccountTypes(models.TextChoices):
   PERSONAL = 'personal', 'Personal'
   BUSINESS = 'business 'Business'
   COURIER = 'courier', 'Courier'
   DRIVER = 'driver', 'Driver'

The following query:

query {
  user(id: "...") {
    id
    accountType
  }
}

Results in the following result:

{
  "data": {
    "controllerTask": {
      "id": "...",
      "accountType": "PERSONAL"
    }
  }
}

Instead of what I would expect

{
  "data": {
    "controllerTask": {
      "id": "...",
      "accountType": "personal"
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I eliminate duplicated Enum code? - Stack Overflow
Create a type-safe utility class which will load enums by code: The interface comes down to:
Read more >
enum — Support for enumerations — Python 3.11.1 ...
The type for Enum and its subclasses. Enum. Base class for creating enumerated ... as well as creating the enum members, properly handling...
Read more >
Duplicate enums in service reference - MSDN - Microsoft
Now, the generated Reference.cs contains, literally, hundreds of duplicated properties on each one of its classes, same version, ...
Read more >
Proto parser does not detect duplicate enum constant names
Also, enum values use C++ scoping rules, so the both enum names and enum constant names must be unique across the entire scope...
Read more >
Use Enumerated Data in Generated Code - MATLAB & Simulink
To specify an integer data type size, derive your enumeration class from the ... the use of duplicate enumeration member names during code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found