react-apollo icon indicating copy to clipboard operation
react-apollo copied to clipboard

defaultOptions for ApolloClient is being ignored (awaitRefetchQueries)

Open Borales opened this issue 5 years ago • 10 comments

Intended outcome:

According to the docs section - https://www.apollographql.com/docs/react/api/apollo-client/#optional-fields defaultOptions provides application-wide default values for options.

So by providing the following code:

const client = new ApolloClient({
  ...,

  defaultOptions: {
    mutate: {
      awaitRefetchQueries: true
    }
  }
});

I'd expect awaitRefetchQueries flag to be passed to all my mutations. The reproduce section describes how to get the issue.

Actual outcome:

This is caused by a ApolloClient.mutate function with a predefined value for awaitRefetchQueries = false here (lines 87 and 104):

https://github.com/apollographql/react-apollo/blob/b2a0a2528c44610f41d38f59c39970fa52899532/packages/hooks/src/data/MutationData.ts#L78-L111

but when ApolloClient merges the provided options param with the defaultOptions here - it's too late, it'll always remain awaitRefetchQueries = false.

How to reproduce the issue:

https://codesandbox.io/s/apollo-client-error-template-c0zj8

  • 2 buttons represent mutation calls with and without the overridden awaitRefetchQueries flag
  • the console tab shows the order of the events

Previously confirmed on https://github.com/apollographql/react-apollo/issues/2643#issuecomment-564921568

Version

  System:
    OS: macOS Mojave 10.14.6
  Binaries:
    Node: 12.13.1 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
  Browsers:
    Chrome: 79.0.3945.79
    Firefox: 71.0
    Safari: 13.0.4
  npmPackages:
    @apollo/react-hoc: ^3.1.3 => 3.1.3
    @apollo/react-hooks: ^3.1.3 => 3.1.3
    apollo-client: 2.6.4 => 2.6.4

Borales avatar Dec 14 '19 20:12 Borales

anyone?

Borales avatar Jan 21 '20 07:01 Borales

Other defaultOptions are being overridden/ignored as well. I'm initializing my ApolloClient with:

    defaultOptions: {
      watchQuery: {
        fetchPolicy: 'no-cache',
        errorPolicy: 'ignore',
      },
      query: {
        fetchPolicy: 'no-cache',
        errorPolicy: 'all',
      },
    }

However, these options seem to be completely ignored when using useQuery.

yelvert avatar Jan 24 '20 05:01 yelvert

the same for me

o-tkach avatar Mar 17 '20 15:03 o-tkach

Bump

zaguiini avatar Apr 07 '20 15:04 zaguiini

Hi @jbaxleyiii @hwillson Is there any chance to get this fixed for a patch release? It's been almost 4 months now.

Borales avatar Apr 07 '20 15:04 Borales

This is causing me a headache as well. Anyone?

floyd-may avatar Apr 27 '20 16:04 floyd-may

For now, I'm using a workaround like this for default context, for example:

export const defaultContextSetterLink = () => {
  return setContext((_, { someContextPiece }) => {
    return {
      someContextPiece: someContextPiece ?? 'DEFAULT_VALUE'
    }
  })
}

const links = [
  defaultContextSetterLink(),
  ...otherLinks
]

zaguiini avatar May 06 '20 18:05 zaguiini

I believe refetchQueries should also be used with defaultOptions whenever the fix comes

mrjamesjcho avatar Jun 04 '20 18:06 mrjamesjcho

There seems to be an issue with the constructor. A "fix" is to assign the client.defaultOptions after the ApolloClient is created:

	const client = new ApolloClient({
		credentials: 'include',
	})

	client.defaultOptions = {
		watchQuery: {
			fetchPolicy: 'network-only',
		},
	}

jimdwyerdev avatar Jun 23 '20 16:06 jimdwyerdev

it's been ~84 years~ 7 months since this issue was reported, any decision/suggestions from the Apollo team?

Borales avatar Jul 15 '20 07:07 Borales