首页 > 解决方案 > Nestjs Graphql突变/查询输入dto为空

问题描述

我不知道我错过了什么,但是当我调试时,突变或查询的输入总是空的,似乎输入是空对象

这是我的代码:

解析器

  @Query(() => String)
  async testMutation(@Args('args') args: UpvotePostInput) {
    return args.postId;
  }

dto

import { InputType, Field } from '@nestjs/graphql';

@InputType()
export class UpvotePostInput {
  @Field(() => String)
  postId: string;
}

空对象 在此处输入图像描述

错误

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.testMutation.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "testMutation"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Query.testMutation.",
            "    at completeValue (/....js:559:13)",
            "    at /.....xecute.js:469:16",
            "    at processTicksAndRejections (internal/process/task_queues.js:97:5)",
            "    at async Promise.all (index 0)"
          ]
        }
      }
    }
  ],
  "data": null
}

在此处输入图像描述

标签: graphqlnestjs

解决方案


解决方案是向属性添加@IsNotEmpty()@IsOptional()装饰器:

@InputType()
export class UpdateCarInput extends PartialType(CreateCarInput) {
    @Field(() => Int)
    @IsNotEmpty()
    id: number;
}

很奇怪,但这解决了问题。


推荐阅读