Use useLazyQuery when query variables are not known upfront or when a query should only run in response to a specific user action (such as a button click, search submission, or opening a modal) rather than automatically on component mount.
Comparison of query types:
useLazyQuery: Use when variables come from a user action.useQuery({ enabled }): Use when variables are known but execution should be gated by a condition.useQuery: Use when variables are known and the query should run immediately on mount.
useLazyQuery starts in a disabled state. It exposes a load(variables?) function to trigger execution. After the initial load, it behaves like a standard useQuery where variables become reactive and results update automatically.
import { TypedDocumentNode } from '@apollo/client'
import { useLazyQuery } from '@vue/apollo-composable'
import { ref } from 'vue'
// ... gql definition ...
const term = ref('')
const { load, current } = useLazyQuery(SEARCH_USERS)
async function search() {
const result = await load({ term: term.value })
console.log('Found users:', result?.users)
}
</script>
<template>
<form @submit.prevent="search">
<input v-model="term">
<button>Search</button>
</form>
<div v-if="current.loading">
Searching...
</div>
<ul v-else-if="current.resultState === 'complete'">
<li v-for="user in current.result.users" :key="user.id">
{{ user.name }}
</li>
</ul>
</template>