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 executing standby queries in ssr #9382

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ class InternalState<TData, TVariables> {
}), [obsQuery]);

const ssrAllowed = !(
this.watchQueryOptions.fetchPolicy === 'standby' ||
this.queryHookOptions.ssr === false ||
this.queryHookOptions.skip
);
Expand Down
31 changes: 31 additions & 0 deletions src/react/ssr/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,37 @@ describe('useLazyQuery Hook SSR', () => {
]
};

it('should not execute the query and properly render the component', () => {
const link = mockSingleLink({
request: { query: CAR_QUERY },
result: { data: CAR_RESULT_DATA }
});

const client = new ApolloClient({
cache: new InMemoryCache(),
link,
ssrMode: true
});

let isRendered = false;
const Component = () => {
useLazyQuery(CAR_QUERY);
isRendered = true;
return null;
};

const app = (
<ApolloProvider client={client}>
<Component />
</ApolloProvider>
);

return renderToStringWithData(app).then(markup => {
expect(isRendered).toBeTruthy();
expect(markup).toEqual('');
});
});

it('should run query only after calling the lazy mode execute function', () => {
const link = mockSingleLink({
request: { query: CAR_QUERY },
Expand Down
35 changes: 35 additions & 0 deletions src/react/ssr/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ describe('useQuery Hook SSR', () => {
});
});

it('should skip SSR tree rendering if `fetchPolicy` option is `standby`', () => {
const link = mockSingleLink({
request: { query: CAR_QUERY },
result: { data: CAR_RESULT_DATA }
});

const client = new ApolloClient({
cache: new InMemoryCache(),
link,
ssrMode: true
});

let renderCount = 0;
const Component = () => {
const { data, loading } = useQuery(CAR_QUERY, { fetchPolicy:'standby' });
renderCount += 1;
if (!loading && data) {
const { make } = data.cars[0];
return <div>{make}</div>;
}
return null;
};

const app = (
<ApolloProvider client={client}>
<Component />
</ApolloProvider>
);

return renderToStringWithData(app).then(result => {
expect(renderCount).toBe(1);
expect(result).toEqual('');
});
});

it('should skip both SSR tree rendering and SSR component rendering if `ssr` option is `false` and `ssrMode` is `true`',
async () => {
const link = mockSingleLink({
Expand Down