【发布时间】:2018-12-27 15:35:41
【问题描述】:
使用 apollo-angular,我有一个像这样的 apollo InMemoryCache
new InMemoryCache({
// ... stuff
cacheRedirects: {
Query: {
form: (_, args, { getCacheKey }) => {
console.log('cache key', [args, getCacheKey({ __typename: 'Form', id: args.formId })]);
return getCacheKey({ __typename: 'Form', id: args.formId });
},
},
},
});
当我运行这个查询时,form 的 cacheRedirect 函数没有被调用。
this.apollo.watchQuery({
query: gql`
query FormSummary($formId: ID!) {
form(formId: $formId) {
__typename
id
description
}
}
`,
variables: { formId }
}).valueChanges
相反,查询只是直接发送到服务器(成功),忽略了表单(带有__typename、id 和description 字段)已经在缓存中的事实。即使它不在缓存中,我也希望表单的 cacheRedirect 函数仍然被调用。当我进行这个极其相似的查询时,表单的cacheRedirect被调用。
this.apollo.watchQuery<any>({
query: gql`
query FormGet($formId: ID!) {
form(formId: $formId) {
...WholeFormResponse
}
}
${Fragments.wholeFormResponse}
`,
variables: { formId: id },
}).valueChanges
我是否遗漏了一些关于 cacheRedirects 工作方式的明显内容?我的印象是,只要调用以 query Form {} 开头的查询,form 的 cacheRedirect 就应该运行。
非常感谢任何帮助!我想我错过了一些明显的东西......
注意:我对cacheRedirect的理解是WholeFormResponse片段的内容无关紧要,所以我没有包含它们。
更新
打开调试器并单步执行一些代码,我的 cacheRedirect 函数没有被调用(在错误示例中)的原因是因为typeof fieldValuein this line of the apollo InMemoryCache source code!== 'undefined'。我还没有弄清楚为什么它应该是未定义的,或者为什么对于我的极其相似的工作查询它等于未定义。
【问题讨论】:
标签: apollo apollo-client apollo-angular