Multi-argument custom query invalidation doesn't seem to work
See original GitHub issueIssue Description
In a project, I have lists of entities called “hypotheses” that need to be refreshed via live queries. These lists are fetched using the following query:
type Query {
hypotheses(
project: ID!
law: Int
filters: ListFiltersInput!
): HypothesisPage!
}
Hypotheses are tied to specific “laws” (identified by integers). The second argument law
can be null
which returns all hypotheses regardless of their laws.
I’m trying to invalidate based on a custom index that looks like this:
{
field: "Query.hypotheses",
args: ["project", "law"]
}
Here is the document I use on the frontend:
export const HypothesesQuery = gql`
${HypothesisPageFragment}
query Hypotheses($project: ID!, $law: Int, $filters: ListFiltersInput!)
@live {
hypotheses(project: $project, law: $law, filters: $filters) {
...HypothesisPage
}
}
`;
Now when creating an hypothesis with a createHypothesis
mutation (resolver is successfully called), the following invalidation doesn’t work:
invalidate([
`Query.hypotheses(project:"${project._id.toHexString()}",law:null)`,
`Query.hypotheses(project:"${project._id.toHexString()}",law:${
hypothesis.law
})`
]);
(project._id.toHexString()
has the correct value)
Any idea of what’s going on? Thank you.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
React-Query and Query Invalidation Question - Stack Overflow
The way to achieve this using react-query is via queryCache.invalidateQueries to invalidate the cache after the mutation. From the docs:.
Read more >Query Invalidation | TanStack Query Docs
For that purpose, the QueryClient has an invalidateQueries method that lets you intelligently mark queries as stale and potentially refetch them too! tsx....
Read more >invalidateQueries doesn't work when enabled attribute is set ...
Hi, im using "react-query": "^3.24.3" and this issue is still happening, i have a query defined with enable: false and when i want...
Read more >Reading and writing data to the cache - Apollo GraphQL Docs
Watched queries can control what happens when they're invalidated by updates to the cache, by passing options like fetchPolicy and nextFetchPolicy to client....
Read more >Mastering Mutations in React Query | TkDodo's blog
There is however a second, integral part to working with data: ... A mutation that likes a blog post has no ties towards...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You would still have the hard-coded double quotes, which can bring weird indices collision. For example imagine a nullable query argument of type
String
:null
and"null"
would serialize to the same index.I see your point, in that case,
JSON.stringify
(aka https://www.npmjs.com/package/json-stable-stringify) might be the best solution. 🤔I think adding
json-stable-stringify
as a dependency should be fine.@strblr Would you like to create a PR with some tests and an implementation?