Created
June 29, 2019 17:24
-
-
Save ngryman/6856c7eb8f9a15b1095032a6ba478c5c to your computer and use it in GitHub Desktop.
Updating cached data from multiple parameterized queries after a mutation (hacky solution)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addTask({ | |
variables: { input }, | |
optimisticResponse: { | |
addTask: { | |
__typename: 'Task', | |
id, | |
...input | |
} | |
}, | |
update: (proxy: any, { data: { addTask } }: any) => { | |
const query = Queries.tasks | |
const variablesList = getVariablesListFromCache(proxy, query) | |
for (const variables of variablesList) { | |
const data = proxy.readQuery({ query, variables }) | |
data.tasks.push(addTask) | |
proxy.writeQuery({ query, data }) | |
} | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getVariablesListFromCache = (proxy: any, query: any) => { | |
const queryName = query.definitions[0].name.value | |
const rootQuery = proxy.data.data.ROOT_QUERY | |
// XXX: When using `optimisticResponse`, `proxy.data.data` resolves to | |
// another cache that doesn't contain the root query. | |
if (!rootQuery) return [] | |
const matchQueryReducer = (names: string[], name: string) => { | |
if (name.startsWith(queryName)) { | |
names.push(name) | |
} | |
return names | |
} | |
const parseQueryNameToVariables = (name: string) => | |
JSON.parse((name.match(/{.*}/) as string[])[0]) | |
return Object.keys(rootQuery) | |
.reduce(matchQueryReducer, []) | |
.map(parseQueryNameToVariables) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case it helps someone, I used the
selectionSet
since I use different names than the declared in the resolvers for the queries in the frontend. To avoid updating queries that are not the ones that I want I ended up using a simple regex, for example, if the query islead
, but I also haveleads
, I only wantlead
. This works for me: