【发布时间】:2021-06-10 08:29:50
【问题描述】:
我正在使用 Contentful GraphQL API 来获取项目集合,在此示例中为足球俱乐部。
query Clubs($limit: Int!, $skip: Int!) {
clubCollection(limit: $limit, skip: $skip) {
total
items {
name
description
}
}
}
响应的结构是:
clubCollection: {
items: [{
... array of all the clubs
}]
}
看起来 Apollo InMemoryCache 仅将完整查询缓存在其 ROOT_QUERY 对象中。但不是每个单独的俱乐部。缓存的设置如下所示:
new InMemoryCache({
typePolicies: {
Query: {
fields: {
clubCollection: concatContentfulPagination()
},
},
},
})
有谁知道我如何定位items 中的俱乐部,以便我可以缓存每个单独的俱乐部?
编辑:
感谢@xadm 的回答,我意识到我不需要像这样扩展InMemoryCache:
new InMemoryCache({
typePolicies: {
Query: {
fields: {
clubCollection: {
items: {
},
...concatContentfulPagination()
}
},
},
},
})
而是根据对象的类型将其添加到 typePolicies 的根目录中,对我来说是Club。当我补充说它确实有效!
new InMemoryCache({
typePolicies: {
Club: {
keyFields: ['name']
},
Query: {
fields: {
clubCollection: concatContentfulPagination()
},
},
},
})
【问题讨论】:
标签: reactjs graphql apollo apollo-client contentful