Apollo-module: defaultOptions is not configurable

Created on 25 Sep 2018  路  10Comments  路  Source: nuxt-community/apollo-module

What problem does this feature solve?

I'd like to set defaultOptions like this.
But it does't seem to be configurable.

Any plan of adding this feature?

Thanks in advance.

This feature request is available on Nuxt community (#c142)
feature-request

Most helpful comment

First off, thanks to @robsontenorio for getting this in, I just wasted a good chunk of my day trying to disable apollo's cache and this thread helped quite a bit. However, it seems that while this PR fixed the core issue, the example provided in the readme didn't translate well into the the multi-client setup i have in my app. I tried adding the the defaultOptions to the main apollo config in nuxt.config.js, but found that they didn't show up when i inspected the particular client i was using. The solution was to add the default options to the client config like so...

nuxt.config.js

apollo: {
    clientConfigs: {
        default: '~/client-config.ts'
    },
},

client-config.ts

import { InMemoryCache } from "apollo-cache-inmemory";

export default function(){
    return {
        cache: new InMemoryCache(),
        apollo: {
            defaultOptions: {
                watchQuery: {
                    fetchPolicy: 'no-cache',
                },
                query: {
                    fetchPolicy: 'no-cache',
                },
                mutate: {
                    fetchPolicy: 'no-cache',
                },
            },
        }
    }
}

Hopefully this helps the next person.

All 10 comments

I won't have time to implement it. But feel free to open a PR

Is there currently a way to set defaultOptions via client configuration file?

This's works for me

  {
    httpEndpoint: 'http://xxxxxxxxxxxx',

    apollo: {
      defaultOptions: {
        query: {
          fetchPolicy: 'network-only'
        }
      }
    }
  }

"@nuxtjs/apollo": "^4.0.0-rc2.3"

Thanks @lin-yen - That's where I landed too. I was hoping to be able to configure the fetchPolicy for an entire client from the configuration file.

@lin-yen As I can see vue-apollo uses provider.defaultOptions instead of client.defaultOptions when building a smart query (proof).

There is no way to change provider.defaultOptions via nuxt.config.js (at least now), so I added a plugin:

// plugins/apollo-overrides.js
export default ({ app }, inject) => {
  app.apolloProvider.defaultOptions = {
    $query: {
      fetchPolicy: 'no-cache'
    }
  }
}

Is there a better way to change provider.defaultOptions?

ps. I'm totally new to nuxt/vue and vue-apollo

@ndan thats the preferred way for now 馃憤

@dohomi Would you accept a PR for this? Would be very useful to set defaultOptions on nuxtjs.config.js

https://www.apollographql.com/docs/react/api/apollo-client.html#apollo-client

 watchQuery: {
    fetchPolicy: 'cache-and-network',
    errorPolicy: 'ignore',
  },
  query: {
    fetchPolicy: 'network-only',
    errorPolicy: 'all',
  },
  mutate: {
    errorPolicy: 'all'
  }

@robsontenorio yes go ahead. Currently I'm very busy in job so I might some longer time to review. I also still wait for the Vue 2.6 release which will fix SSR issues of this package and then I hope the integration is smooth for all users

First off, thanks to @robsontenorio for getting this in, I just wasted a good chunk of my day trying to disable apollo's cache and this thread helped quite a bit. However, it seems that while this PR fixed the core issue, the example provided in the readme didn't translate well into the the multi-client setup i have in my app. I tried adding the the defaultOptions to the main apollo config in nuxt.config.js, but found that they didn't show up when i inspected the particular client i was using. The solution was to add the default options to the client config like so...

nuxt.config.js

apollo: {
    clientConfigs: {
        default: '~/client-config.ts'
    },
},

client-config.ts

import { InMemoryCache } from "apollo-cache-inmemory";

export default function(){
    return {
        cache: new InMemoryCache(),
        apollo: {
            defaultOptions: {
                watchQuery: {
                    fetchPolicy: 'no-cache',
                },
                query: {
                    fetchPolicy: 'no-cache',
                },
                mutate: {
                    fetchPolicy: 'no-cache',
                },
            },
        }
    }
}

Hopefully this helps the next person.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lindesvard picture lindesvard  路  7Comments

SuddenlyRust picture SuddenlyRust  路  5Comments

Coppola-Aleandro picture Coppola-Aleandro  路  3Comments

kieusonlam picture kieusonlam  路  9Comments

appinteractive picture appinteractive  路  3Comments