【问题标题】:Nest JS GraphQL “Cannot return null for non-nullable” [duplicate]Nest JS GraphQL“不能为非空返回null” [重复]
【发布时间】:2020-01-28 03:42:32
【问题描述】:

我尝试解决学习代码中的一个错误,但失败了。然后我只是尝试启动此代码...

https://github.com/nestjs/nest/tree/master/sample/23-type-graphql

同样的情况……

错误看起来像

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Recipe.id.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "recipe",
        "id"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Recipe.id.",
            "    at completeValue (/home/innistry/Downloads/nest-master/sample/23-type-graphql/node_modules/graphql/execution/execute.js:560:13)",
            "    at /home/innistry/Downloads/nest-master/sample/23-type-graphql/node_modules/graphql/execution/execute.js:492:16",
            "    at process._tickCallback (internal/process/next_tick.js:68:7)"
          ]
        }
      }
    }
  ],
  "data": null
}

有人有想法吗?

【问题讨论】:

  • 如果您遇到同样的问题并像我一样访问了此页面,我在此页面上添加了答案。 stackoverflow.com/questions/56319137/…我无法在此页面上添加答案,因为此问题被标记为重复问题。

标签: javascript node.js typescript graphql nestjs


【解决方案1】:

这是最快的修复,刚刚启动。

import { Field, ID, ObjectType } from 'type-graphql';

@ObjectType()
export class Recipe {
  @Field(type => ID, { nullable: true })
  id?: string;

  @Field({ nullable: true })
  title?: string;

  @Field({ nullable: true })
  description?: string;

  @Field({ nullable: true })
  creationDate?: Date;

  @Field(type => [String], { nullable: true })
  ingredients?: string[];
}

【讨论】:

  • 不是解决问题的好方法。应该有意使该字段为空,而不是绕过错误。
  • 我收到了完全相同的错误消息。发生这种情况是因为我在测试中使用了部分夹具,而我没有填写实体中的所有必要字段。人们应该寻找这种滥用而不是这种快速修复解决方法。
猜你喜欢
  • 2017-07-13
  • 2018-04-10
  • 2023-04-08
  • 2019-10-25
  • 2019-05-28
  • 2017-03-13
  • 2019-06-24
  • 2019-07-08
  • 2018-01-28
相关资源
最近更新 更多