Utilizing Nestjs and GraphQL for backend development, encountered an error when defining a model class (code first): Schema must contain uniquely named types but contains multiple types named "Address". Below is the Reader model file example:
@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[];
}
This excerpt details the GraghQL configuration in the app module (code first)
GraphQLModule.forRoot({
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
cors: false,
sortSchema: true,
context: ({ req }) => ({ req }),
}),
Seeking guidance on resolving this issue. Your help is greatly appreciated!