npm i @apollo/client @vue/apollo-composable @vue/apollo-components graphql
pnpm add @apollo/client @vue/apollo-composable @vue/apollo-components graphql
yarn add @apollo/client @vue/apollo-composable @vue/apollo-components graphqlTemplate components built on @vue/apollo-composable,
which is a peer dependency. Both packages talk to the same Apollo Client and the same cache,
so you can mix them freely, even within one component.
<script setup lang="ts">
import { ApolloQuery } from '@vue/apollo-components'
import { GetDogs } from './queries'
</script>
<template>
<ApolloQuery :query="GetDogs">
<template #loading>
Loading…
</template>
<template #error="{ error, refetch }">
{{ error.message }}
<button @click="refetch()">
Retry
</button>
</template>
<template #data="{ data }">
<ul>
<li v-for="dog in data.dogs" :key="dog.id">
{{ dog.breed }}
</li>
</ul>
</template>
</ApolloQuery>
</template>Import the components directly, as above. Global registration is available but loses the generic slot-prop types:
import { VueApolloComponents } from '@vue/apollo-components'
app.use(VueApolloComponents)| Component | Wraps | Purpose |
|---|---|---|
<ApolloQuery> |
useQuery |
Fetch data, with #loading / #error / #empty / #data slots |
<ApolloMutation> |
useMutation |
Expose mutate to the template |
<ApolloSubscription> |
useSubscription |
Stream results, renderless when given no slot |
<ApolloSubscribeToMore> |
(nothing) | Merge a subscription into the enclosing <ApolloQuery> |
<ApolloFragment> |
useFragment |
Read a fragment from the cache, for data masking |
Full prop, event and slot reference: https://v5.apollo.vuejs.org/api/components/
disabledreplaces v4'sskip, and inverts the composable'senabled. An absent prop means the operation runs.<ApolloQuery>has two modes. Providing#dataopts into slot-per-state rendering; using only the default slot hands you the raw state. The default slot renders in both.#emptyis also the terminal branch: a query that settles with no result, no error and no load in flight renders it. Adisabledquery renders nothing at all.- Every prop maps to the option of the same name and keeps its
useQuerydefault. Nothing is toggled on for you based on which slots you pass. <ApolloFragment>reads a single entity. Usev-forfor lists, so each row gets its own complete/incomplete branch.- Components cannot suspend. Use
await useQuery(...)in<script setup>for that.
