首页 > 解决方案 > 如何使用 apollo 缓存来匹配作为子数组/子 ID 列表的输入变量?

问题描述

如何使用 apollo 缓存来匹配作为缓存中 id 的子数组/子列表的输入变量?

特别是,当我执行 apollo 查询时,我想返回一个参数列表子集的响应。

换句话说,我怎样才能在缓存中添加额外的条目,以便我的缓存可以匹配未来的“子查询”,例如

fetch({
    variables: {
        ids: ['1']
    }
})

当我之前询问过

fetch({
    variables: {
        ids: ['1', '2', '3']
    }
})

完整示例如下:

// This code is not working, but just used to illustrate an example.
const [fetch, data] = useQuery( /* graphql params here */ );

// Apollo cache will cache the response for `ids: ['1', '2', '3']`
fetch({
    variables: {
        ids: ['1', '2', '3']
    }
})

// In these subsequent call, I don't want to hit the server and want to hit the cache.
// Can I leverage apollo cache for this?
fetch({
    variables: {
        ids: ['1']
    }
})
fetch({
    variables: {
        ids: ['2','3']
    }
})
fetch({
    variables: {
        ids: ['1','2']
    }
})

标签: apolloapollo-clientreact-apollo

解决方案


您应该能够对所讨论的类型使用Key 参数read 函数之一。


推荐阅读