【问题标题】:GraphQL query use nested objects` fields to filterGraphQL 查询使用嵌套对象的字段进行过滤
【发布时间】:2022-12-14 05:59:08
【问题描述】:

我刚开始学习 .net core 中的 graphql 和 HotChocolate.Data。 我使用属性 [UseFiltering] 如何使用嵌套对象的字段来过滤表中的行并同时过滤嵌套对象?

query GetData(
  $search: String
) {
  documents(
    where: {
       name: { contains: $search }
    }
  ) {
    total: totalCount
    items {
      id
      name
      requirements(
        where: {
          name: { contains: $search }
        }
      ) 
      {
        id
        name
      }
    }
  }
}

按文档过滤工作正常,但如果我尝试连接按要求过滤,我会出错 生成期间出错:字段“Document.requirements”上的未知参数“where”。

【问题讨论】:

  • 我发现我必须将 [UseFiltering] 属性添加到类的集合属性中(即你的 requirements 属性

标签: graphql hotchocolate


【解决方案1】:

为此,您需要告诉 HotChocolate 您要在该字段上进行过滤。 As Chris says in his comment,你可以使用注解来完成[UseFiltering]

或者,您可以使用代码优先的方法:

在你的 Item 类型中,你应该将 UseFiltering() 添加到 Requirements 字段:

protected override void Configure(IObjectTypeDescriptor<Item> descriptor)
{
    descriptor
        .Field(x => x.Requirements)
        .UseFiltering(); // This is the relevant line to add
}

【讨论】:

    猜你喜欢
    • 2011-12-24
    • 2022-11-11
    • 2018-07-09
    • 2017-04-13
    • 2018-08-04
    • 2017-11-25
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多