【问题标题】:Get parent object at field type policy在字段类型策略中获取父对象
【发布时间】:2022-06-12 20:44:59
【问题描述】:

假设我有以下获取产品列表的查询。

query ProductList() {
  products() {
    name
    price
    stockQuantity
    isAvailable @client # This is a local-only field
  }
}

我还在内存缓存创建时为仅限本地字段添加了类型策略,并带有读取功能:

const cache = new InMemoryCache({
  typePolicies: { // Type policy map
    Product: {
      fields: { // Field policy map for the Product type
        isAvailable: { // Field policy for the isAvailable field
          read(existing, context) { // The read function for the isAvailable field
            // Return value
          }
        }
      }
    }
  }
});

如何在isAvailable读取函数中获取父Product对象的stockQuantity字段?

【问题讨论】:

    标签: javascript graphql apollo-client apollo-cache-inmemory


    【解决方案1】:
    const cache = new InMemoryCache({
      typePolicies: { // Type policy map
        Product: {
          fields: { // Field policy map for the Product type
            isAvailable: { // Field policy for the isAvailable field
              read(existing, {readField}) { // The read function for the isAvailable field
                const stockQuantity = readField("stockQuantity");
                return stockQuantity ? true : false;
              }
            }
          }
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-10-04
      • 1970-01-01
      • 2022-01-01
      • 2013-12-18
      • 2014-10-08
      • 2018-04-20
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多