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.

How to customise mutation validation error messages

See original GitHub issue

Hi,

I’ve been trying to customise mutation validation errors but so far didn’t succeed in doing so, having the following query I would like to be able to return errors if different shape

mutation CreateProject($projectName: String!, $description: String!) {
  createProject(projectName: $projectName, description: $description) {
    id
    projectName
    description
  }
}

Standard errors look like

{
  "errors": [
    {
      "message": "Variable \"$description\" of required type \"String!\" was not provided.",
      "locations": [
        {
          "line": 1,
          "column": 47
        }
      ]
    }
  ]
}

What I would like to have is

{
  "errors": [
    {
        "$description": "Error message...",
        "locations": [...]
    }
}

So is it possible to adjust error response format?

Thanks.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
KingDarBojacommented, Feb 6, 2020

Looking forward for the doc section regarding customization of error messages.

3reactions
sultanimancommented, Nov 1, 2018

So I managed to make it and here is my solution

Custom view

from typing import Any, Dict

from graphene_django.views import GraphQLView
from graphql.error import GraphQLError

from graph.format_error import format_error


class MyGraphQLView(GraphQLView):
    @staticmethod
    def format_error(error) -> Dict[str, Any]:
        if isinstance(error, GraphQLError):
            return format_error(error)

        return GraphQLView.format_error(error)

Error formatter

from typing import Dict, Any

from graphql import GraphQLError


def format_error(error: GraphQLError) -> Dict[str, Any]:
    """Extract field from ``error`` and return formatted error
    :param error: GraphQLError
    :return: mapping of fieldName -> error message
    """
    formatted_error = {
        e.variable.name.value: str(e)
        for e in error.nodes
    }

    if error.path:
        formatted_error["path"] = error.path

    return formatted_error
Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation and User Errors in GraphQL Mutations
The mutation code above validates the input by using validator.js library, builds an array of error messages and throws a custom error before...
Read more >
Writing custom validations - Drupal Graphql - GitBook
Writing custom validations. One aspect that is important to consider when creating mutations is providing good error messages and validations.
Read more >
Is it possible customize Hasura mutation/query Error messages?
My question is, is it possible to configure this error message? Because here, the role don't have the permission to do mutation. So...
Read more >
Error handling - Apollo GraphQL Docs
Custom errors. You can create a custom errors and codes using the graphql package's GraphQLError class, like so: TypeScript.
Read more >
Error messaging — Ariadne 0.1 documentation
Your first instinct when planning error messaging may be to use this approach to communicate custom errors (like permission or validation errors) raised...
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