简体   繁体   中英

GraphQLError: Input Object type must define one or more fields

I have this error

GraphQLError: Input Object type CreateUserInputDTO must define one or more fields.
      at SchemaValidationContext.reportError

I have tried some suggestions, but still doesnt work Input Object type `TypeName` must define one or more fields

I am not sure why am having this error because the other project I had works. I suspect that probably I use the latest modules?

Here is the code

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,
) {}

//the error is here, if I add properties it will work
@InputType()
export class CreateUserInputDTO extends OmitType(UserBaseDTO, [
  'lastLoginTime',
]) {}

this is the dependency in the 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"
  }
}

Thanks

PickType , OmitType and PartialType take the fields of the base class ( UserBaseDTO here) according to its own decorators by default. Since the base class is purely abstract, this leads to an input with no fields.

The solution is to pass the target decorator property when mapping abstract types:

@InputType()
export class CreateUserInputDTO extends OmitType(
  UserBaseDTO,
  ['lastLoginTime'] as const,  // by the way, `as const` generates better TypeScript types; has no effect on the GraphQL generated types though
  InputType                    // now this is the decorator that will be applied to `UserBaseDTO` when mapping this type
) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM