I'm trying to use @urql/vue
to perform graphql queries
outside of vue3 setup(), using vuex for example. On vue2 I used the apollo-client this way and it worked normally.
This is to be able to carry out the reuse of queries with greater ease. But an error returns to me that I don't know if there is any way around it, has anyone been through this and managed to resolve it?
urql-vue.mjs?091e:36 Uncaught (in promise) Error: use* functions may only be called during the `setup()` or other lifecycle hooks.
this is my client definition: client.js
import { createClient } from '@urql/vue';
import { DEV_SERVER_URI } from '../../../../../config/environment/Constants';
const client = createClient({
url: DEV_SERVER_URI,
requestPolicy: 'cache-and-network',
});
export default client;
App.js
import { createApp } from 'vue';
import urql from '@urql/vue';
import App from './App.vue';
import router from './router';
import store from './store';
import clientGql from './services/api/graphql/config/client';
(async function () {
// This is where I want to call my vuex action.
const user = await store.dispatch('initpayload/setInitPayload');
}());
const app = createApp(App);
app.use(urql, clientGql);
app.use(store);
app.use(router);
app.mount('#app');
initpayload.actions.js
import InitPayloadService from '@/services/api/graphql/modules/initpayload';
export default {
setInitPayload: ({ commit }) => {
console.log('setInitPayload');
return InitPayloadService
.getInitPayload()
.then((res) => {
const { aboutMe } = res.data.getInitPayload;
return aboutMe;
});
},
};
this is my service: initpayload.service.js
import { useQuery } from '@urql/vue';
import initPayloadQueries from './initpayload.queries';
export default {
getInitPayload() {
return useQuery({
query: initPayloadQueries.GET_INIT_PAYLOAD,
});
},
};
dependencies used:
package.json
"dependencies": {
"@urql/vue": "^0.4.0",
"core-js": "^3.6.5",
"graphql": "^15.5.0",
"vue": "^3.0.0",
"vue-router": "^4.0.0-0",
"vuex": "^4.0.0-0"
},
Is there a way to solve this, so that we can be using the use* functions in this way?