urql
urql copied to clipboard
RFC: Allow for selectively skipping local resolver with a hook option
Summary
Graphcache is great and the local resolvers feature works great for merging data together when doing cursor pagination. However, if I wanted to use the same query in multiple places and in some components I did NOT want to merge data when paginating, my hands are tied because if you apply a local resolver to a query, then it applies to ALL calls to that query. I tried setting requestPolicy
to network-only
but that does not seem to have an effect on whether local resolvers are used.
Proposed Solution
Perhaps a simple switch to skip the local resolver straight from the hooks. Maybe a skipLocalResolvers option or something similar?
Requirements
I am not familiar with the internals of urql, so really I'm just looking to have an option to decide whether a query will use the local resolvers I've written to resolve things or not, however that might best be implemented.
Thank you for the amazing work! Love using urql :)
I don't know if this will also work for queries (as the docs only talk about mutations and I haven't tried it), but it may be possible to pass an "extra" argument to your query, and use this in the resolver to conditionally perform your merge?
https://formidable.com/open-source/urql/docs/graphcache/cache-updates/#variables-for-optimistic-updates
This would be really useful, I'm having the same experience as you.
Generally I would say this option already exists, let's say for a local resolver you are using the pagination
but you want to opt out of it then you can do
const myResolver = relayPagination();
cacheExchange({
resolvers: {
Query: {
list: (parent, args, cache, info) => {
// or any other condition 😅
if (info.variables.skipPagination) {
return cache.resolve('Query', 'list', args)
}
return myResolver(parent, args, cache, info);
}
}
}
})
Thanks @JoviDeCroock, that's very helpful.
Generally I would say this option already exists, let's say for a local resolver you are using the
pagination
but you want to opt out of it then you can doconst myResolver = relayPagination(); cacheExchange({ resolvers: { Query: { list: (parent, args, cache, info) => { // or any other condition 😅 if (info.variables.skipPagination) { return cache.resolve('Query', 'list', args) } return myResolver(parent, args, cache, info); } } } })
Just tried this - info.variables
seems to be getting filtered down before it is passed to cacheExchange resolvers. @JoviDeCroock , so I cannot do conditional behavior this way
Sounds good, much appreciated.
Yeah I'm not sure where it happens but it's definitely being passed in to useQuery and not making it into the info
object.
For context I'm on Graphcache 4.4.3, although it seems that neither of the snippets you linked have changed since then, so might not be a relevant detail.
Just tried in a sanbox, works great https://codesandbox.io/s/jovial-cloud-usm1t4?file=/src/components/Todos.js
Okay I got it working, it turned out to be filtering it out because I had requestPolicy: "network-only"
set, thank you :)
Resolved by #3191 / #3306 which have been released in @urql/[email protected]