dip*_*ent 7 javascript react-redux apollostack react-apollo apollo-client
我有一个在创建或更新时触发的upsert查询.在更新时,Apollo将结果集成到缓存中,但在创建时却没有.
这是查询:
export const UPSERT_NOTE_MUTATION = gql`
mutation upsertNote($id: ID, $body: String) {
upsertNote(id: $id, body: $body) {
id
body
}
}`
Run Code Online (Sandbox Code Playgroud)
我的客户:
const graphqlClient = new ApolloClient({
networkInterface,
reduxRootSelector: 'apiStore',
dataIdFromObject: ({ id }) => id
});
Run Code Online (Sandbox Code Playgroud)
来自服务器的响应是相同的:返回两者id并body返回,但Apollo不会data自动将新ID添加到缓存对象中.
是否有可能让Apollo自动添加新对象而data不会触发后续获取?
这是我的数据存储的样子:
UPDATE
根据文档,该函数updateQueries应该允许我将新元素推送到我的资产列表,而不必再次触发我的原始获取查询.
该函数被执行但该函数返回的任何内容都被完全忽略,并且不修改缓存.
即使我做这样的事情:
updateQueries: {
getUserAssets: (previousQueryResult, { mutationResult }) => {
return {};
}
}
Run Code Online (Sandbox Code Playgroud)
没有什么变化.
更新#2
仍然无法获取我的资产列表进行更新.
在里面updateQueries,这是我的previousQueryResult样子:
updateQueries: {
getUserAssets: (previousQueryResult, { mutationResult }) => {
return {
assets: []
.concat(mutationResult.data.upsertAsset)
.concat(previousQueryResult.assets)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但无论我返回什么,数据存储都不刷新:
作为参考,这是每个asset看起来像:
正如您在更新中所述,您需要使用updateQueries来更新与此突变相关的查询。尽管您的问题没有说明要使用突变结果更新哪种查询,但我假设您有这样的内容:
query myMadeUpQuery {
note {
id
body
}
}
Run Code Online (Sandbox Code Playgroud)
它应该返回系统中当前的笔记列表,以及每个笔记的 ID 和正文。使用updateQueries,您的回调接收查询结果(即有关新插入注释的信息)和此查询的先前结果(即注释列表),并且您的回调必须返回应分配给上面查询的新结果。
请参阅此处的类似示例。本质上,如果没有immutability-helper给定示例使用的 ,您可以updateQueries按如下方式编写回调:
updateQueries: {
myMadeUpQuery: (previousQueryResult, { mutationResult }) => {
return {
note: previousQueryResult.note(mutationResult.data.upsertNode),
};
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
739 次 |
| 最近记录: |