简体   繁体   English

Nestjs定义GraphQL对象类型得到这个错误:架构必须包含唯一命名的类型但包含多个名为“地址”的类型

[英]Nestjs define GraphQL Object type got this error: Schema must contain uniquely named types but contains multiple types named "Address"

I use Nestjs & GraphQL for backend development, when I define model class (code first), I got following error: Schema must contain uniquely named types but contains multiple types named "Address".我使用 Nestjs 和 GraphQL 进行后端开发,当我定义模型类(代码优先)时,出现以下错误:架构必须包含唯一命名的类型,但包含多个名为“地址”的类型。 Following is my Reader model file:以下是我的 Reader 模型文件:

@ObjectType()
class Address {
  @Field()
  homeAddress: string;
  @Field()
  province: string;
  @Field()
  postcode: string;
}

@ObjectType()
class FavouriteBook {
  @Field()
  bookID: string;
  @Field()
  createDate: Date;
}

@ObjectType()
export class ReaderProfile {
  @Field()
  _id: string;
  @Field()
  firstName: string;
  @Field()
  lastName: string;
  @Field()
  gender: string;
  @Field()
  birthday: Date;
  @Field()
  address?: Address;
  @Field()
  postcode: string;
  @Field(type => Int)
  readTimes: number;
  @Field(type => Int)
  readDuration: number;
  @Field(type => Int)
  score: number;
  @Field()
  securityQuestion: string;
  @Field()
  securityAnswer: string;
}

@ObjectType()
export class ReaderReadHistory {
  @Field()
  _id: string;
  @Field()
  bookID: string;
  @Field(type => Int)
  currentPage: number;
  @Field()
  startTime: Date;
  @Field()
  lastReadTime: Date;
  @Field(type => Int)
  readTimes: number;
  @Field(type => Int)
  readDuration: number;
}

@ObjectType()
export class Reader {
  @Field()
  _id: string;
  @Field()
  username: string;
  @Field()
  password: string;
  @Field()
  email: string;
  @Field()
  registerDate: Date;
  @Field()
  isActive: boolean;
  @Field({ nullable: true })
  currentRefreshToken?: string;
  @Field(type => ReaderProfile)
  readerProfile: ReaderProfile;
  @Field(type => [FavouriteBook])
  favouriteBook: FavouriteBook[];
  @Field(type => [ReaderReadHistory])
  readHistory: ReaderReadHistory[];
}

And this is my GraghQL configuration in app module: (code first)这是我在 app 模块中的 GraghQL 配置:(代码优先)

GraphQLModule.forRoot({
      autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
      cors: false,
      sortSchema: true,
      context: ({ req }) => ({ req }),
    }),

Does anyone know how to fix this?有谁知道如何解决这一问题? Thanks a lot!非常感谢!

Eventually I did not find reason, and I changed coding to schema first which works well as expected.最终我没有找到原因,我首先将编码更改为模式,这符合预期。 Hopefully this help for those have same issue but can't fixed with code first approach, try schema first.希望这对那些有相同问题但无法使用代码优先方法修复的人有所帮助,请先尝试模式。 Also I found schema first approach has less boil-plates, especially you can use separate auto generate function (refer to following) to generate all the type script interfaces in one file, there is pretty helpful.此外,我发现模式优先方法具有较少的沸腾板,尤其是您可以使用单独的自动生成功能(请参阅下文)在一个文件中生成所有类型脚本接口,这非常有用。

import { GraphQLDefinitionsFactory } from '@nestjs/graphql';
import { join } from 'path';

const definitionsFactory = new GraphQLDefinitionsFactory();
definitionsFactory.generate({
  typePaths: ['./src/**/*.graphql'],
  path: join(process.cwd(), 'src/graphql.ts'),
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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