切换到 graphql 15.5.1 后返回空数组导致 "cannot return null value" 错误

After switchinng to graphql 15.5.1 returning an empty array results in a "cannot return null value" error

编辑:您可以跳到最后查看 codesandbox 中错误的简化示例

我正在将我的 apollo 项目升级到新版本的 graphql,但我遇到了一个奇怪的错误。在 google 上找不到与此场景匹配的任何结果。当我尝试 return 具有不可空字段(包括天)的 ObjectTypes DrivingActivitiesDailySummaryType 的空数组时,它会抛出“不能 return 不可空字段 DrivingActivitiesDailySummaryType.day 为空。”。这在版本 14.7.0 中按预期工作。这里发生了什么事?如何避免?我认为正常的行为是允许空数组,但我在变更日志中找不到任何指示此类更改的内容。

我的解析器目前只是 return 用于测试的硬编码值:

@Query(() => DrivingActivityPayload)
    public async totalDrivingActivity(
        @Ctx("user") userContext: UserContext,
        @Args() input: DrivingActivitiesQueryInput
    ): Promise<DrivingActivityPayload> {
        return {
            totalSummary: { speed: 1, activities: 1, duration: 1, distance: 1 },
            dailySummaries: [],
            monthlySummaries: [],
        }
    }

我的负载定义:

@ObjectType()
export class DrivingActivityPayload {
    @Field(() => DrivingActivitiesTotalSummaryType)
    public totalSummary: DrivingActivitiesTotalSummaryType

    @Field(() => DrivingActivitiesDailySummaryType)
    public dailySummaries: DrivingActivitiesDailySummaryType[]

    @Field(() => DrivingActivitiesMonthlySummaryType)
    public monthlySummaries: DrivingActivitiesMonthlySummaryType[]

    constructor(
        totalSummary: DrivingActivitiesTotalSummaryType,
        dailySummaries: DrivingActivitiesDailySummaryType[],
        monthlySummaries: DrivingActivitiesMonthlySummaryType[]
    ) {
        this.totalSummary = new DrivingActivitiesTotalSummaryType(totalSummary)
        this.dailySummaries = dailySummaries.map(summary => new DrivingActivitiesDailySummaryType(summary))
        this.monthlySummaries = monthlySummaries.map(summary => new DrivingActivitiesMonthlySummaryType(summary))
    }
}

GraphQL 错误:

  "errors": [
    {
      "message": "Cannot return null for non-nullable field DrivingActivitiesDailySummaryType.day.",
      "locations": [
        {
          "line": 18,
          "column": 7
        }
      ],
      "path": [
        "totalDrivingActivity",
        "dailySummaries",
        "day"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field DrivingActivitiesDailySummaryType.day.",
            "    at completeValue ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:559:13)",
            "    at resolveField ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:472:19)",
            "    at executeFields ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:292:18)",
            "    at collectAndExecuteSubfields ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:748:10)",
            "    at completeObjectValue ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:738:10)",
            "    at completeValue ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:590:12)",
            "    at completeValue ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:556:21)",
            "    at resolveField ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:472:19)",
            "    at executeFields ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:292:18)",
            "    at collectAndExecuteSubfields ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:748:10)",
            "    at completeObjectValue ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:738:10)",
            "    at completeValue ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:590:12)",
            "    at completeValue ([PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:556:21)",
            "    at [PROJECT-FOLDER]/node_modules/graphql/execution/execute.js:469:16",
            "    at processTicksAndRejections (node:internal/process/task_queues:96:5)",
            "    at async Promise.all (index 0)"
          ]
        }
      }
    }
  ],
  "data": null
}

DrivingActivitiesDailySummaryType 定义(虽然这不重要):

@ObjectType()
export class DrivingActivitiesDailySummaryType {
    @Field(() => Date)
    public day: Date

    @Field()
    public distance: number

    @Field()
    public activities: number

    constructor(summary: DrivingActivitiesDailySummaryType) {
        this.day = summary.day
        this.activities = summary.activities
        this.distance = summary.distance
    }
}

编辑:这是一个沙盒 link,其中重现了错误: https://codesandbox.io/s/heuristic-jepsen-5frr3?file=/package.json (我不知道如何更改默认终端行为,因此您必须使用“+”图标和 运行“npm start”打开一个新终端) 当使用提供的 package.json(新版本)时,错误被重现,当你用 package-old.json(旧版本的 graphql 和 type-graphql)切换 package.json 的内容时,有没有错误。

EDIT2:您可能需要将“/graphql”添加到 codesandbox link 才能正常工作

您应该将每日和每月摘要定义为数组:

@Field(() => [DrivingActivitiesDailySummaryType])
public dailySummaries: DrivingActivitiesDailySummaryType[]

@Field(() => [DrivingActivitiesMonthlySummaryType])
public monthlySummaries: DrivingActivitiesMonthlySummaryType[]