【问题标题】:GrahpQL Mutation not returning value of FieldResolverGraphQL Mutation 不返回字段解析器的值
【发布时间】:2022-04-07 18:23:49
【问题描述】:

我正在使用 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);
  }
}

【问题讨论】:

  • 这个问题与 mongodb / typegoose 有什么关系?我在代码中看不到任何与此相关的内容

标签: typescript mongodb graphql typegraphql typegoose


【解决方案1】:

尝试添加这个

{ new: true }

在您的查找示例中:

UserModel.findByIdAndUpdate(
      _id,
      {
        employee: input,
      },
      { new: true }
    );

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 2019-07-23
    • 2022-01-10
    • 1970-01-01
    • 2020-10-07
    • 2021-04-19
    • 1970-01-01
    • 2021-07-26
    • 2020-08-26
    相关资源
    最近更新 更多