graphql - 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
}
解决方案
解决方案是向属性添加@IsNotEmpty()
或@IsOptional()
装饰器:
@InputType()
export class UpdateCarInput extends PartialType(CreateCarInput) {
@Field(() => Int)
@IsNotEmpty()
id: number;
}
很奇怪,但这解决了问题。
推荐阅读
- kubernetes - oc 和 kubectl 命令有什么区别?
- reactjs - React State Hook - 切换一个类
- laravel - 如何使用 laravel app 和 twilio 配置 whatapp 的聊天
- composer-php - 如何修复“https://repo.packagist.org/packages.json 不包含有效的 Json”?
- javascript - 如何在 Laravel 中使用 Ajax 调用基于其他下拉菜单添加下拉菜单?
- powershell - 错误后powershell脚本无法继续。已经在代码中无声地继续
- ionic-framework - 有没有办法使用来自 Snapkit 登录 Web api 的访问令牌获取用户 Bitmoji?
- firebase-hosting - Firebase 托管 - 如何将域重定向到 url(.pl 到 .com/pl)
- typescript - SyntaxError:意外的令牌,预期的“;” 使用 Prettier 的 .css 文件
- javascript - 如何在 Tensorflow.js 中计算拉普拉斯算子?