Can’t use extensions with GraphQLError

Can’t use extensions with GraphQLError


3

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',
        https: {
          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; https: { 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.

3 Answers
3


1

You forgot to encapsulate the second argument with an object. { extensions }

Do this:

throw new GraphQLError('the error message', 
{ // curly bracket here to open the object
  extensions: {
    code: 'SOMETHING_BAD_HAPPENED',
    https: {
      status: 404,
      headers: new Map([
        ['some-header', 'it was bad'],
        ['another-header', 'seriously'],
      ]),
    },
  },
} // curly bracket here to close the object
);

Also, @Alpin Cleopatra answer works because of the constructor overload, but it’s not intended to be used this way.

See the two signatures here:

Can't use extensions with GraphQLError

1

  • 3

    I tried the code above (which is the also the same as the code in apollo-server documentation) but I'm getting the same error mentioned in this post. Only the comment below worked with the null options and I don't think it's okay to use it. Any suggestions please?

    – Kit A.

    Dec 7, 2022 at 16:26


0

This way works:

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

2

  • Make sure you're using graphql >= 16. I forgot to run yarn install, so I had v15 installed, which is why I was seeing this error.

    – twiz

    Feb 1 at 17:27

  • that is some unpleasing code but it actually works LOL

    – aryankarim

    Feb 21 at 20:24


0

For some reason, I have to stick with graphql@15, with the answers above, I create an extended GraphQLError class:

// backend/errors/GraphQLError.ts

import { ASTNode, GraphQLError as OriginalGraphQLError, Source } from "graphql";
import { Maybe } from "type-graphql";

type ErrorOptions = {
  nodes?: Maybe<ReadonlyArray<ASTNode> | ASTNode>;
  source?: Maybe<Source>;
  positions?: Maybe<ReadonlyArray<number>>;
  path?: Maybe<ReadonlyArray<string | number>>;
  originalError?: Maybe<Error>;
  extensions?: Maybe<{ [key: string]: any }>;
};

export class GraphQLError extends OriginalGraphQLError {
  constructor(message: string, options?: ErrorOptions) {
    const extensions = {
      ...(options?.originalError && {
        originalError: options?.originalError,
      }),
      ...(options?.extensions && {
        ...options?.extensions,
      }),
    };
    super(
      message,
      options?.nodes,
      options?.source,
      options?.positions,
      options?.path,
      options?.originalError,
      extensions
    );
  }
}

So I use this instead of the one from graphql.

Also notice that I have to put originalError into extensions object,
graphql-yoga throws an error if originalError is not existed.

Usage:


// SomewhereResolver.ts

async destroyDbMutation(){
   try{
      const ok = await businessRequirement.destroyDB();
      return ok;
   }catch(error:any){
      throw new GraphQLError(error.message, { originalError: error });
   }
}




Leave a Reply

Your email address will not be published. Required fields are marked *