簡體   English   中英

NestJs - Graphql 錯誤無法確定 GraphQL 輸入類型<field> . 確保您的 class 已使用合適的裝飾器進行裝飾</field>

[英]NestJs - Graphql Error Cannot determine a GraphQL input type for the <Field>. Make sure your class is decorated with an appropriate decorator

我目前正在按照本文檔https://docs.nestjs.com/graphql/resolvers使用 Nest、mongoDB 和 GraphQl 開發博客應用程序,並使用映射類型https://docs.nestjs.com/graphql/mapped構建我的 DTO -類型。 . 但是我遇到錯誤

Error: Cannot determine a GraphQL input type ("UserDTO") for the "author". Make sure your class is decorated with an appropriate decorator.
    at InputTypeFactory.create

這是post.dto.ts文件

import {
  Field,
  InputType,
  IntersectionType,
  ObjectType,
  PartialType,
} from '@nestjs/graphql';
import { BaseDTO } from 'src/shared/dto/base.dto';
import { PagedResultDTO } from 'src/shared/dto/pagedResult.dto';
import { UserDTO } from '../user/user.dto';

@ObjectType({ isAbstract: true })
export class PostBaseDTO {
  @Field()
  title: string;

  @Field()
  description: string;

  @Field()
  headerPhoto: string;

  @Field()
  content: string;

  @Field()
  komootId: string;

  @Field()
  date: Date;

  @Field(() => [String])
  keyword: string[];

  @Field(() => UserDTO, { nullable: true }) //THE ERROR IS HERE, when passing "UserDTO" as the type
  author?: UserDTO;
}

@ObjectType()
export class PostDTO extends IntersectionType(
  PostBaseDTO,
  BaseDTO,
  ObjectType,
) {}

@InputType()
export class CreatePostInputDTO extends IntersectionType(
  PostBaseDTO,
  BaseDTO,
  InputType,
) {}

@InputType()
export class UpdatePostInputDTO extends PartialType(PostDTO, InputType) {}

@ObjectType()
export class PostsPagedResultDTO extends PagedResultDTO<PostDTO> {
  @Field(() => [PostDTO])
  public records: PostDTO[];
}

user.dto.ts文件

import {
  Field,
  InputType,
  IntersectionType,
  ObjectType,
  OmitType,
  PartialType,
} from '@nestjs/graphql';
import { UserRole } from 'src/schemas/userRole.schema';
import { BaseDTO } from 'src/shared/dto/base.dto';
import { PagedResultDTO } from 'src/shared/dto/pagedResult.dto';

@ObjectType({ isAbstract: true })
export class UserBaseDTO {
  @Field()
  public email: string;

  @Field()
  public username: string;

  @Field()
  public password: string;

  @Field(() => String, { nullable: true })
  public role: UserRole;
}

@ObjectType()
export class UserDTO extends IntersectionType(
  UserBaseDTO,
  BaseDTO,
  ObjectType,
) {}

@InputType()
export class CreateUserInputDTO extends OmitType(
  UserBaseDTO,
  ['role'] as const, 
  InputType,
) {}

@InputType()
export class UpdateUserInputDTO extends PartialType(UserDTO, InputType) {}

@ObjectType()
export class UsersPagedResultDTO extends PagedResultDTO<UserDTO> {
  @Field(() => [UserDTO])
  public records: UserDTO[];
}


然后我嘗試在user.dto.ts文件中實現它

@InputType()
export class UserDTOInput extends IntersectionType(
  UserBaseDTO,
  BaseDTO,
  InputType,
) {}

並將其作為author類型傳遞給post.dto.ts ,但仍然會拋出錯誤...

任何幫助表示贊賞。 謝謝:)

好的,所以我終於修好了。 我將關系字段(在本例中為author字段)放入不同類型的PostDTO (ObjectType) 和CreatePostInputDTO (inputType)

@ObjectType()
export class PostBaseDTO {
  @Field()
  title: string;

  @Field()
  description: string;

  @Field()
  headerPhoto: string;

  @Field()
  content: string;

  @Field()
  komootId: string;

  @Field()
  date: Date;

  @Field(() => [String])
  keyword: string[];
}

@ObjectType()
export class PostDTO extends IntersectionType(
  PostBaseDTO,
  BaseDTO,
  ObjectType,
) {
  @Field(() => UserDTO, { nullable: true })
  author?: UserDTO; //will be called via resolveField
}

@InputType()
export class CreatePostInputDTO extends IntersectionType(
  PostBaseDTO,
  BaseDTO,
  InputType,
) {
  @Field(() => String) // to store the objectID string
  author: MongooseSchema.Types.ObjectId
}

但是,我仍然不確定這種方法是否好。 至少現在,它正在工作:D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM