Nest JS GraphQL“不能为不可为空返回空值" [英] Nest JS GraphQL “Cannot return null for non-nullable”

查看:37
本文介绍了Nest JS GraphQL“不能为不可为空返回空值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to resolve one error in my study code, but failed. Then I just try to launch this code...

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
}

有人有想法吗?

推荐答案

这是最快的解决方案,只需启动即可.

This is the fastest fix, just to launch.

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

这篇关于Nest JS GraphQL“不能为不可为空返回空值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆