【发布时间】:2019-11-26 11:00:12
【问题描述】:
我尝试在运行 Mutation 后更新 Apollo 缓存。为此,我使用mutate 方法:
client.mutate({
mutation: CREATE_PLAYLIST,
variables: { ...playlistUpdated, users: [1] },
update: cache => {
const { playlists } = cache.readQuery({ query: GET_USER_PLAYIST });
cache.writeQuery({
query: GET_USER_PLAYIST,
data: { playlists: playlists.concat(playlistUpdated) }
});
}
});
但是在writeQuery 函数上,我有这个错误:TypeError: TypeError: undefined is not an object (evaluating 'data.playlists')。我不太明白为什么会出现此错误,因为GET_USER_PLAYIST 查询响应中存在 data.playlists :/
const GET_USER_PLAYIST = gql`
{
playlists(where: { users: { id: 1 } }) {
name
id
}
}
`;
谁能帮帮我?
感谢社区!
【问题讨论】:
-
看起来像是错字。调用
writeQuery时,您正在构造一个具有playlist属性的对象,但根据您的查询,它应该是playlists。 -
嗨!嗯,是的,我认为这是错误的 c/p,但与
playlists的结果相同 :(
标签: reactjs apollo react-apollo