-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Open
Description
Hi,
Is there a way to get a list of queries by name in the cache? I have an issue where I'm unable to update a cached query because I don't know what variables have been used to fetch it.
Example:
- Query to get a list of cards (paginated)
- Query accepts sort/filter options
- Query uses
keyArgs
in type policy to cache queries separately - Mutation to create a card
- Add card to list using
cache.writeQuery
in the mutationupdate
callback
Here's what my field policy looks like:
const cardsPagination: FieldPolicy<CardCollection> = {
keyArgs(args) {
const variables = args as GetCardsQueryVariables;
return variables.input ? `${variables.input.order_by}${variables.input.order_by_dir}` : '';
},
merge(existing, incoming, { readField }) {
if (!existing) {
return incoming;
}
const pageInfo = { ...existing.pageInfo, ...incoming.pageInfo };
const edges = uniqueBy([...existing.edges, ...incoming.edges], (edge) =>
readField('idx', edge.node),
);
return { __typename: 'CardCollection', pageInfo, edges };
},
};
and the mutation update:
update(cache, { data }) {
const card = data?.cardCreate;
if (!card) {
return;
}
cache.writeQuery<GetCardsQuery, GetCardsQueryVariables>({
query: getCardsQuery,
// Should have variables here...
data: {
cards: {
__typename: 'CardCollection',
pageInfo: pageInfoStub,
edges: [{ node: card, cursor: Date.now().toString() }],
},
},
});
},
My current solution is to call cache.writeQuery
with all possible variable combinations, but this isn't ideal. Using refetchQueries
is not something I consider a "solution", it's a work-around.
Thanks.
Metadata
Metadata
Assignees
Labels
No labels