【问题标题】:How to create/Fetch data dynamically by filters and order in GraphQL?如何通过 GraphQL 中的过滤器和顺序动态创建/获取数据?
【发布时间】:2021-10-27 04:44:57
【问题描述】:

我正在 graphql + apollo 客户端中创建一个查询,其过滤器和订单可以根据客户在前端选择的内容而改变。例如:

query (
        $orderPart: String!
        $wherePart: String!
        ) {
    getProductInfo(
        order {$orderPart}
        where {$wherePart}
    ) {
      productID
      description
      size
      model
      cathegoryNumber
    }

其中 $orderPart 将等于“description: DESC”或“productID: ASC”(取决于客户在某一时刻选择了什么)。 $wherePart 将等于 "cathegoryNumber: {eq: 12}" 或 "productID: {eq: 111111}"。

我需要将 order/filter 子句完全作为参数传递。

但它不起作用。语法错误“语法错误:预期名称,找到 $”。

所以我的问题是......

有没有办法实现这些动态过滤器/订单?如何实现此功能?有没有其他方法来实现这个动态过滤器和订单?

谢谢。

注意: 在官方文档中,我发现只有值可以作为参数传递:

query (
        $orderValue: sortEnumType! = DESC
        $whereValue: String! = "description1"
        ) {
    getProductInfo(
        order {productID: $orderValue}
        where {description: {eq: $whereValue} }
    ) {
      productID
      description
      size
      model
      cathegoryNumber
    }

但这不是我需要的,因为始终无法更改过滤器/订单。第一次 (product:ASC),第二次 (cathegoryNumber:DESC) 等等,它们每次都可能完全不同……

【问题讨论】:

标签: graphql apollo


【解决方案1】:

感谢您的帮助,我找到了解决方案。创建模板时使用 apollo 客户端和变量:

let orderValue = 'order {productID: ASC}'; // built dynamically
let whereValue = 'description: {eq: "description1"}'; // built dynamically

document = gql`
query {
    getProductInfo(
        ${orderValue}
        ${whereValue}
    ) {
      productID
      description
      size
      model
      categoryNumber
    }
    `;

【讨论】:

猜你喜欢
  • 2021-11-05
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
相关资源
最近更新 更多