apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

Make it possible to globally disable ssr in useQuery

Open ctretyak opened this issue 5 years ago • 7 comments

I would like all my requests to be without SSR, except for those in which I explicitly indicate

ctretyak avatar Sep 16 '19 13:09 ctretyak

@ctretyak isn't this possible with defaultOptions https://www.apollographql.com/docs/react/api/apollo-client/#example-defaultoptions-object

MrLoh avatar Sep 18 '19 17:09 MrLoh

No reaction on ssr: false

ctretyak avatar Oct 16 '19 07:10 ctretyak

Setting ssr false inside defaultOptions doesn't work apparently

sinwailam193 avatar Apr 12 '20 02:04 sinwailam193

Would love to see this added as an option of defaultOptions object.

stramel avatar Feb 19 '21 04:02 stramel

Is there any workaround?

lucas-janon avatar Dec 02 '21 20:12 lucas-janon

@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)

pleportz avatar Dec 02 '21 21:12 pleportz

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.

lucas-janon avatar Dec 06 '21 13:12 lucas-janon