【问题标题】:NestJS/GraphQL - Cannot determine a GraphQL input type for the "employer"NestJS/GraphQL - 无法确定“雇主”的 GraphQL 输入类型
【发布时间】:2021-05-30 12:00:42
【问题描述】:

在我的 NestJs API 中,我收到以下错误,因为我在 CandidateService 中使用了 EmployerService

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)

我无法弄清楚为什么会出现此错误。你可以看到我的实体和@nestjs/graphql 输入如下所示。

employer.entity.ts

@ObjectType()
@Entity()
export class Employer {
  @PrimaryGeneratedColumn()
  @Field(() => Int)
  id: number;

  @Column()
  @Field()
  name: string;

  @Column({ unique: true })
  @Field()
  email: string;

  @Column()
  @Field()
  phone: string;

  @Column()
  @Field()
  industry: string;

  @OneToOne(() => Address, address => address.employer, {
    cascade: ['insert', 'update'],
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'address_id' })
  @Field(() => Address)
  address: Address;

  @OneToMany(() => Candidate, candidate => candidate.employer, {
    cascade: ['insert', 'update'],
    onDelete: 'CASCADE',
    nullable: true,
  })
  @JoinColumn({ name: 'employees_id' })
  @Field(() => [Candidate], { nullable: true })
  employees?: Candidate[];

  @OneToMany(() => SubUser, sub => sub.employer)
  @JoinColumn({ name: 'sub_id' })
  @Field(() => [SubUser])
  sub: SubUser[];

  @CreateDateColumn({ name: 'created_at' })
  @Field()
  createdAt: string;

  @UpdateDateColumn({ name: 'updated_at' })
  @Field()
  updatedAt: string;
}

employer.input.ts

@InputType()
export class EmployerInput {
  @Field()
  name: string;

  @Field()
  email: string;

  @Field()
  phone: string;

  @Field()
  industry: string;
}

employer.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);
  }
}

任何对此有一些见解的人,请提供帮助。谢谢。

【问题讨论】:

  • 你能显示使用employer的解析器吗?从错误来看,您似乎正在尝试将输入类型设为 Employer 而不是 EmployerInput
  • @JayMcDoniel 我添加了雇主解析器
  • 我的候选输入中也有这个@Field({ nullable: true }) employer?: Employer;
  • 这可能就是错误所在。我敢打赌应该是EmployerInput 类型

标签: graphql nestjs typeorm


【解决方案1】:

感谢 cmets 中的 Jay,我发现了错误。

candidate.input.ts

@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;

这样就解决了。

【讨论】:

    猜你喜欢
    • 2021-02-20
    • 2021-04-28
    • 2020-12-20
    • 2019-11-29
    • 2021-05-14
    • 1970-01-01
    • 2020-07-07
    • 2021-04-24
    • 2020-09-05
    相关资源
    最近更新 更多