使用 react-apollo (GraphQL) 进行插入突变,如何获取本地存储更新的插入 ID?

Using react-apollo (GraphQL) to do an insert mutation, how do I get the inserted id for the local store update?

我有以下提交处理程序的表单,然后使用 react-apollo 的 mutate 方法发送 GraphQL 突变:

const doSubmit = (values, { setSubmitting, setErrors }) => {
    //create a new obj: can't submit the extra stuff in values, GraphQL will yell at us!
    const newUser = {
        firstName: values.firstName,
        lastName: values.lastName,
        emailAddress: values.emailAddress,
        isActive: values.isActive,
        sharedAccount: values.sharedAccount,
        userId: values.userId
    };

    mutate({
        variables: { user: newUser },
        update: store => {
            //only update if adding a new user
            if (values.userId !== 0) {
                return;
            }
            let data = store.readQuery({ query: USER_LIST_QUERY });
            data.users.push(newUser);
            store.writeQuery({ query: USER_LIST_QUERY, data });
        }
    }).then(() => {
        //let formik know that submit is complete
        setSubmitting(false);
        //todo: redirect to userlist
    });
};

此方法基于 this mutate-with-update example in the docs

我知道 mutate promise 处理程序将有权访问插入的 id,因为它将在 graphql 响应中返回,但似乎不及时。

除非我也可以从那个 promise 处理程序访问 store,否则我不明白这是怎么可能的。但这似乎是一个如此常见的用例,必须有一种方法,对吧?我是否遗漏了一些明显的东西?

更新方法提供更新对象作为第二个参数

update: (store, { ... access userId here ... }) => { ... }

来自docs

This function will be called twice over the lifecycle of a mutation. Once at the very beginning if an optimisticResponse was provided. The writes created from the optimistic data will be rolled back before the second time this function is called which is when the mutation has succesfully resolved. At that point update will be called with the actual mutation result and those writes will not be rolled back.