Skip to content

Mutation can not represent TextChoice value  #1476

Closed
@FranciscoNMora

Description

@FranciscoNMora

Note: for support questions, please use stackoverflow. This repository's issues are reserved for feature requests and bug reports.

  • What is the current behavior?

When executing a mutation that returns a Type of a Model that has a CharField with a TextChoices enum, it returns the following error:
"Enum 'Status' cannot represent value: <AppointmentStatus.CANCELLED: 'C'>"

Define these classes

class AppointmentStatus(models.TextChoices):
    BOOKED = "B", "Booked"
    CANCELLED = "C", "Cancelled"
    

class Appointment(models.Model):
    id = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
    status = models.CharField(max_length=1, choices=AppointmentStatus)


class AppointmentType(DjangoObjectType):
    class Meta:
        model = Appointment


class CancelAppointment(Mutation):
    appointment = graphene.Field(AppointmentType, required=False)

    class Arguments:
        appointment_id = graphene.UUID(required=True)

    def mutate(self, info, appointment_id):
        appointment = Appointment.objects.get(id=appointment_id)
        return CancelAppointment(appointment=appointment)


class Mutation(object):
    cancel_appointment = CancelAppointment.Field()

When using this mutation and querying the result status

mutation cancelAppointment($appointmentId: UUID!) {
  cancelAppointment(
    appointmentId: $appointmentId
  ) {
    appointment {
      id
      status
    }
  }
}

and querying the result status I receive the following error:

"errors": [
    {
      "message": "Enum 'Status' cannot represent value: <AppointmentStatus.CANCELLED: 'C'>'",
      "locations": [
        {
          "line": 10,
          "column": 7
        }
      ],
      "path": [
        "cancelAppointment",
        "appointment",
        "status"
      ]
    }

  • What is the expected behavior?
    The appointment status values should be shown without errors.

  • What is the motivation / use case for changing the behavior?

  • Please tell us about your environment:

    • Version: graphene-django==3.1.5, python 3.9.13
    • Platform: Ubuntu 22.04
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)

I'm trying to migrate from graphene-django 2.15.0 to 3.1.5.

These are the whole traces of the problem:

File "/usr/local/lib/python3.9/site-packages/graphql/execution/execute.py", line 540, in execute_field       
  completed = self.complete_value(                                                                           
File "/usr/local/lib/python3.9/site-packages/graphql/execution/execute.py", line 639, in complete_value      
  return self.complete_leaf_value(cast(GraphQLLeafType, return_type), result)                                
File "/usr/local/lib/python3.9/site-packages/graphql/execution/execute.py", line 774, in complete_leaf_value 
  serialized_result = return_type.serialize(result)                                                          
File "/usr/local/lib/python3.9/site-packages/graphene/types/definitions.py", line 58, in serialize           
  return super(GrapheneEnumType, self).serialize(value)                                                      
File "/usr/local/lib/python3.9/site-packages/graphql/type/definition.py", line 1233, in serialize            
  raise GraphQLError(           

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions