ecerroni / apollo-cache-updater Goto Github PK
View Code? Open in Web Editor NEWHelper for updating the apollo cache after a mutation
License: MIT License
Helper for updating the apollo cache after a mutation
License: MIT License
If different queries have the same exact variables there are some cases where the wrong queries get updated in the cache.
I currently have this in my code:
update: (store, { data: { deleteTracks } }) => {
const updates = ApolloCacheUpdater({
proxy: store,
queriesToUpdate: [getTracksQuery],
searchVariables: {
// ??
},
mutationResult: deleteTracks,
});
},
It just so happens that my delete mutation returns an array of IDs that were actually found and deleted, but the cache updater doesn't like this:
ApolloCacheUpdater Error: mutationResult cannot be an array | [mutation-result_no-array]
I'm also not totally sure I'm using searchVariables
properly.
Although queries in the cache should not be frozen, sometimes it still happens. I've not understood what is the exact reason of this.
The error message is:
TypeError: Cannot assign to read only property '__my_query_name__' of object '#<Object>'
When adding a new Employee, ApolloCacheUpdater successfully updates the cache but does not trigger a re-render ( shown once the page is refreshed).
REMOVE is working as intended and triggers a re-render upon completion.
fetchPolicy: 'cache-and-network',
errorPolicy: 'all',
notifyOnNetworkStatusChange: true,
returnPartialData: true
The following image shows:
const { loading, data, refetch } = useQuery(GET_EMPLOYEES)
When an element is moved from a query to another the old element (before the mutation result) is moved instead of the updated value
Hi! First of all thank you for this awesome helper.
I'm just having trouble making it work with @client directive. Below is my query
const TODO_MANY = gql`
query {
todoMany {
_id
title
isOptimistic @client
}
}
`;
const ADD_TODO = gql`
mutation TodoCreateOne($input: CreateOneTodoInput!) {
todoCreateOne(record: $input) {
record {
_id
title
}
recordId
}
}
`;
Here's my onClick callback
const onClick = useCallback(() => {
const id = uuid();
addTodo({
variables: {
input: {
title: 'test12',
},
},
optimisticResponse: {
__typename: 'Mutation',
todoCreateOne: {
__typename: 'CreateOneTodoPayload',
recordId: id,
record: {
__typename: 'Todo',
title: 'test12',
_id: id,
isOptimistic: true,
},
},
},
update: (cache, { data: { todoCreateOne } }) => {
const updates = ApolloCacheUpdater({
proxy: cache,
queriesToUpdate: [TODO_MANY],
operation: {
type: 'ADD',
add: ({ query, type, data, variables }) => {
console.log('Calling Add');
const rec = todoCreateOne.record;
return [rec, ...data];
},
},
searchVariables: {},
mutationResult: todoCreateOne,
ID: 'recordId',
});
if (updates) console.log(`Success`);
},
});
}, []);
It is working without @client. "Calling Add" is displaying in console
but when I add @client, add function is not executing.
Note : I also tried doing the cache update manually and it works.
const queryData = cache.readQuery({ query: TODO_MANY });
cache.writeQuery({
query: TODO_MANY,
data: {
todoMany: [
...queryData.todoMany,
{
...todoCreateOne.record,
},
],
},
});
Thank you.
Hi! Just wanted to know the feasibility of adding declaration typescript files (since the project is solely built on javascript) This would be a great addition for pure Typescript projects.
Thanks,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.