gpt4 book ai didi

reactjs - 未处理的拒绝(错误): Cannot assign to read only property 'getPosts' of object '#'
转载 作者:行者123 更新时间:2023-12-03 15:34:39 25 4
gpt4 key购买 nike

我已经在互联网上搜索了有关此问题的解决方案,但还没有找到解决方案。我正在创建一个新帖子,并将其添加到Apollo根查询缓存中。由于某种原因,即使newData使用新查询返回了新对象,也不会将其分配给缓存。

    const [createPost, { error }] = useMutation(CREATE_POST_MUTATION, {
variables: values,
update(proxy, result) {
const data = proxy.readQuery({
query: FETCH_POSTS_QUERY,
});

let newData = [...data.getPosts];
newData = [result.data.createPost, ...newData];
proxy.writeQuery({ query: FETCH_POSTS_QUERY, data: newData });
values.body = '';
},
});

最佳答案

仅仅发布了一个问题后,我就知道了。查看阿波罗writeQuery文档,我注意到您必须复制每个状态。

const [createPost, { error }] = useMutation(CREATE_POST_MUTATION, {
variables: values,
update(proxy, result) {
const data = proxy.readQuery({
query: FETCH_POSTS_QUERY,
});

let newData = [...data.getPosts];
newData = [result.data.createPost, ...newData];
proxy.writeQuery({
query: FETCH_POSTS_QUERY,
data: {
...data,
getPosts: {
newData,
},
},
});
values.body = '';
},
});

关于reactjs - 未处理的拒绝(错误): Cannot assign to read only property 'getPosts' of object '#<Object>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64396559/

25 4 0