Skip to content

Document how to customize global, request validation errors that prevent execution #487

Closed
@christianrowlands

Description

@christianrowlands

I apologize if I missed it, but I did a scrub of the documentation and I could not find anything about how to handle returning custom responses for request that don't pass schema validation.

Right now, I am seeing something like the following returned from this framework:

{
    "errors": [
        {
            "message": "Variable 'myVariable' has an invalid value: Field 'name' has coerced Null value for NonNull type 'String!'",
            "locations": [
                {
                    "line": 1,
                    "column": 21
                }
            ],
            "extensions": {
                "classification": "ValidationError"
            }
        }
    ]
}

This does provide the relevant information for someone experienced with GraphQL because it mentioned the field name is a String!. However, I would like to return a simplified message such as "The name field is required in the MyInputObject when making the mutation someMutationCall", or something like that.

I poked around the code base a bit and found that the org.springframework.graphql.execution.DefaultExecutionGraphQlService class has an execute method. If I create my own implementation and register it as a bean I see that the execute method is called and there I can transform the above error into something more beginner developer friendly.

There are two problems with the above approach. First, the DefaultExecutionGraphQlService#execute method is final, so I can't just override it, I have to create a whole new custom implementation of the interface. Second, I don't really like handling this in the execute method. Instead, I really like the approach that was taken with the DataFetcherExceptionResolverAdapter where we can just override the com.opengov.coa.definition.adapter.http.errors.CoaDefinitionErrorHandler#resolveToMultipleErrors method. It would be great if there was something similar to that for validation of the request.

I did find this existing ticket, but it is a bit different.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions