Skip to content

Commit 330564c

Browse files
committed
fix(useLazyQuery): load() on server, fix #1495
1 parent 460a0db commit 330564c

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

packages/test-e2e-ssr/src/components/LazyQueryImmediately.vue

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import gql from 'graphql-tag'
33
import { useLazyQuery } from '@vue/apollo-composable'
4-
import { defineComponent, computed, reactive } from 'vue'
4+
import { defineComponent, computed } from 'vue'
55
66
export default defineComponent({
77
setup () {
@@ -12,6 +12,7 @@ export default defineComponent({
1212
label
1313
messages {
1414
id
15+
text
1516
}
1617
}
1718
}
@@ -44,6 +45,10 @@ export default defineComponent({
4445
<div v-if="channel">
4546
<div>Loaded channel: {{ channel.label }}</div>
4647
<div>Messages: {{ channel.messages.length }}</div>
48+
49+
<div v-for="message in channel.messages" :key="message.id" class="message">
50+
{{ message.text }}
51+
</div>
4752
</div>
4853
</div>
4954
</template>

packages/test-e2e-ssr/tests/e2e/specs/test.cy.ts

+5
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ describe('Vue 3 + Apollo Composable', () => {
3535
cy.get('.message').should('have.lengthOf', 1)
3636
cy.contains('.message', 'Hello world!')
3737
})
38+
39+
it('loads lazy query with load call', () => {
40+
cy.visit('/lazy-query-immediately')
41+
cy.contains('.message', 'Meow!')
42+
})
3843
})

packages/vue-apollo-composable/src/useLazyQuery.ts

+13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ import { DocumentNode } from 'graphql'
22
import { isRef } from 'vue-demi'
33
import { useQueryImpl, DocumentParameter, VariablesParameter, OptionsParameter, UseQueryOptions, UseQueryReturn } from './useQuery'
44
import type { OperationVariables } from '@apollo/client/core'
5+
import { isServer } from './util/env.js'
56

67
export interface UseLazyQueryReturn<TResult, TVariables extends OperationVariables> extends UseQueryReturn<TResult, TVariables> {
8+
/**
9+
* Activate the query and starts loading.
10+
* @param document Override document
11+
* @param variables Override variables
12+
* @param options Override options
13+
* @returns Returns false if the query is already active, otherwise the next result of the query.
14+
*/
715
load: (document?: DocumentNode | null, variables?: TVariables | null, options?: UseQueryOptions | null) => false | Promise<TResult>
816
}
917

@@ -35,6 +43,11 @@ export function useLazyQuery<
3543
if (isFirstRun) {
3644
query.forceDisabled.value = false
3745

46+
// If SSR, we need to start the query manually since `watch` on `isEnabled` in `useQueryImpl` won't be called.
47+
if (isServer) {
48+
query.start()
49+
}
50+
3851
return new Promise<TResult>((resolve, reject) => {
3952
const { off: offResult } = query.onResult((result) => {
4053
if (!result.loading) {

packages/vue-apollo-composable/src/useQuery.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ export function useQueryImpl<
251251
return
252252
}
253253

254+
// On server the watchers on document, variables and options are not triggered
255+
if (isServer) {
256+
applyDocument(documentRef.value)
257+
applyVariables(variablesRef.value)
258+
applyOptions(unref(optionsRef))
259+
}
260+
254261
started = true
255262
error.value = null
256263
loading.value = true
@@ -465,7 +472,12 @@ export function useQueryImpl<
465472
const isEnabled = computed(() => enabledOption.value && !forceDisabled.value && !!documentRef.value)
466473

467474
// Applying options first (in case it disables the query)
468-
watch(() => unref(optionsRef), value => {
475+
watch(() => unref(optionsRef), applyOptions, {
476+
deep: true,
477+
immediate: true,
478+
})
479+
480+
function applyOptions (value: UseQueryOptions<TResult, TVariables>) {
469481
if (currentOptions.value && (
470482
currentOptions.value.throttle !== value.throttle ||
471483
currentOptions.value.debounce !== value.debounce
@@ -474,16 +486,15 @@ export function useQueryImpl<
474486
}
475487
currentOptions.value = value
476488
restart()
477-
}, {
478-
deep: true,
479-
immediate: true,
480-
})
489+
}
481490

482491
// Applying document
483-
watch(documentRef, value => {
492+
watch(documentRef, applyDocument)
493+
494+
function applyDocument (value: DocumentNode | null | undefined) {
484495
currentDocument = value
485496
restart()
486-
})
497+
}
487498

488499
// Applying variables
489500
let currentVariables: TVariables | undefined
@@ -494,17 +505,19 @@ export function useQueryImpl<
494505
} else {
495506
return undefined
496507
}
497-
}, (value) => {
508+
}, applyVariables, {
509+
deep: true,
510+
immediate: true,
511+
})
512+
513+
function applyVariables (value?: TVariables) {
498514
const serialized = JSON.stringify([value, isEnabled.value])
499515
if (serialized !== currentVariablesSerialized) {
500516
currentVariables = value
501517
restart()
502518
}
503519
currentVariablesSerialized = serialized
504-
}, {
505-
deep: true,
506-
immediate: true,
507-
})
520+
}
508521

509522
// Refetch
510523

0 commit comments

Comments
 (0)