Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Allow for selectively skipping local resolver with a hook option #2650

Closed
mtourj opened this issue Aug 30, 2022 · 10 comments
Closed

RFC: Allow for selectively skipping local resolver with a hook option #2650

mtourj opened this issue Aug 30, 2022 · 10 comments
Labels
future 🔮 An enhancement or feature proposal that will be addressed after the next release

Comments

@mtourj
Copy link

mtourj commented Aug 30, 2022

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 :)

@mtourj mtourj added the future 🔮 An enhancement or feature proposal that will be addressed after the next release label Aug 30, 2022
@sarink
Copy link

sarink commented Sep 23, 2022

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

@focux
Copy link

focux commented Nov 25, 2022

This would be really useful, I'm having the same experience as you.

@JoviDeCroock
Copy link
Collaborator

JoviDeCroock commented Nov 25, 2022

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);
      }
    }
  }
})

@mtourj
Copy link
Author

mtourj commented Nov 25, 2022

Thanks @JoviDeCroock, that's very helpful.

@mtourj
Copy link
Author

mtourj commented Nov 25, 2022

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);
      }
    }
  }
})

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

@JoviDeCroock
Copy link
Collaborator

JoviDeCroock commented Nov 25, 2022

It shouldn't be based on the code here can look at it next week though which is used here

@mtourj
Copy link
Author

mtourj commented Nov 25, 2022

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.

@JoviDeCroock
Copy link
Collaborator

Just tried in a sanbox, works great https://codesandbox.io/s/jovial-cloud-usm1t4?file=/src/components/Todos.js

@mtourj
Copy link
Author

mtourj commented Nov 25, 2022

Okay I got it working, it turned out to be filtering it out because I had requestPolicy: "network-only" set, thank you :)

@kitten
Copy link
Member

kitten commented Jul 22, 2023

Resolved by #3191 / #3306 which have been released in @urql/[email protected]

@kitten kitten closed this as completed Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
future 🔮 An enhancement or feature proposal that will be addressed after the next release
Projects
None yet
Development

No branches or pull requests

5 participants