Skip to content

Commit aac57b3

Browse files
alunyovfacebook-github-bot
authored andcommitted
useFragmentInternal should suspend if environment changes and there is a query in flight
Summary: There is a bug in the new hooks implementation, where the `useFragmentInternal` would not suspend after the change of the environment (while the fragment selector are the same) with the active operation in flight. This diff fixes that by checking if the environment has changed and we would throw the promise for pending operation in flight. Reviewed By: voideanvalue Differential Revision: D52516772 fbshipit-source-id: 13a61114e6b7718af548cf45d2e4685bcd121993
1 parent 1972913 commit aac57b3

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/react-relay/relay-hooks/__tests__/useFragmentNode-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,33 @@ describe.each([
661661
]);
662662
});
663663

664+
it('should supsend when the environment changes and there is query in flight', () => {
665+
const renderer = renderSingularFragment();
666+
assertFragmentResults([
667+
{
668+
data: {
669+
id: '1',
670+
name: 'Alice',
671+
profile_picture: null,
672+
...createFragmentRef('1', singularQuery),
673+
},
674+
},
675+
]);
676+
677+
const newEnvironment = createMockEnvironment();
678+
679+
internalAct(() => {
680+
// Let there be an operation in flight
681+
fetchQuery(newEnvironment, singularQuery).subscribe({});
682+
683+
setEnvironment(newEnvironment);
684+
});
685+
686+
// It should suspend when the environment changes and there is a query
687+
// in flight.
688+
expect(renderer.toJSON()).toEqual('Singular Fallback');
689+
});
690+
664691
it('should re-read and resubscribe to fragment when fragment pointers change', () => {
665692
renderSingularFragment();
666693
assertRenderBatch([

packages/react-relay/relay-hooks/experimental/useFragmentInternal_EXPERIMENTAL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ function useFragmentInternal_EXPERIMENTAL(
505505
// We only suspend when the component is first trying to mount or changing
506506
// selectors, not if data becomes missing later:
507507
if (
508+
environment !== previousEnvironment ||
508509
!committedFragmentSelectorRef.current ||
509510
!areEqualSelectors(committedFragmentSelectorRef.current, fragmentSelector)
510511
) {

0 commit comments

Comments
 (0)