diff --git a/packages/test-e2e-composable-vue3/package.json b/packages/test-e2e-composable-vue3/package.json index 5d3a3e60..e26496fc 100644 --- a/packages/test-e2e-composable-vue3/package.json +++ b/packages/test-e2e-composable-vue3/package.json @@ -20,6 +20,7 @@ "@vue/apollo-util": "workspace:*", "graphql": "^16.7.1", "graphql-tag": "^2.12.6", + "pinia": "^2.1.6", "test-server": "workspace:*", "vue": "^3.3.4", "vue-router": "^4.2.4" diff --git a/packages/test-e2e-composable-vue3/src/components/App.vue b/packages/test-e2e-composable-vue3/src/components/App.vue index 7c8a948a..76698d7f 100644 --- a/packages/test-e2e-composable-vue3/src/components/App.vue +++ b/packages/test-e2e-composable-vue3/src/components/App.vue @@ -1,22 +1,21 @@ + + - - diff --git a/packages/test-e2e-composable-vue3/src/components/ChannelListPinia.vue b/packages/test-e2e-composable-vue3/src/components/ChannelListPinia.vue new file mode 100644 index 00000000..73f21db7 --- /dev/null +++ b/packages/test-e2e-composable-vue3/src/components/ChannelListPinia.vue @@ -0,0 +1,43 @@ + + + diff --git a/packages/test-e2e-composable-vue3/src/components/ChannelListPinia2.vue b/packages/test-e2e-composable-vue3/src/components/ChannelListPinia2.vue new file mode 100644 index 00000000..02be1694 --- /dev/null +++ b/packages/test-e2e-composable-vue3/src/components/ChannelListPinia2.vue @@ -0,0 +1,51 @@ + + + diff --git a/packages/test-e2e-composable-vue3/src/main.ts b/packages/test-e2e-composable-vue3/src/main.ts index e3feb6e7..7bf9b916 100644 --- a/packages/test-e2e-composable-vue3/src/main.ts +++ b/packages/test-e2e-composable-vue3/src/main.ts @@ -1,15 +1,13 @@ -import { createApp, h, provide } from 'vue' +import { createApp } from 'vue' import { DefaultApolloClient } from '@vue/apollo-composable' +import { createPinia } from 'pinia' import { apolloClient } from './apollo' import App from './components/App.vue' import { router } from './router' import '@/assets/styles/tailwind.css' -const app = createApp({ - setup () { - provide(DefaultApolloClient, apolloClient) - }, - render: () => h(App), -}) +const app = createApp(App) +app.use(createPinia()) app.use(router) +app.provide(DefaultApolloClient, apolloClient) app.mount('#app') diff --git a/packages/test-e2e-composable-vue3/src/router.ts b/packages/test-e2e-composable-vue3/src/router.ts index 74e387be..6fd05f5a 100644 --- a/packages/test-e2e-composable-vue3/src/router.ts +++ b/packages/test-e2e-composable-vue3/src/router.ts @@ -50,5 +50,19 @@ export const router = createRouter({ path: '/null-query', component: () => import('./components/NullQuery.vue'), }, + { + path: '/pinia', + component: () => import('./components/ChannelListPinia.vue'), + meta: { + layout: 'blank', + }, + }, + { + path: '/pinia2', + component: () => import('./components/ChannelListPinia2.vue'), + meta: { + layout: 'blank', + }, + }, ], }) diff --git a/packages/test-e2e-composable-vue3/src/stores/channel.ts b/packages/test-e2e-composable-vue3/src/stores/channel.ts new file mode 100644 index 00000000..ab02e011 --- /dev/null +++ b/packages/test-e2e-composable-vue3/src/stores/channel.ts @@ -0,0 +1,33 @@ +import gql from 'graphql-tag' +import { useQuery } from '@vue/apollo-composable' +import { defineStore } from 'pinia' +import { computed, watch } from 'vue' + +interface Channel { + id: string + label: string +} + +export const useChannels = defineStore('channel', () => { + const query = useQuery<{ channels: Channel[] }>(gql` + query channels { + channels { + id + label + } + } + `) + + const channels = computed(() => query.result.value?.channels ?? []) + + watch(query.loading, value => { + console.log('loading', value) + }, { + immediate: true, + }) + + return { + loading: query.loading, + channels, + } +}) diff --git a/packages/test-e2e-composable-vue3/tests/e2e/specs/pinia.cy.ts b/packages/test-e2e-composable-vue3/tests/e2e/specs/pinia.cy.ts new file mode 100644 index 00000000..b6762499 --- /dev/null +++ b/packages/test-e2e-composable-vue3/tests/e2e/specs/pinia.cy.ts @@ -0,0 +1,19 @@ +describe('Pinia', () => { + beforeEach(() => { + cy.task('db:reset') + }) + + it('with current instance', () => { + cy.visit('/pinia') + cy.get('.channel-link').should('have.lengthOf', 2) + cy.contains('.channel-link', '# General') + cy.contains('.channel-link', '# Random') + }) + + it('with effect scope only', () => { + cy.visit('/pinia2') + cy.get('.channel-link').should('have.lengthOf', 2) + cy.contains('.channel-link', '# General') + cy.contains('.channel-link', '# Random') + }) +}) diff --git a/packages/vue-apollo-composable/src/useApolloClient.ts b/packages/vue-apollo-composable/src/useApolloClient.ts index 3f0bdc31..cee3e60a 100644 --- a/packages/vue-apollo-composable/src/useApolloClient.ts +++ b/packages/vue-apollo-composable/src/useApolloClient.ts @@ -1,4 +1,4 @@ -import { getCurrentInstance, inject } from 'vue-demi' +import { getCurrentInstance, getCurrentScope, inject } from 'vue-demi' import { ApolloClient } from '@apollo/client/core/index.js' export const DefaultApolloClient = Symbol('default-apollo-client') @@ -35,7 +35,7 @@ export function useApolloClient (clientId?: ClientId): UseApo // Save current client in current closure scope const savedCurrentClients = currentApolloClients - if (!getCurrentInstance()) { + if (!getCurrentInstance() && !getCurrentScope()) { resolveImpl = (id?: ClientId) => { if (id) { return resolveClientWithId(savedCurrentClients, id) @@ -68,7 +68,7 @@ export function useApolloClient (clientId?: ClientId): UseApo throw new Error( `Apollo client with id ${ id ?? 'default' - } not found. Use provideApolloClient() if you are outside of a component setup.`, + } not found. Use an app.runWithContext() or provideApolloClient() if you are outside of a component setup.`, ) } return client diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d64cb2fa..b3f631fa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -189,6 +189,9 @@ importers: graphql-tag: specifier: ^2.12.6 version: 2.12.6(graphql@16.7.1) + pinia: + specifier: ^2.1.6 + version: 2.1.6(typescript@5.0.2)(vue@3.3.4) test-server: specifier: workspace:* version: link:../test-server @@ -12834,6 +12837,24 @@ packages: engines: {node: '>=6'} dev: true + /pinia@2.1.6(typescript@5.0.2)(vue@3.3.4): + resolution: {integrity: sha512-bIU6QuE5qZviMmct5XwCesXelb5VavdOWKWaB17ggk++NUwQWWbP5YnsONTk3b752QkW9sACiR81rorpeOMSvQ==} + peerDependencies: + '@vue/composition-api': ^1.4.0 + typescript: '>=4.4.4' + vue: '*' + peerDependenciesMeta: + '@vue/composition-api': + optional: true + typescript: + optional: true + dependencies: + '@vue/devtools-api': 6.5.0 + typescript: 5.0.2 + vue: 3.3.4 + vue-demi: 0.14.6(@vue/composition-api@1.0.0)(vue@3.3.4) + dev: false + /pirates@4.0.5: resolution: {integrity: sha512-8V9+HQPupnaXMA23c5hvl69zXvTwTzyAYasnkb0Tts4XvO4CliqONMOnvlq26rkhLC3nWDFBJf73LU1e1VZLaQ==} engines: {node: '>= 6'} @@ -15560,7 +15581,6 @@ packages: resolution: {integrity: sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==} engines: {node: '>=12.20'} hasBin: true - dev: true /uglify-es@3.3.9: resolution: {integrity: sha512-r+MU0rfv4L/0eeW3xZrd16t4NZfK8Ld4SWVglYBb7ez5uXFWHuVRs6xCTrf1yirs9a4j4Y27nn7SRfO6v67XsQ==}