Skip to content

Commit cc063ca

Browse files
committed
docs: update installation instructions and Nuxt guide for Apollo Client setup
1 parent 12c8cce commit cc063ca

5 files changed

Lines changed: 136 additions & 18 deletions

File tree

‎packages/docs/guide/installation.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ Install Apollo Client and Vue Apollo:
77
::: code-group
88

99
```shell [npm]
10-
npm install @apollo/client@next @vue/apollo-composable@next graphql
10+
npm install @apollo/client @vue/apollo-composable@next graphql
1111
```
1212

1313
```shell [yarn]
14-
yarn add @apollo/client@next @vue/apollo-composable@next graphql
14+
yarn add @apollo/client @vue/apollo-composable@next graphql
1515
```
1616

1717
```shell [pnpm]
18-
pnpm add @apollo/client@next @vue/apollo-composable@next graphql
18+
pnpm add @apollo/client @vue/apollo-composable@next graphql
1919
```
2020

2121
:::
2222

23-
::: warning Pre-release Versions
24-
Vue Apollo v5 requires Apollo Client 4.1+, both currently in pre-release. The `@next` tag installs the latest pre-release version.
23+
::: warning Pre-release Version
24+
Vue Apollo v5 is currently in pre-release. The `@next` tag installs the latest pre-release version.
2525
:::
2626

2727
## Step 2: Create an Apollo Client

‎packages/docs/ssr/nuxt.md‎

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,124 @@
11
# Nuxt
22

3-
::: warning Work in Progress
4-
This page is under construction.
3+
This guide covers setting up Vue Apollo with server-side rendering (SSR) in Nuxt.
4+
5+
::: warning @nuxtjs/apollo Compatibility
6+
The `@nuxtjs/apollo` module does not yet support Vue Apollo v5. This guide shows how to set up Apollo Client manually using a Nuxt plugin.
57
:::
8+
9+
## Step 1: Install Dependencies
10+
11+
Install Apollo Client and Vue Apollo:
12+
13+
::: code-group
14+
15+
```shell [npm]
16+
npm install @apollo/client @vue/apollo-composable@next graphql
17+
```
18+
19+
```shell [yarn]
20+
yarn add @apollo/client @vue/apollo-composable@next graphql
21+
```
22+
23+
```shell [pnpm]
24+
pnpm add @apollo/client @vue/apollo-composable@next graphql
25+
```
26+
27+
:::
28+
29+
## Step 2: Create Apollo Plugin
30+
31+
Create a Nuxt plugin to initialize Apollo Client with SSR support:
32+
33+
```ts
34+
import type { NormalizedCacheObject } from '@apollo/client'
35+
// app/plugins/apollo.ts
36+
import {
37+
ApolloClient,
38+
HttpLink,
39+
InMemoryCache
40+
41+
} from '@apollo/client'
42+
import { DefaultApolloClient } from '@vue/apollo-composable'
43+
import { defineNuxtPlugin } from 'nuxt/app'
44+
45+
declare module 'nuxt/app' {
46+
interface NuxtPayload {
47+
apollo?: NormalizedCacheObject
48+
}
49+
}
50+
51+
export default defineNuxtPlugin({
52+
name: 'apollo',
53+
enforce: 'pre',
54+
55+
setup(nuxtApp) {
56+
const cache = new InMemoryCache()
57+
58+
// Restore cache from server payload on client
59+
if (import.meta.client && nuxtApp.payload.apollo) {
60+
cache.restore(nuxtApp.payload.apollo)
61+
}
62+
63+
const httpLink = new HttpLink({
64+
uri: 'https://example.com/graphql',
65+
})
66+
67+
const client = new ApolloClient({
68+
link: httpLink,
69+
cache,
70+
ssrMode: import.meta.server,
71+
// Prevent refetching on client when we already have data
72+
ssrForceFetchDelay: import.meta.client ? 100 : undefined,
73+
})
74+
75+
// Provide to Vue app
76+
nuxtApp.vueApp.provide(DefaultApolloClient, client)
77+
78+
// Extract cache after render on server
79+
if (import.meta.server) {
80+
nuxtApp.hook('app:rendered', () => {
81+
nuxtApp.payload.apollo = client.extract() as NormalizedCacheObject
82+
})
83+
}
84+
},
85+
})
86+
```
87+
88+
This plugin:
89+
- Creates an Apollo Client instance with SSR mode enabled on the server
90+
- Restores the cache from the server payload on the client (hydration)
91+
- Extracts the cache after server render and sends it to the client via the Nuxt payload
92+
93+
## Step 3: Use in Components
94+
95+
You can now use Vue Apollo composables in your components:
96+
97+
```vue
98+
<script setup>
99+
import { gql } from '@apollo/client'
100+
import { useQuery } from '@vue/apollo-composable'
101+
102+
const { current } = useQuery(gql`
103+
query {
104+
company {
105+
ceo
106+
}
107+
}
108+
`)
109+
</script>
110+
111+
<template>
112+
<div>
113+
<div v-if="current.resultState === 'complete'">
114+
{{ current.result.company.ceo }}
115+
</div>
116+
</div>
117+
</template>
118+
```
119+
120+
## Next Steps
121+
122+
Learn more about fetching data with queries:
123+
124+
[Queries →](/data/queries)

‎packages/vue-apollo-composable/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"test": "vitest"
4545
},
4646
"peerDependencies": {
47-
"@apollo/client": "^4.1.0-alpha.8",
47+
"@apollo/client": "^4.1.0",
4848
"@vue/reactivity": "^3.5.0",
4949
"@vue/runtime-core": "^3.5.0",
5050
"@vueuse/core": "^14.0.0",

‎pnpm-lock.yaml‎

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ shellEmulator: true
77
trustPolicy: no-downgrade
88

99
trustPolicyExclude:
10-
- '@apollo/client@4.1.0-alpha.8'
1110
- undici-types@6.21.0
1211
- vite@5.4.21
1312
- chokidar@4.0.3
@@ -18,7 +17,7 @@ packages:
1817
catalog:
1918
'@akryum/sheep': ^0.5.1
2019
'@antfu/eslint-config': ^6.5.1
21-
'@apollo/client': ^4.1.0-alpha.8
20+
'@apollo/client': ^4.1.0
2221
'@graphql-codegen/cli': ^6.1.0
2322
'@graphql-yoga/plugin-defer-stream': ^3.17.1
2423
'@microsoft/api-extractor': ^7.55.2

0 commit comments

Comments
 (0)