Apollo-client: defaultOptions still not working

Created on 3 Apr 2018  路  11Comments  路  Source: apollographql/apollo-client

I tried to update apollo-client to the latest version 2.2.8, and try to set the defaultOptions for all request like below:

const defaultOptions = {
  watchQuery: {
    fetchPolicy: 'network-only',
    errorPolicy: 'all',
  },
  query: {
    fetchPolicy: 'network-only',
    errorPolicy: 'all',
  },
  mutate: {
    errorPolicy: 'all',
  },
};
const client = new ApolloClient({
  link,
  cache: new InMemoryCache(),
  defaultOptions
})

But everything does not change.

But the fetchPolicy I defined in the query is worked, like

options: {
    fetchPolicy: 'network-only'
}

Any ideas about it?

needs-reproduction 馃毀 in-triage

Most helpful comment

Can confirm that the method I linked above from ApolloClient continues to not be called ever in my app. We are using the higher order component style of using apollo-client and not the new query component. In QueryManger.fetchQuery - the options passed into that method do include options set via defaultOptions and then QueryManager.fetchQuery doesn't utilize the default options at all.

All 11 comments

Default options is still not working, specifically with no-cache and network-only. I have a reproduction here that shows different results when defaultOptions is used vs setting the fetchPolicy prop on the Query component directly.

https://codesandbox.io/s/4zmopq25o0

NOTE: this is using the Query component on 2.3.1 AND the latest react-apollo

   // package.json
    "apollo-cache-inmemory": "1.2.1",
    "apollo-client": "2.3.1",
    "apollo-codegen": "0.19.1",
    "apollo-link": "1.2.2",
    "apollo-link-error": "1.0.9",
    "apollo-link-http": "1.5.4",
    "react-apollo": "2.1.4"

Yea did a bit of digging, and the code that applies default options (and many other settings) is never run. Ran in apollo-client 2.3.2 - 2.3.5.

https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/ApolloClient.ts#L234-L254

The issue outlined in https://github.com/apollographql/apollo-client/issues/3256#issuecomment-389715514 has been addressed (see https://github.com/apollographql/apollo-client/issues/3452#issuecomment-410322219).

@ZiXYu - any chance you can provide a small runnable reproduction that shows your issue?

@zamiang - you mentioned that the default options (and other settings) are never run, but the code sample you pointed to shows them being used. If you still think this is the case, can you please elaborate further?

Thanks all!

Awesome! Yea i guess i should have added a commit hash to that link. The code below was not there before. Can confirm the fix tonight but I very highly suspect this fixed the issue.

    if (this.defaultOptions.query) {
      options = { ...this.defaultOptions.query, ...options } as QueryOptions<
        TVariables
      >;
    }

Have verified that the problem is NOT fixed for us in apollo-client 2.3.7

const defaultOptions = {
  watch: {
    fetchPolicy: 'network-only'
    errorPolicy: 'all' 
  },
  query: {
    fetchPolicy: 'network-only'
    errorPolicy: 'all'
  },
  mutate: {
    errorPolicy: 'all
  },
};

In that case, the fetch policy is not set for queries and apollo-client uses its internal default instead of the one I've passed in.

Can confirm that the method I linked above from ApolloClient continues to not be called ever in my app. We are using the higher order component style of using apollo-client and not the new query component. In QueryManger.fetchQuery - the options passed into that method do include options set via defaultOptions and then QueryManager.fetchQuery doesn't utilize the default options at all.

@zamiang Any chance you provide a small runnable reproduction that demonstrates this?

I have the same problem, it's just this one query that I'm calling and never returns the correct data, any solutions for this?

Reached out to @hwillson via email and offered to demo the described behavior in our business app over a screen-share.

If we aren't able to sync up, I'll post a video of the behavior.

Hi @hwillson @davidsharff, did you manage to get to the bottom of this issue? Or figure out any work arounds? Seeing the same behaviour using apollo-client 2.4.13

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

treecy picture treecy  路  3Comments

kriswep picture kriswep  路  3Comments

stubailo picture stubailo  路  3Comments

gregorskii picture gregorskii  路  3Comments

rafgraph picture rafgraph  路  3Comments