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.

Fields' names are snake_case in SerializerMutation errors

See original GitHub issue

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."
              ]
            }
          ]
        }
      }
    }
    

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jkimbocommented, Jul 13, 2019

This issue should be fixed in v2.4.0 by setting the CAMELCASE_ERRORS setting: http://docs.graphene-python.org/projects/django/en/latest/settings/#camelcase-errors

0reactions
jkimbocommented, Jun 14, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Graphene Documentation - Read the Docs
These errors have two fields: field, a string containing the name of the invalid form field, and messages, a list of strings with...
Read more >
unable to create a SerializerMutation without errors, but data ...
I do not why but when I try to create new data using serializermutation, data is created but graphiql return an error. Here...
Read more >
1. Naming rules - GraphQL Rules
Use camelCase for GraphQL fields and arguments. 1.2. Use UpperCamelCase for GraphQL types. 1.3. Use CAPITALIZED_WITH_UNDERSCORES to name ENUM types. 1.4. Avoid ...
Read more >
Mutations - Graphene-Python
These errors have two fields: field , a string containing the name of the invalid ... from graphene_django.rest_framework.mutation import SerializerMutation ...
Read more >
Snake case property names - Json.NET
This sample uses a T:Newtonsoft.Json.Serialization.SnakeCaseNamingStrategy specified using a contract resolver to snake case serialized property names.
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