javascript - Nest JS GraphQL “Cannot return null for non-nullable”

标签 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
}

有人有想法吗?

最佳答案

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

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

关于javascript - Nest JS GraphQL “Cannot return null for non-nullable”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58140891/

相关文章:

node.js - 当项目使用 CommonJS 模块时,create-react-app 将模块导入为 ES6 模块

javascript - AWS Lambda 在执行在 s3 存储桶中保存多个文件的 forEach 循环后返回 'null'

typescript - 折叠一个有区别的联合——从联合派生出一个包含所有可能的键值组合的伞型

javascript - 以更简单的方式获取树数据结构的深度

javascript - 将鼠标坐标从一种窗口大小转换为适合另一种窗口大小

javascript - 使用 google api 登录我的网站

javascript - 为什么在将 `maxlength` 属性设置为 `input` 时不能使用点表示法?

php - 多文件上传支持所有浏览器,特别是ie7,ie8和ie9

node.js - 想要弄清楚NodeJS应用程序结构(完整的JavaScript堆栈)

typescript - 在 src 目录中创建 React App (TypeScript) 自动生成 react-app-env.d.ts