Nest JS GraphQL“不能为不可为空返回空值”

inn*_*try 1 javascript node.js typescript graphql nestjs

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

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
}
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

inn*_*try 5

这是最快的修复,只需启动即可。

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[];
}

Run Code Online (Sandbox Code Playgroud)

  • 不是一个好的办法来解决。应该有意使该字段可为空,而不是为了绕过错误。 (3认同)