Skip to content

Fields' names are snake_case in SerializerMutation errors #556

Closed
@kamilkijak

Description

@kamilkijak

When serializer is not valid, SerializerMutation returns errors where fields' names are snake_case (default DRF behaviour). I believe that it's easier for the client to use pascalCase which is used for the input of mutation.

Simplified example:

  • serializers.py
    from rest_framework import serializers
    
    class UserSerializer(serializers.ModelSerializer):
        class Meta:
            model = User
            fields = ('birth_date', )
    
        def validate_birth_date(self, value):
            age = calculate_age(value)
            if int(age) < 18:
                raise serializers.ValidationError(
                    'Sorry, you must be at least 18 years old.'
                )
            return value
  • schema.py
    from graphene_django.rest_framework.mutation import SerializerMutation
    
    class UserMutation(SerializerMutation):
        class Meta:
            serializer_class = UserSerializer
  • GraphQL mutation
    mutation CreateUser {
      signUp(input: {birthDate: "2010-01-01"}) {
        birthDate
        errors {
          field
          messages
        }
      }
    }
  • Response with birth_date in errors (instead of birthDate)
    {
      "data": {
        "signUp": {
          "birthDate": null,
          "errors": [
            {
              "field": "birth_date",
              "messages": [
                "Sorry, you must be at least 18 years old."
              ]
            }
          ]
        }
      }
    }

Metadata

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