I'm trying to use @vue/apollo-composable with my Nuxt-Ts application. This is the example how it should be injected into root instance on a "normal" Vue application:
import { provide } from '@vue/composition-api'
import { DefaultApolloClient } from '@vue/apollo-composable'
const app = new Vue({
setup () {
provide(DefaultApolloClient, apolloClient)
},
render: h => h(App),
})
Problem: I don't know how to get access to the root instance in Nuxt-TS.
I tried making a plugin, but it's injected either directly into the root instance (which is not right, because @vue/apollo-composable
is using composition-api::provide()
which creates it's own property _provided
.
And if I use nuxt plugin's inject
a $
get's concatenated. And if I write a _provided
object directly in via ctx.app._provided =
it doesn't stick.
import { DefaultApolloClient } from "@vue/apollo-composable";
const myPlugin: Plugin = (context, inject) => {
const defaultClient = ctx.app.apolloProvider.defaultClient;
inject(DefaultApolloClient.toString(), defaultClient) // results in $$ and also composition-api::inject is checking inside `_provided[DefaultApolloClient]`
}
export default myPlugin
I can't call provide()
like in the original example, because it's only allowed inside a VueComponent::setup
function.
I also tried creating a Component and just use it on the page I need it (kind of defeats the purpose of installing in root instance though)
const InstallGraphQl = createComponent({
name: "InstallGraphQl",
setup(_props, ctx: any) {
debugger;
const apolloClient = ctx.app.apolloProvider.defaultClient;
ctx.provide(DefaultApolloClient, apolloClient);
},
});
export default createComponent({
name: "DefaultLayout",
components: {
InstallGraphQl
},
setup(_props, _ctx: SetupContext) {
const { result } = useQuery(SharedLayoutQuery);
return { result };
},
});
but then setup
of the exported components gets called before InstallGraphQl::setup
...
Edit: Also for more information about @vue/apollo-composable
see discussion here: https://github.com/vuejs/vue-apollo/issues/687
Just set a setup()
to the root options:
/* plugins/provide-apollo-client.js */
import {provide} from '@vue/composition-api'
import {DefaultApolloClient} from '@vue/apollo-composable'
export default function ({app}) {
app.setup = () => {
provide(DefaultApolloClient, ...)
}
// Or, use local mixin
app.mixins = (app.mixins || []).concat({
setup () {...},
})
}
/* nuxt.config.js */
export default {
plugins: ['~/plugins/provide-apollo-client'],
}
Not much familiar with nuxt-ts though, but I think the code should just work.