NestJS/GraphQL-无法确定";的GraphQL输入类型;雇主“;
在我的NestJSAPI中,我遇到了以下错误,因为我在NestJS/GraphQL-无法确定";的GraphQL输入类型;雇主“;,graphql,nestjs,typeorm,Graphql,Nestjs,Typeorm,在我的NestJSAPI中,我遇到了以下错误,因为我在CandidateService中使用了EmployeerService Error: Nest can't resolve dependencies of the CandidateService (CandidateRepository, AddressRepository, ProvinceRepository, ?). Please make sure that the argument EmployerService at inde
CandidateService
中使用了EmployeerService
Error: Nest can't resolve dependencies of the CandidateService (CandidateRepository, AddressRepository, ProvinceRepository, ?). Please make sure that the argument EmployerService at index [3] is available in the CandidateModule context.
Potential solutions:
- If EmployerService is a provider, is it part of the current CandidateModule?
- If EmployerService is exported from a separate @Module, is that module imported within CandidateModule?
@Module({
imports: [ /* the Module containing EmployerService */ ]
})
我通过将EmployerModule
添加到CandidateModule
导入中解决了这个问题,如下所示
@Module({
imports: [
EmployerModule,
TypeOrmModule.forFeature([Candidate, Address, Province]),
],
providers: [CandidateResolver, CandidateService],
exports: [CandidateService, TypeOrmModule],
})
export class CandidateModule {}
这似乎修复了以前的错误,但也创建了以下错误
UnhandledPromiseRejectionWarning: Error: Cannot determine a GraphQL input type for the "employer". Make sure your class is decorated with an appropriate decorator.
at InputTypeFactory.create (/Users/amed/Documents/Projects/jnoble/packages/api/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type.factory.js:19:23)
at /Users/amed/Documents/Projects/jnoble/packages/api/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:44:52
at Array.forEach (<anonymous>)
at /Users/amed/Documents/Projects/jnoble/packages/api/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:42:33
at resolveThunk (/Users/amed/Documents/Projects/jnoble/packages/api/node_modules/graphql/type/definition.js:480:40)
at defineInputFieldMap (/Users/amed/Documents/Projects/jnoble/packages/api/node_modules/graphql/type/definition.js:1207:18)
at GraphQLInputObjectType.getFields (/Users/amed/Documents/Projects/jnoble/packages/api/node_modules/graphql/type/definition.js:1155:27)
at collectReferencedTypes (/Users/amed/Documents/Projects/jnoble/packages/api/node_modules/graphql/type/schema.js:376:81)
at collectReferencedTypes (/Users/amed/Documents/Projects/jnoble/packages/api/node_modules/graphql/type/schema.js:372:11)
at new GraphQLSchema (/Users/amed/Documents/Projects/jnoble/packages/api/node_modules/graphql/type/schema.js:157:7)
雇主输入
@InputType()
export class EmployerInput {
@Field()
name: string;
@Field()
email: string;
@Field()
phone: string;
@Field()
industry: string;
}
雇主.resolver.ts
@Resolver()
export class EmployerResolver {
constructor(private readonly employerService: EmployerService) {}
@Query(() => [Employer], { nullable: true })
async allEmployers(): Promise<Employer[]> {
return await this.employerService.findAll();
}
@Query(() => [Employer], { nullable: true })
async employerByName(@Args('input') input: string): Promise<Employer[]> {
return await this.employerService.findByName(input);
}
@Query(() => [Employer], { nullable: true })
async employerByEmail(@Args('input') input: string): Promise<Employer> {
return await this.employerService.findByEmail(input);
}
@Query(() => [Employer], { nullable: true })
async employerById(
@Args('id', { type: () => Int }) id: number,
): Promise<Employer> {
return await this.employerService.findById(id);
}
@Mutation(() => Employer)
async addEmployer(
@Args('sub') sub: SubUserInput,
@Args('employer')
employer: EmployerInput,
@Args('address') address: AddressInput,
@Args('province') province: ProvinceInput,
): Promise<Employer> {
return await this.employerService.addEmployer(
sub,
employer,
address,
province,
);
}
@Mutation(() => Employer)
async updateEmployer(
@Args('id', { type: () => Int }) id: number,
@Args('input')
input: EmployerUpdateInput,
@Args('province', { nullable: true }) province: ProvinceInput,
): Promise<Employer> {
return await this.employerService.updateEmployer(id, input, province);
}
}
@Resolver()
导出类EmployerResolver{
构造函数(私有只读employerService:employerService){}
@查询(()=>[Employer],{nullable:true})
异步allEmployers():承诺{
返回并等待。employeerservice.findAll();
}
@查询(()=>[Employer],{nullable:true})
异步employerByName(@Args('input')输入:字符串):承诺{
返回等待this.employerService.findByName(输入);
}
@查询(()=>[Employer],{nullable:true})
异步EmployerByMail(@Args('input')输入:字符串):承诺{
返回等待this.employeerservice.findByEmail(输入);
}
@查询(()=>[Employer],{nullable:true})
异步EmployerByd(
@Args('id',{type:()=>Int})id:number,
):承诺{
返回等待此。employeerservice.findById(id);
}
@突变(()=>雇主)
异步加法器(
@Args('sub')sub:SubUserInput,
@Args(“雇主”)
雇主:雇主,
@Args(“地址”)地址:AddressInput,
@Args(“省”)省:省输入,
):承诺{
返回等待此。employerService.addEmployer(
附属的,
雇主,
地址:,
省,,
);
}
@突变(()=>雇主)
异步更新雇员(
@Args('id',{type:()=>Int})id:number,
@Args('输入')
输入:EmployeeUpdate输入,
@Args('province',{nullable:true})province:ProvinceInput,
):承诺{
返回等待此.employerService.updateEmployer(id,输入,省);
}
}
任何对此有见解的人,请帮助。谢谢。由于Jay的评论,我发现了这个错误 候选输入
@InputType()
export class CandidateInput {
@Field()
firstName: string;
@Field()
lastName: string;
@Field({ nullable: true })
middleName: string;
@Field({ nullable: true })
preferredName: string;
@Field()
dateOfBirth: string;
@Field()
jobTitle: string;
@Field()
phone: string;
@Field()
email: string;
@Field({ nullable: true })
password?: string;
@Field({ nullable: true })
employer: Employer; // This is where the error originates
@Field(() => [String])
languages: string[];
@Field(() => [String])
skills: string[];
@Field({ nullable: true })
validDriversLicense: boolean;
@Field({ nullable: true })
ownVehicle: boolean;
@Field()
statusInCanada: string;
@Field()
available: boolean;
}
我将有问题的字段改为
@Field(() => Int, { nullable: true })
employerId?: number;
这就解决了问题。你能给我看一下使用
雇主的解决方案吗?从错误中可以看出,您试图将输入类型设置为Employer
而不是employeerinput
@JayMcDoniel我已经添加了雇主解决方案我的候选输入@字段({nullable:true})Employer?:Employer代码>这可能就是错误所在。我打赌应该是employeerinput
type
@Field(() => Int, { nullable: true })
employerId?: number;