kriasoft/graphql-starter-kit

ValidationError always returning `The request is invalid` instead of custom message

nizarayari opened this issue · 2 comments

Hi there, thanks for this great boilerplate. I have a question regarding ValidationError class. It always returns The request is invalid. even if I pass a custom error message like so:

const { data, errors } = validate(input, ctx);

 errors.push({
   key: 'code',
   message: 'Invalid code',
});

if (errors.length) {
  throw new ValidationError(errors);
}

It returns the default message "The request is invalid." instead of Invalid code

Here is the error printed in the console

{ Error: The request is invalid.
    at new CombinedError (/usr/src/app/node_modules/graphql-tools/src/stitching/errors.ts:85:5)
    at Object.checkResultAndHandleErrors (/usr/src/app/node_modules/graphql-tools/src/stitching/errors.ts:107:11)
    at CheckResultAndHandleErrors.transformResult (/usr/src/app/node_modules/graphql-tools/src/transforms/CheckResultAndHandleErrors.ts:15:12)
    at /usr/src/app/node_modules/graphql-tools/src/transforms/transforms.ts:37:45
    at Array.reduce (<anonymous>)
    at applyResultTransforms (/usr/src/app/node_modules/graphql-tools/src/transforms/transforms.ts:35:21)
    at /usr/src/app/node_modules/graphql-tools/src/stitching/delegateToSchema.ts:81:12
    at step (/usr/src/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:32:23)
    at Object.f [as next] (/usr/src/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:13:53)
    at fulfilled (/usr/src/app/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:4:58)
    at <anonymous>
  errors:
   [ { Error: The request is invalid.
    at mutateAndGetPayload (/usr/src/app/src/schema/user/mutations.js:99:15)
    at <anonymous>
       message: 'The request is invalid.',
       locations: [],
       path: [Array] } ] }

Nothing to do with the boilerplate code. It was due to schema stitching swallowing errors. I had to tweak my formatError function to match the new mergedSchema

        formatError: (error: any) => {
          const { originalError } = error.originalError.errors[0];
          return {
            message: error.message,
            code: originalError && originalError.code,
            state: originalError && originalError.state,
            locations: error.locations,
            path: error.path,
          };
        },