我为此苦苦挣扎了好几天。
一个月前,我已经使用 AppSync 成功配置了 Apollo-Link-State,并开始添加默认值和解析器,如下所示:
const cache = new InMemoryCache() //create the cache to be shared by appsync and apollo-link-state
const appSyncAtrributes = {
//...
//setup appSync
}
const stateLink = createLinkWithCache(() => withClientState({
cache,
resolvers: {
Mutation: {
updateCurrentUser: (_, { username, thumbnailUrl }, { cache }) => {
// ...
// working mutation that store the login information when user authenticate.
}
}
},
defaults: {
currentUser: {
__typename: 'CurrentUser',
username: '',
thumbnailUrl: ''
}
}
}))
const appSyncLink = createAppSyncLink({
url: appSyncAtrributes.graphqlEndpoint,
region: appSyncAtrributes.region,
auth: appSyncAtrributes.auth,
complexObjectsCredentials: () => Auth.currentCredentials()
})
const link = ApolloLink.from([stateLink, appSyncLink])
const client = new AWSAppSyncClient({}, { link })
这么久了(我正在调用@client 突变并围绕我的应用程序进行查询)。
但现在我正试图在我的链接状态中添加其他数据(其他一切都保持不变):
defaults: {
currentUser: {
__typename: 'CurrentUser',
username: '',
thumbnailUrl: '',
foo: 'bar',
},
hello: 'world',
userSettings: {
__typename: 'userSettings',
isLeftHanded: true
}
}
而且我的缓存没有更新。我是说 :
currentUser
仍然包含__typename: 'CurrentUser', username: '', thumbnailUrl: '
但不包含foo: 'bar'. And the cache doensn't contains
hello: 'bar' or
userSettings`。
更令人困惑的是,如果我给username
或thumbnailUrl
喜欢一个值username: 'joe'
,缓存实际上反映了这种变化!(同时忽略我所有的其他修改)
我尝试了这个实验的所有变体并清除了所有缓存(甚至在新同事的计算机上运行它以确保它们不涉及脏缓存)。
我完全一无所知。
语境 :
- 它发生在iOS模拟器中
- 我正在调试观看 appsync 的 redux 缓存
- 阿波罗缓存内存:1.2.8
- 阿波罗客户端:2.3.5
- 阿波罗链接:1.2.2
- 阿波罗链接状态:0.4.1
- aws-appsync:1.3.2
更新:实际上 currentUser 既不是从默认值存储的。当调用突变时,它会进入缓存。