graphql-python / graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
http://docs.graphene-python.org/projects/django/en/latest/
MIT License
4.31k stars 769 forks source link

Django ValidationError are serialized as string instead of error array #1042

Open cedricfarinazzo opened 4 years ago

cedricfarinazzo commented 4 years ago

Django ValidationError are serialized as string instead of error array.

from django.core.exceptions import ValidationError

from graphene import ObjectType, Schema, Int

class Query(ObjectType):
  test = Int()

  def resolve_test(root, context, **kwargs):
    raise ValidationError("error message")

schema = Schema(query=Query)

or https://repl.it/repls/AffectionateObviousBookmark#cookbook/schema.py

For the following query:

query Test {
  test
}

We got the following response:

{
  "errors": [
    {
      "message": "['error message']",
      "locations": [
        {
          "line": 33,
          "column": 3
        }
      ],
      "path": [
        "test"
      ]
    }
  ],
  "data": {
    "test": null
  }
}

Look at the message field ...

{
  "errors": [
    {
      "message": "error message",
      "locations": [
        {
          "line": 33,
          "column": 3
        }
      ],
      "path": [
        "test"
      ]
    }
  ],
  "data": {
    "test": null
  }
}
zbyte64 commented 4 years ago

This is how exceptions are treated by default. The question I see is whether to treat ValidationError differently from other exceptions during the resolve phase. Aside from the message being different, I would think locations would be dropped.

One argument against this feature is that this isn't an explicit validation phase.