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

using context inside of useQuery falls into an infinite loop #507

Closed
rmacy opened this issue Jan 15, 2020 · 5 comments
Closed

using context inside of useQuery falls into an infinite loop #507

rmacy opened this issue Jan 15, 2020 · 5 comments
Labels
documentation 📖 This needs to be documented but won't need code changes

Comments

@rmacy
Copy link

rmacy commented Jan 15, 2020

Describe the bug
useQuery will fall into an indefinite render loop if you pass context aka Partial<OperationContext> as a parameter.

Steps to reproduce
Create a useQuery hook and pass context, e.g.

useQuery({ query: MyQuery, variables: { someId }, context: {} });

Expected behavior
useQuery is only called once.

Actual behavior
useQuery falls into an infinite loop.

Additional context
I think using context is valuable because it's a great way to pass in an auth token as a header if you don't have access to an auth token outside of a component render context.

@rmacy rmacy added the bug 🐛 Oh no! A bug or unintented behaviour. label Jan 15, 2020
@JoviDeCroock
Copy link
Collaborator

Hey @macy

Thanks for the error report, this is intended. We advice using useMemo this way you can control when the query should refetch. There can be conditions where you don't want to refetch for some minor change (i.e. token being refetched). We can't anticipate these scenario's that's why we move this responsibility to your side.

useQuery({ query: MyQuery, variables: { someId }, context: React.useMemo(() => ({}), []) });

Should do the trick.

@JoviDeCroock JoviDeCroock added documentation 📖 This needs to be documented but won't need code changes question 🙋 and removed bug 🐛 Oh no! A bug or unintented behaviour. labels Jan 15, 2020
@rmacy
Copy link
Author

rmacy commented Jan 15, 2020

Thanks @JoviDeCroock -- I don't know if that was clear in the docs after a quick glance, I'd be happy to make a PR, thoughts?

@JoviDeCroock
Copy link
Collaborator

That would be awesome, we're trying to be as clear as we can in our documentation but an outside view is most often the best to show us these oversights. That contribution would be more than welcome!

@rmacy
Copy link
Author

rmacy commented Jan 15, 2020

🔥 Will do! Thank you for the awesome work in this library!

@sunpietro
Copy link
Contributor

I've created a PR that updates the missing info in the docs: #1800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation 📖 This needs to be documented but won't need code changes
Projects
None yet
Development

No branches or pull requests

3 participants