【问题标题】:Nestjs Graphql the mutation/query input dto is emptyNestjs Graphql突变/查询输入dto为空
【发布时间】:2021-06-13 00:39:54
【问题描述】:

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

这是我的代码:

解析器

  @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
}

【问题讨论】:

  • 你认为指令语法无关紧要吗?

标签: graphql nestjs


【解决方案1】:

解决方法是在属性中添加@IsNotEmpty()@IsOptional()装饰器:

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

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

【讨论】:

    【解决方案2】:

    添加深度部分 https://gist.github.com/navix/6c25c15e0a2d3cd0e5bce999e0086fc9

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

    【讨论】:

      猜你喜欢
      • 2017-08-24
      • 2022-12-19
      • 2016-01-14
      • 2021-09-09
      • 2018-09-17
      • 2022-01-13
      • 2018-11-10
      • 2020-01-03
      • 2019-09-22
      相关资源
      最近更新 更多