我正在使用 TypeGraphQL 和 Typegoose 来通过对父级的引用来存储类别树的 API。它的一个 FieldResolver 正在返回一组祖先(使用 graphLookup)。
@FieldResolver(() => [Category])
async ancestors(@Root() input: GetCategoryInput) {
return this.categoryService.getAncestors(input);
}
当我查询祖先时,这可以正常工作,但是当我创建或更新一个类别时,我返回的祖先数组总是空的。
这是完整的解析器
@Resolver(Category)
export class CategoryResolver implements ResolverInterface<Category> {
constructor(private categoryService: CategoryService) {
this.categoryService = new CategoryService();
}
@Query(() => Category)
category(@Arg("input") input: GetCategoryInput) {
return this.categoryService.getCategory(input);
}
@Query(() => [Category])
categories() {
return this.categoryService.getCategories();
}
@FieldResolver(() => [Category])
async ancestors(@Root() input: GetCategoryInput) {
return this.categoryService.getAncestors(input);
}
@FieldResolver(() => [Category])
async descendants(@Root() input: GetCategoryInput) {
return this.categoryService.getDescendants(input);
}
@Mutation(() => Category)
async create(@Arg("input") input: CreateCategoryInput) {
return this.categoryService.createCategory(input);
}
@Mutation(() => Category)
async update(
@Arg("_id", () => ID) _id: string,
@Arg("input") input: UpdateCategoryInput
) {
return this.categoryService.updateCategory(_id, input);
}
}