Home > other >  Can't use extensions with GraphQLError
Can't use extensions with GraphQLError

Time:10-18

From the Apollo Graphql document, this way can define extension error:

https://www.apollographql.com/docs/apollo-server/data/errors/

import { GraphQLError } from 'graphql';

throw new GraphQLError('the error message', 
  extensions: {
    code: 'SOMETHING_BAD_HAPPENED',
    http: {
      status: 404,
      headers: new Map([
        ['some-header', 'it was bad'],
        ['another-header', 'seriously'],
      ]),
    },
  },
);

But in my case it got this error:

Argument of type '{ extensions: { code: string; http: { status: number; headers: Map<string, string>; }; }; }' is not assignable to parameter of type 'Maybe<ASTNode | readonly ASTNode[]>'.
  Object literal may only specify known properties, and 'extensions' does not exist in type 'ASTNode | readonly ASTNode[]'.

 23         extensions: {
            ~~~~~~~~~~~~~
 24           code: 'SOMETHING_BAD_HAPPENED',
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
 31           },
    ~~~~~~~~~~~~
 32         },
    ~~~~~~~~~

Found 1 error(s).

I'm using these packages:

  • "apollo-server-core": "^3.7.0",
  • "apollo-server-express": "^3.4.0",
  • "apollo-server-plugin-base": "^0.13.0",

I also tried to install apollo-server-testing but still can't use extensions.

CodePudding user response:

This way works:

  throw new GraphQLError('the error message',
    null,
    null,
    null,
    null,
    null,
    {
      code: 'SOMETHING_BAD_HAPPENED',
      http: {
        status: 404,
      },
    },
  );
  • Related