GraphQL disable input validation, add custom error

1.3k Views Asked by At

Trying to disable explicit message on input validation errors. When I request a field that does not exist (i.e age) i get:

{
"data": null,
"errors": [{
        "path": null,
        "locations": [
            {
                "line": 1,
                "column": 129,
                "sourceName": null
            }
        ],
        "message": "Validation error of type FieldUndefined: Field 'age' in type 'Patient' is undefined @ 'path/age'"
    }]}

I want to override the error to show a custom error message:

    {
"data": null,
"errors": [
    {
        "message": "invalid fields"
    }
]}

sample of my schema with custom SystemError (does not work):

type Patient {
  id: Int!
  firstName: String
  lastName: String
}

type Query {
  patient(id: Int!): Patient
  errors: [SystemError]
}

type SystemError {
  id: Int!
  message: String
}

schema {
  query: Query
}
1

There are 1 best solutions below

0
xadm On

You can try to use a middleware, f.e. middy (some googled appsync middleware), https://github.com/bboure/middy-appsync#error-handling

To limit error info you can try (not tested) to use something like this:

const doStuff = (event, context, callback) => {
  if( context.result && context.result.errorMessage ) {
    callback(new GraphQlError('Error'));
  } else {
    callback(null, context.result.data);
  }
};