GraphQLError:CreateUserInputDTO输入对象需定义至少一个字段求助
问题:GraphQLError - Input Object type CreateUserInputDTO必须定义一个或多个字段
错误信息
GraphQLError: Input Object type CreateUserInputDTO must define one or more fields. at SchemaValidationContext.reportError
问题详情
已尝试过同类问题的解决方案但未解决,同类项目可正常运行,怀疑是使用了最新版本的相关模块导致错误。
相关代码
import { Field, InputType, IntersectionType, ObjectType, OmitType, } from '@nestjs/graphql'; import { BaseDTO } from 'src/shared/dto/base.dto'; @ObjectType({ isAbstract: true }) @InputType({ isAbstract: true }) export class UserBaseDTO { @Field() public email: string; @Field() public firstName: string; @Field() public lastName: string; @Field({ nullable: true }) public lastLoginTime: Date; @Field() public bio: string; @Field() public dob: Date; } @ObjectType() export class UserDTO extends IntersectionType( UserBaseDTO, BaseDTO, ObjectType, ) {} // 错误出在这里,手动添加属性后可正常运行 @InputType() export class CreateUserInputDTO extends OmitType(UserBaseDTO, [ 'lastLoginTime', ]) {}
项目依赖(package.json)
{"name": "server","version": "0.0.1","description": "","author": "","private": true,"license": "UNLICENSED","scripts": {"prebuild": "rimraf dist","build": "nest build","format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"","start": "nest start","start:dev": "nest start --watch","start:debug": "nest start --debug --watch","start:dev:db": "./scripts/start-db.sh","start:prod": "node dist/main","lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix","test": "jest","test:watch": "jest --watch","test:cov": "jest --coverage","test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand","test:e2e": "jest --config ./test/jest-e2e.json","typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -d src/config/ormconfigs.ts","migration:create": "cross-env ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli migration:create ./src/migrations/$npm_config_name","migration:generate": "cross-env npm run typeorm -- migration:generate ./src/migrations/$npm_config_name","migration:run": "npm run build && npm run typeorm -- migration:run","migration:revert": "npm run typeorm -- migration:revert"},"dependencies": {"@apollo/gateway": "^2.1.1","@apollo/subgraph": "^2.1.1","@nestjs/apollo": "^10.1.0","@nestjs/common": "^9.0.11","@nestjs/config": "^2.2.0","@nestjs/core": "^9.0.11","@nestjs/graphql": "^10.1.1","@nestjs/platform-express": "^9.0.11","@nestjs/swagger": "^6.1.2","@nestjs/typeorm": "^9.0.1","apollo-server-express": "^3.10.2","axios": "^0.27.2","cross-env": "^7.0.3","dotenv": "^16.0.2","graphql": "^16.6.0","graphql-tools": "^8.3.6","mysql2": "^2.3.3","reflect-metadata": "^0.1.13","rimraf": "^3.0.2","rxjs": "^7.5.6","swagger-ui-express": "^4.5.0","ts-morph": "^16.0.0","typeorm": "^0.3.9"},"devDependencies": {"@nestjs/cli": "^9.1.3","@nestjs/schematics": "^9.0.3","@nestjs/testing": "^9.0.11","@types/express": "^4.17.14","@types/jest": "^29.0.2","@types/node": "^18.7.18","@types/supertest": "^2.0.12","@typescript-eslint/eslint-plugin": "^5.37.0","@typescript-eslint/parser": "^5.37.0","eslint": "^8.23.1","eslint-config-prettier": "^8.5.0","eslint-plugin-prettier": "^4.2.1","jest": "^29.0.3","prettier": "^2.7.1","supertest": "^6.2.4","ts-jest": "^29.0.1","ts-loader": "^9.3.1","ts-node": "^10.9.1","tsconfig-paths": "^4.1.0","typescript": "^4.8.3","yarn-upgrade-all": "^0.7.1"},"jest": {"moduleFileExtensions": ["js","json","ts"],"rootDir": "src","testRegex": ".*\\.spec\\.ts$","transform": {"^.+\\.(t|j)s$": "ts-jest"},"collectCoverageFrom": ["**/*.(t|j)s"],"coverageDirectory": "../coverage","testEnvironment": "node"}}
解决方案
问题根源在于@nestjs/graphql v10+版本中,使用OmitType处理同时标记为@ObjectType和@InputType的抽象类时,需要显式指定要继承的类型类型。
修改CreateUserInputDTO的定义,添加第三个参数指定为InputType:
@InputType() export class CreateUserInputDTO extends OmitType(UserBaseDTO, ['lastLoginTime'], InputType) {}
原因解释
你的UserBaseDTO同时被装饰为@ObjectType和@InputType(均为抽象类),旧版本的NestJS GraphQL可能会默认推断要继承的类型,但新版本必须明确指定,否则OmitType会错误地处理成ObjectType的字段逻辑,导致生成的CreateUserInputDTO作为输入类型时没有可用字段,触发GraphQL的 schema 验证错误。
内容的提问来源于stack exchange,提问作者theboyonfire
相关产品推荐
相关产品推荐

