apollo-feature-requests
apollo-feature-requests copied to clipboard
Make it possible to globally disable ssr in useQuery
I would like all my requests to be without SSR, except for those in which I explicitly indicate
@ctretyak isn't this possible with defaultOptions https://www.apollographql.com/docs/react/api/apollo-client/#example-defaultoptions-object
No reaction on ssr: false
Setting ssr false inside defaultOptions doesn't work apparently
Would love to see this added as an option of defaultOptions
object.
Is there any workaround?
@lucas-janon As a workaround we override the useQuery
hook:
import {
DocumentNode,
QueryHookOptions,
QueryResult,
TypedDocumentNode,
useQuery,
} from '@apollo/client';
export const useApolloQuery = <TData, TVariables>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options?: QueryHookOptions<TData, TVariables>
): QueryResult<TData, TVariables> => {
return useQuery(query, {
ssr: false,
fetchPolicy: 'cache-and-network',
...options,
});
};
Then we use useApolloQuery
everywhere instead of useQuery
.
Because old habits die hard we added an eslint rule to warn the dev team when using useQuery
.
Note: we used to pass fetchPolicy: 'cache-and-network',
through defaultOptions
but it seems to be currently broken (issue: https://github.com/apollographql/apollo-client/issues/9105)
Thank you @pleportz!
The exact same solution isn't possible for us, since we're using graphql-codegen
.
But you gave me a great idea: the new version of @graphql-codegen/typescript-react-apollo
includes a defaultBaseOptions config, adding ssr: false
to it does the trick. Even if the query has its own baseOptions
, since they get merged.