无法将扩展与 GraphQLError 一起使用

问题描述 投票:0回答:3

从Apollo Graphql文档来看,这种方式可以定义扩展错误:

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'],
          ]),
        },
      },
    );

但就我而言,它出现了这个错误:

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).

我正在使用这些软件包:

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

我也尝试安装

apollo-server-testing
但仍然无法使用扩展。

exception error-handling graphql apollo-server
3个回答
1
投票

您忘记用对象封装第二个参数。

{ extensions }

这样做:

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

此外,@Alpin Cleopatra 答案由于构造函数重载而有效,但并不打算以这种方式使用。

请参阅此处的两个签名:


0
投票

这种方法有效:

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

0
投票

出于某种原因,我必须坚持

graphql@15
,根据上面的答案,我创建了一个扩展的 GraphQLError 类:

// 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
    );
  }
}

所以我用这个而不是

graphql
中的那个。

还请注意,我必须将

originalError
放入
extensions
对象中, 如果
graphql-yoga
不存在,
originalError
会抛出错误。

© www.soinside.com 2019 - 2024. All rights reserved.