Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use hasInjectionContext in useApolloClient before calling inject #1529

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script lang="ts">
import { apolloClient } from '@/apollo'
import gql from 'graphql-tag'
import { provideApolloClient, useQuery } from '@vue/apollo-composable'
import { ref, defineComponent, hasInjectionContext, effectScope, computed, watch, onBeforeUnmount } from 'vue'

const hello = ref('')
const scope = effectScope()

scope.run(() => {
const query = provideApolloClient(apolloClient)(() => useQuery(gql`
query hello {
hello
}
`))
const innerHello = computed(() => query.result.value?.hello ?? '')

watch(innerHello, (newHello) => {
hello.value = newHello
}, {
immediate: true,
})
})

export default defineComponent({
setup () {
onBeforeUnmount(() => {
scope.stop()
})

return {
hello,
}
},
})
</script>

<template>
<div class="no-setup-scope-query">
{{ hello }}
</div>
</template>
7 changes: 7 additions & 0 deletions packages/test-e2e-composable-vue3/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,12 @@ export const router = createRouter({
layout: 'blank',
},
},
{
path: '/no-setup-scope-query',
component: () => import('./components/NoSetupScopeQuery.vue'),
meta: {
layout: 'blank',
},
},
],
})
5 changes: 5 additions & 0 deletions packages/test-e2e-composable-vue3/tests/e2e/specs/test.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@ describe('Vue 3 + Apollo Composable', () => {
cy.get('[data-test-id="global-loading"]').should('not.contain', 'Global loading...')
cy.contains('#app', 'Currently viewing # General')
})

it('supports queries outside of setup but within scope', () => {
cy.visit('/no-setup-scope-query')
cy.contains('.no-setup-scope-query', 'Hello world!')
})
})
4 changes: 2 additions & 2 deletions packages/vue-apollo-composable/src/useApolloClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentInstance, getCurrentScope, inject } from 'vue-demi'
import { hasInjectionContext, inject } from 'vue-demi'
import { ApolloClient } from '@apollo/client/core/index.js'

export const DefaultApolloClient = Symbol('default-apollo-client')
Expand Down Expand Up @@ -35,7 +35,7 @@ export function useApolloClient<TCacheShape = any> (clientId?: ClientId): UseApo
// Save current client in current closure scope
const savedCurrentClients = currentApolloClients

if (!getCurrentInstance() && !getCurrentScope()) {
if (!hasInjectionContext()) {
resolveImpl = (id?: ClientId) => {
if (id) {
return resolveClientWithId(savedCurrentClients, id)
Expand Down
Loading