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

useLazyQuery's refetch method allows updating variables but should also allow updating the query without having to remount the component it's used in #352

Closed
atomless opened this issue Mar 31, 2022 · 4 comments
Assignees
Labels
project-apollo-client (legacy) LEGACY TAG DO NOT USE

Comments

@atomless
Copy link

It's an odd omission to not be able to update the gql query when calling the reFetch method returned by useLazyQuery. This forces us to trigger a complete remounting of the component in which it's used just to update the query. You can override the variables, so why not the query?

Versions
@apollo/client: 3.5.10
graphql: 16.0.1
System:
OS: macOS 10.15.7
Binaries:
Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
Browsers:
Chrome: 99.0.4844.83 (Official Build) (x86_64)

@FreddieHoy
Copy link

@atomless did you find a solution to this apart form remounting the entire component?

@bignimbus
Copy link
Contributor

Hi all, thanks so much for the discussion here! I'm moving this to our feature requests repo for tracking purposes.

@bignimbus bignimbus transferred this issue from apollographql/apollo-client Dec 14, 2022
@bignimbus bignimbus added the project-apollo-client (legacy) LEGACY TAG DO NOT USE label Dec 14, 2022
@bignimbus
Copy link
Contributor

Thanks for posting here! I'm new-ish and still getting acquainted with some of the decisions made on the project but I'm curious about why you'd expect a function called refetch to be able to change the query? When I think of the meaning of refetch I think about doing something again. But updating the query seems to be initiating a new fetch, not re-fetching a previously-fetched query.

Regarding this question:

You can override the variables, so why not the query?

Variables are eventually interpolated but do not impact the structure of the query itself.

Anyhow, I hope my comment makes sense, thanks again!

@jerelmiller
Copy link
Member

Hey @atomless 👋

This functionality was added in v3.7.11. You can call the execute function returned from useLazyQuery to update the query document.

const [execute] = useLazyQuery(document)

// will execute the query using `newDocument` instead
execute({ query: newDocument });

Hope this helps!

@jerelmiller jerelmiller self-assigned this Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project-apollo-client (legacy) LEGACY TAG DO NOT USE
Projects
None yet
Development

No branches or pull requests

4 participants