defaultOptions for ApolloClient is being ignored (awaitRefetchQueries)

Author: BoralesCreated Dec 14, 2019Updated Jul 15, 2020

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:

javascript
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

Source: apollographql/react-apollo