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

Added useQuery frame tests to show that there is an unneeded frame. #9508

Closed
wants to merge 1 commit into from
Closed

Added useQuery frame tests to show that there is an unneeded frame. #9508

wants to merge 1 commit into from

Conversation

FritsvanCampen
Copy link
Contributor

PR related to #9459 (review)

@benjamn
Copy link
Member

benjamn commented Mar 10, 2022

@FritsvanCampen Were you intending this test to fail? Or is it just a (currently passing) regression test?

@benjamn benjamn self-assigned this Mar 10, 2022
@FritsvanCampen
Copy link
Contributor Author

FritsvanCampen commented Mar 10, 2022

Were you intending this test to fail? Or is it just a (currently passing) regression test?

The intention was that the test passes and showing that there is an 'unneeded' frame. Regardless of whether we want to spend efforts on trying to eliminate it, I think it's good to document that this how the hook currently behaves. In it's current state the tests also serve as a regression test for any unintentional 'new' frames that might be emitted if the implementation is changed. So this PR could be merged as-is, it's merely documenting the current behavior.

I think it would be good to expand these tests for other cases. I tried making a test for refetch() but I was surprised to find that it emits no extra frames at all; but that it might be correct. It would also be good to add tests for useLazyQuery.

The tests can be written passing with UNNEEDED_FRAMEs to indicate that optimization is possible. If the hook is optimized the tests can simply be updated by removing the UNNEEDED_FRAME markers. I think I can find some time to expand these tests but there might be interesting cases that I don't know about. Are you interested in having these tests (and supporting them going forward)?

PS.
I just remembered that expectFrames can be improved so that it asserts that UNNEEDED_FRAME is equal to the last frame. That might prevent unintended regressions from slipping in.

@benjamn
Copy link
Member

benjamn commented Mar 10, 2022

Okay, great! I'm all for adding more passing tests that help codify the current behavior (as long as they're not difficult to update when we deliberately adjust the behavior).

Comment on lines +95 to +101
await waitFor(() => result.current.loading === false);
rerender({ children: null });
expectFrames(result, [
{ loading: true, data: void 0 },
{ loading: false, data: { hello: "world" } },
UNNEEDED_FRAME
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FritsvanCampen Looking more closely at this code, the extra frame results from calling rerender({ children: null }) after waiting for result.current.loading to be false (which accounts for the first two results).

If I understand correctly, since you're calling the React rerender function here, the renderHook is definitely going to run again, so there's not much Apollo Client can do to keep useQuery from being called again when the rerender happens.

What are you proposing useQuery should do when it's called in that case? Returning the same object it returned last time (the UNNEEDED_FRAME) seems like a reasonable behavior, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted, I think you're right. In this case there is no UNNEEDED_FRAME. I think I made a mistake copy-pasting from the referential stability test. There still an UNNEEDED_FRAME in the other test though.

@hwillson hwillson added this to the Release 3.6 milestone Mar 29, 2022
@benjamn benjamn deleted the branch apollographql:release-3.6 April 5, 2022 21:47
@benjamn benjamn closed this Apr 5, 2022
@benjamn benjamn reopened this Apr 14, 2022
@benjamn benjamn changed the base branch from useQuery-internal-state-ref to release-3.6 April 14, 2022 21:38
benjamn added a commit that referenced this pull request Apr 14, 2022
This removes the UNNEEDED_FRAME from the second test added in PR #9508,
which currently causes the test to fail, though the version without the
UNNEEDED_FRAME is how we ultimately want useQuery to behave.

I believe the other test is correct to have the repeated frame:
#9508 (comment)
benjamn added a commit that referenced this pull request Apr 14, 2022
@benjamn
Copy link
Member

benjamn commented Apr 14, 2022

Sorry for the accidental close! This should soon be fixed (at last) by #9599.

benjamn added a commit that referenced this pull request Apr 18, 2022
…-frame

Fix extra `useQuery` result frames (as demonstrated by #9508).
@benjamn
Copy link
Member

benjamn commented Apr 18, 2022

This should be fixed now in @apollo/client@3.6.0-rc.0, thanks to #9599. Please try updating with

npm i @apollo/client@beta

when you have the chance!

We are targeting the final release for v3.6 for the end of this week.

@FritsvanCampen
Copy link
Contributor Author

The 3.6.x release has resolved this issue. Thanks for the fix!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants