Apollo Client: the Upsert mutation only modifies the cache when updating, but does not create

I have an upsert request that runs when I create or update. When updating, Apollo integrates the result into the cache, but not on its creation.

Here is the request:

export const UPSERT_NOTE_MUTATION = gql` mutation upsertNote($id: ID, $body: String) { upsertNote(id: $id, body: $body) { id body } }` 

My client:

 const graphqlClient = new ApolloClient({ networkInterface, reduxRootSelector: 'apiStore', dataIdFromObject: ({ id }) => id }); 

The response from the server is identical: both id and body are returned, but Apollo does not automatically add new identifiers to the data cache object.

Is it possible that Apollo will automatically add new objects to the data without causing further fetching?

Here's what my data warehouse looks like:

enter image description here

UPDATE

According to the documentation, the updateQueries function should allow me to insert a new item into my list of assets without having to repeat the query to select the source origin.

The function is executed, but everything that is returned by the function is completely ignored and the cache does not change.

Even if I do something like this:

  updateQueries: { getUserAssets: (previousQueryResult, { mutationResult }) => { return {}; } } 

Nothing changes.

UPDATE # 2

Still unable to update my asset list.

Inside updateQueries this is what my previousQueryResult looks like:

  updateQueries: { getUserAssets: (previousQueryResult, { mutationResult }) => { return { assets: [] .concat(mutationResult.data.upsertAsset) .concat(previousQueryResult.assets) } } } 

But no matter what I return, the data store is not updated:

enter image description here

For reference, here is what each asset looks like:

enter image description here

+6
source share
2 answers

Have you followed suit here ? I would write updateQueries in mutate like this:

 updateQueries: { getUserAssets: (previousQueryResult, { mutationResult }) => { const newAsset = mutationResult.data.upsertAsset; return update(prev, { assets: { $unshift: [newAsset], }, }); }, } 

Or with the purpose of the object instead of updating from the universal assistant:

 updateQueries: { getUserAssets: (previousQueryResult, { mutationResult }) => { const newAsset = mutationResult.data.upsertAsset; return Object.assign({}, prev, {assets: [...previousQueryResult.assets, newAsset]}); }, } 
+1
source

As you indicate in your update, you need to use updateQueries to update queries related to this mutation. Although your question does not indicate which query needs to be updated with the result of the mutation, I assume that you have something like this:

 query myMadeUpQuery { note { id body } } 

which should return a list of notes that are in your system, with the identifier and body of each note. With updateQueries your callback receives the result of the request (i.e., Information about the recently inserted note) and the previous result of this request (i.e., the List of notes), and your callback should return a new result, which should be assigned to the request above.

See here for a similar example. Essentially, without the immutability-helper that is used in this example, you can write the updateQueries as follows:

 updateQueries: { myMadeUpQuery: (previousQueryResult, { mutationResult }) => { return { note: previousQueryResult.note(mutationResult.data.upsertNode), }; } } 
0
source

Source: https://habr.com/ru/post/1014111/


All Articles