apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

Make it possible to globally disable ssr in useQuery #175

Open ctretyak opened 4 years ago

ctretyak commented 4 years ago

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

MrLoh commented 4 years ago

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

ctretyak commented 4 years ago

No reaction on ssr: false

sinwailam193 commented 4 years ago

Setting ssr false inside defaultOptions doesn't work apparently

stramel commented 3 years ago

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

lucas-janon commented 2 years ago

Is there any workaround?

pleportz commented 2 years ago

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

lucas-janon commented 2 years ago

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.