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

Fix failing regression test for skipped SSR useQuery stuck in standby #9677

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
- Update `ts-invariant` to version 0.10.2 to fix source map warnings. <br/>
[@benjamn](https://github.com/benjamn) in [#9672](https://github.com/apollographql/apollo-client/pull/9672)

- Test that `useQuery` queries with `skip: true` do not stall server-side rendering. <br/>
[@nathanmarks](https://github.com/nathanmarks) and [@benjamn](https://github.com/benjamn) in [#9677](https://github.com/apollographql/apollo-client/pull/9677)

## Apollo Client 3.6.2 (2022-05-02)

### Bug Fixes
Expand Down
14 changes: 7 additions & 7 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ class InternalState<TData, TVariables> {
subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
}), [obsQuery]);

if (this.renderPromises) {
this.renderPromises.registerSSRObservable(obsQuery);
const ssrAllowed = !(
this.queryHookOptions.ssr === false ||
this.queryHookOptions.skip
);

const ssrAllowed = !(
this.queryHookOptions.ssr === false ||
this.queryHookOptions.skip
);
if (this.renderPromises && ssrAllowed) {
this.renderPromises.registerSSRObservable(obsQuery);

if (ssrAllowed && obsQuery.getCurrentResult().loading) {
if (obsQuery.getCurrentResult().loading) {
// TODO: This is a legacy API which could probably be cleaned up
this.renderPromises.addObservableQueryPromise(obsQuery);
}
Expand Down
50 changes: 50 additions & 0 deletions src/react/ssr/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,56 @@ describe('useQuery Hook SSR', () => {
});
});

it('should render SSR tree rendering if `skip` option is `true` for only one instance of the query', async () => {
let renderCount = 0;

const AnotherComponent = () => {
const {
loading,
data,
} = useQuery(CAR_QUERY, { skip: false });

renderCount += 1;

if (!loading) {
expect(data).toEqual(CAR_RESULT_DATA);
const { make, model, vin } = data.cars[0];
return (
<div>
{make}, {model}, {vin}
</div>
);
}

return null;
};

const Component = () => {
const {
loading,
data,
} = useQuery(CAR_QUERY, { skip: true });
renderCount += 1;

expect(loading).toBeFalsy();
expect(data).toBeUndefined();

return <AnotherComponent />;
};

const app = (
<MockedProvider mocks={CAR_MOCKS}>
<Component />
</MockedProvider>
);

return renderToStringWithData(app).then(result => {
expect(renderCount).toBe(4);
expect(result).toMatch(/Audi/);
expect(result).toMatch(/RS8/);
});
});

it('should return data written previously to cache during SSR pass if using cache-only fetchPolicy', async () => {
const cache = new InMemoryCache({
typePolicies: {
Expand Down