突变后的 Apollo 客户端更新无法映射原始结果?

Apollo client update after mutation can't map over original result?

所以基本上我想在突变后更新我的 UI,但这真的很痛苦,现在已经卡住了几天。

我的一些组件依赖于我的根查询,并映射结果。

每当我创建一个新资源并更新我的根查询时,原始结果就不再加载,这会导致 can't read property map of undefined,因为结果不再存在。添加支票没有任何作用,它只会一直加载。

这是我的突变:

export const withCreateLink = graphql(createLink, {
// Mutate normally gets passed as prop to wrapped component
props({ownProps, mutate}) {
    return {
        // But for updating the store we're actually hijacking mutate to a custom function        
        createLink(url, description, group) {
            return mutate({
                variables: {
                    url,
                    description,
                    group
                },
                optimisticResponse: {
                    createLink: {
                        __typename: 'Link',
                        id: -1,
                        url,
                        description,
                        group: {
                            id: group
                        }
                    }
                },
                update: (proxy, mutationResult) => {
                    const query = getAllGroups;
                    const data = proxy.readQuery({query});

                    data.allGroups.forEach((groupData) => {
                        if(groupData.id == group)
                            groupData.links.push(mutationResult.data.createLink);
                    });

                    proxy.writeQuery({
                        query,
                        data
                    })
                }
            })
        }
    }
}
});

我只想通过刷新我的资源添加到的组来简单地更新我的 UI。有人可以帮我吗?

转换 dataIdFromObject 以检查 typename 是否有效:

dataIdFromObject: (result) => {
    if (result.id && result.__typename) {
        return result.__typename + ':' + result.id;
    }
    return null;
}

我想我无法仅使用 Id 写入缓存。这显然是默认的提议。