react-apollo
react-apollo copied to clipboard
defaultOptions for ApolloClient is being ignored (awaitRefetchQueries)
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
anyone?
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
.
the same for me
Bump
Hi @jbaxleyiii @hwillson Is there any chance to get this fixed for a patch release? It's been almost 4 months now.
This is causing me a headache as well. Anyone?
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
]
I believe refetchQueries
should also be used with defaultOptions
whenever the fix comes
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',
},
}
it's been ~84 years~ 7 months since this issue was reported, any decision/suggestions from the Apollo team?