Graphql 阿波罗怎么会聪明到知道什么时候合并,而不是添加?

Graphql 阿波罗怎么会聪明到知道什么时候合并,而不是添加?,graphql,apollo,react-apollo,graphql-js,Graphql,Apollo,React Apollo,Graphql Js,所以我的React Apollo应用程序上有一个表单,可以添加或更新用户。在接收数据时,如果用户存在,Apollo会知道并正确合并/规范数据 如果不是,它不会做任何事情,我需要像这样手动附加它: const result = await this.props.upsertPersonMutation({ variables: { ...this.state.fields, },

所以我的React Apollo应用程序上有一个表单,可以添加或更新用户。在接收数据时,如果用户存在,Apollo会知道并正确合并/规范数据

如果不是,它不会做任何事情,我需要像这样手动附加它:

        const result = await this.props.upsertPersonMutation({
            variables: {
                ...this.state.fields,
            },
            update: (store, { data }) => {
                // somewhere in here I'll need to check if it's new or not
                const initDataQuery = store.readQuery({query: INIT_DATA});
                const { upsertPersonMutation } = data;

                initDataQuery.getInitData.user.people.push(upsertPersonMutation)
                store.writeQuery({query: INIT_DATA, data: initDataQuery})
            }
        }).then(response => {
            this.setState({
                isLoading: false
            });

            this.props.closeCallback();
        })

虽然我对它能够实现前者印象深刻,但为什么它不能将新条目附加到数组中呢?我觉得如果它只解决了一半的问题,那么可能就达不到目的了,你知道吗?

这种情况发生的原因是,在执行变异后,apollo客户端会检查apollo缓存中是否有具有相同标识符密钥的项。如果有,它将自动更新。阿波罗称之为这些更新

如果缓存中没有与突变结果匹配的内容,apollo客户端将不会添加它。突变显式用于突变对象和查询以获取它们。使用
update
函数是通过变异添加它的方法


默认情况下,标识符键是
或访问对象的路径。阅读此链接以了解更多信息

回答得很好,佩德罗。非常感谢