我正在尝试使用update:在执行突变后更新查询 . 问题是商店中的查询应用了几个不同的变量,我想更新查询并使用相同的变量返回它 .
我在文档中发现updateQueries有一个选项可以包含queryVariables,它是执行查询的最后一组变量 .
我还没有找到任何描述如何从更新内部检索queryVariables或类似内容的东西 .
更新内部:我可以使用
lastQuery = Object.keys(store.data.ROOT_QUERY).slice(-1)[0]
这将返回如 "userRecipes({"first":20,"minTime":0,"maxTime":500,"filterType":"Explore","searchTerm":""})"
的结果
我现在这样做的hacky方法是解析该字符串以取出变量,这样我最终可以像这样使用readQuery:
const lastQuery = Object.keys(store.data.ROOT_QUERY).slice(-1)[0]
const searchPosition = lastQuery.search("searchTerm")
const searchTerm = lastQuery.slice((searchPosition + 13),-3)
// also parsing the lastQuery string for filterType, minTime, maxTime
const data = store.readQuery({
query: QUERY_USER_RECIPES,
variables: {
filterType: filterType,
searchTerm: searchTerm,
minTime: minTime,
maxTime: maxTime,
}
});
这不是最好的方法 . 是否有更简单的方法来访问更新内部的变量?
似乎应该有一种方法来读取存储中的现有查询和变量,而无需使用readQuery传递变量 .
谢谢你看看这个问题!
版
apollo-client@1.4.0 react-apollo@1.4.2
1 回答
For apollo 2, but should be the same in 1.x
In the docs,您看到您也可以将变量传递给readQuery .
下面是一个用户可以预订单击BookEvent组件的事件的示例,如果突变成功,它将自动反映在上部组件EventDetail中 .
在触发变异的组件(BookEvent)中,我将
store
和eventId
传递给在上部组件(EventDetail)中声明的函数,并通过子组件的props传递:以下是在上部组件中执行缓存更新的函数:
它像
<BookEvent updateStoreAfterBooking={updateCacheAfterBooking} ... />
那样传递 .不要忘记将所需的变量传递给writeQuery .