'react apollo cache.write issue

I use "@apollo/client": "^3.5.9" and have an issue when try to update cache

Here is the problem:

I use useMutation hook and inside this hook try to update my cache

const [createUser, { data, loading, error }] = useMutation(addUser, {
  update(cache, { data }) {
    const newUser = data?.createUser?.user;
    const existUsers: any = cache.readQuery({
      query: allUsersQuery
    });
    cache.writeQuery({
      query: allUsersQuery,
      data: {
        allUsers: [...existUsers?.allUsers.edges, newUser]
      }
    });
  }
});

I receive an errors below Error 1:

react_devtools_backend.js:4061 Missing field 'edges' while writing result {
  "__typename": "UserSchema",
  "id": "acb4e46f-b80a-42ee-b2d5-c838b63b2c63",
  "name": "456"
}

Error 2:

react_devtools_backend.js:4061 Cache data may be lost when replacing the allUsers field of a Query object.

To address this problem (which is not a bug in Apollo Client), define a custom merge function for the Query.allUsers field, so InMemoryCache can safely merge these objects:

  existing: {"__typename":"UserConnection","edges":[{"__typename":"UserEdge","node":{"__ref":"CustomUserNode:6a845e81-ae09-46c5-9b5c-5e250efbad71"}},{"__typename":"UserEdge","node":{"__ref":"CustomUserNode:2cff31b1-ed2b-4245-b7d9-268df82b8c4f"}},{"__typename":"UserEdge","node":{"__ref":"CustomUserNode:95869b70-4c7b-42f1-baad-d7c358caa4ff"}},{"__typename":"UserEdge","node":

I try to googled, try to wrap in try and catch blow, but it doesn't work. I supposed i missing something but i do not what exactly.

Any comments highly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source