I tried setting defaultOptions
like this:
export const apolloClient = new ApolloClient({
link,
cache,
connectToDevTools: true,
defaultOptions: {
watchQuery: {
fetchPolicy: 'cache-and-network'
},
query: {
fetchPolicy: 'cache-and-network'
}
}
})
and yet re-rendering of components does not trigger refetching, however manually setting fetchPolicy
option on a particular component does achieve the intended result
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions to Apollo Client!
@jbaxleyiii is it possible to get some updates on this?
I'm interested in this as well--right now I have to set the fetchPolicy
on every request.
not working for me neither
I'm not a fan of flooding github with +1's, but I have this exact issue as well.
Just opened https://github.com/apollographql/apollo-client/pull/2748 which I think should address the issue described here.
@tgriesser Sorry I dont understand why this not working, even i only set this.
defaultOptions: {
watchQuery: {
fetchPolicy: 'cache-and-network'
}
}
You have to declare defaultOptions as a constant like this:
const defaultOptions = {
watchQuery: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'all',
},
query: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all',
}
}
const client = new ApolloClient({
link: from([
logoutLink,
errorsLink,
httpLink
]),
cache: new InMemoryCache(),
defaultOptions
});
I am having the same issue.
I'm seeing the same issue with network-only
as well.
If I use the following defaultOptions
, the cache still seems to be in effect:
const defaultOptions = {
watchQuery: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all',
},
};
However if I supply fetchPolicy: 'network-only'
as an option to an individual query, the cache is not used (as expected).
+1
I can confirm that the defaultOptions are still not working with apollo-client 2.3.5 (also see #3256 for a related/duplicate issue and some findings from @zamiang).
Same here, defaultOptions
doesn't work
Hi all - would anyone here be able to provide a small runnable reproduction that clearly demonstrates this issue? Thanks!
@hwillson in terms of reproduction steps, I'd need to sanitise some of the production code we have, but long story short, when we try to do something like the following, it doesn't work for us - the cache is used:
const defaultOptions = {
watchQuery: {
fetchPolicy: 'network-only',
errorPolicy: 'ignore',
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
}
const apollo = new ApolloClient({
defaultOptions,
// I tried either initialising a cache like this:
// cache: new InMemoryCache(),
// or not defining the cache key on the object. Both don't work for me
uri: '/graphql/',
})
// then try to run a query directly using the apollo client
// it does not do a new network request
// instead it uses the cache
const entity_id = 1
apollo.query({
query: load_some_info_query,
variables: { entity_id },
})
// it is possible to force a network request if fetchPolicy is set on each query
apollo.query({
query: load_some_info_query,
variables: { entity_id },
fetchPolicy: 'network-only',
})
apollo-client version: 2.4.0
node version 8.11.3
+1
are default props that hard to fix? its been 1 year and 1 day lol.
+1
@kcvin94 It sounds very easy for you to fix them. Mind send a PR?
Same issue
Okay, guys, I think the reason is you should provide all 3 properties together (watchQuery, query and mutate):
This code works
const defaultOptions = {
watchQuery: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'ignore',
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all'
}
}
This code doesn't affect to default options:
const defaultOptions = {
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all'
}
}
or...
const defaultOptions = {
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
}
}
@helios1138 @stefanholzapfel @sfilipov @hwillson please check it. It works for me.
@hwillson I think it would be much better if I can specify only one property for default options. Because now I should provide all 3 necessary properties.
PS: I don't use watchQuery
but default option of watchQuery
affects on watch
@daryn-k Confirm your code works馃帀 seems you have to declare all three of those fields to make defaultOptions works.
I can't get defaultOptions
to work for anything.
I went looking in the source code. The options (specifically context
in my case) get passed into the various methods of QueryManager
and are included if hasClientExports
rings true.
const updatedVariables: OperationVariables =
hasClientExports(mutation)
? await this.localState.addExportedVariables(
mutation,
variables,
context,
)
: variables;
Problem is, hasClientExports
is also looking for the @export
directive.
export function hasClientExports(document: DocumentNode) {
return (
document &&
hasDirectives(['client'], document) &&
hasDirectives(['export'], document)
);
}
Or am I reading this wrong?
@daryn-k it seems that when we mean to set fetchPolicy
for queries, we actually need to set watchQuery.fetchPolicy
. I think it's a bit confusing and should be described better in the documentation, but it works (at least on the latest version of Apollo Client).
And in this case, I'm not sure what query.fetchPolicy
is supposed to be about
My config currently just contains
defaultOptions: {
watchQuery: { fetchPolicy: 'cache-and-network' },
},
Thanks for reporting this. There hasn't been any activity here in quite some time, so we'll close this issue for now. If this is still a problem (using a modern version of Apollo Client), please let us know. Thanks!
Hi :)
The issue is still the same in the latest versions:
"@apollo/react-hooks": "3.1.2",
"apollo-boost": "0.4.4",
The following does successfully use network-only
:
defaultOptions = {
watchQuery: { fetchPolicy: 'network-only', errorPolicy: 'all' },
query: { fetchPolicy: 'network-only', errorPolicy: 'all' },
mutate: { errorPolicy: 'all' },
}
This (which would be much nicer) does not work and uses the cache:
defaultOptions = {
query: { fetchPolicy: 'network-only' },
}
Still have the same issue:
"@apollo/client": "3.0.0-beta.43"
React client side app
const client = new ApolloClient({
link: concat(authMiddleware, httpLink),
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: { errorPolicy: "all" },
mutate: { errorPolicy: "all" },
query: { errorPolicy: "all" },
},
})
Default options not working
If if provide errorPolicy to the useMutation hook, then it works?
I also have the same issue:
"@apollo/client": "3.0.0-beta.41"
If I provide errorPolicy
in useMutation
and useQuery
it works but it would be great if I didn't have to do it for every mutation and query.
This may be related but I would also want refetchQueries
to use defaultOptions
@jbaxleyiii i think this needs to be reopened?
It's possible this PR introduced the error.
https://github.com/apollographql/apollo-client/pull/5863/files
From what I can tell, if you do not define an errorPolicy
on the mutation itself, then an object with
{
errorPolicy: undefined
}
gets passed in here (line 101 of MutationData.ts):
https://github.com/apollographql/apollo-client/pull/5863/files#diff-93aa5dbc2696fb1257210a16aeac6e82L101
In ApolloClient
, it will merge errorPolicy: undefined
with the default errorPolicy
set on ApolloClient
, effectively wiping out the top-level default:
ie. options
passed to this function will be { ..., errorPolicy: undefined, ... }
ApolloClient.prototype.mutate = function (options) {
if (this.defaultOptions.mutate) {
options = __assign(__assign({}, this.defaultOptions.mutate), options);
}
return this.queryManager.mutate(options);
};
cc: @jbaxleyiii
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',
},
}
Most helpful comment
Okay, guys, I think the reason is you should provide all 3 properties together (watchQuery, query and mutate):
This code works
This code doesn't affect to default options:
or...
@helios1138 @stefanholzapfel @sfilipov @hwillson please check it. It works for me.