Skip to content

Commit 1e98ef2

Browse files
committed
docs: add guide for advanced Nuxt Apollo integration
- Introduced a new "Custom Apollo Integration for Nuxt" guide with steps for runtime customization. - Updated sidebar navigation to include the new guide under advanced topics.
1 parent 6eb41fc commit 1e98ef2

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

packages/docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export default defineConfig({
8484
},
8585
{
8686
items: [
87+
{
88+
link: '/advance/nuxt-custom-integration',
89+
text: 'Nuxt Integration'
90+
},
8791
{
8892
link: '/advance/tracking',
8993
text: 'Tracking'
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Custom Apollo Integration for Nuxt
2+
3+
4+
A compact guide for setting up and extending Apollo Client in Nuxt, focusing on module limitations, plugin customization, and best practices.
5+
6+
## Overview
7+
8+
Nuxt modules simplify setup but run only at **build time**, meaning they can’t access runtime APIs like `useRuntimeConfig` or `useApolloClient`.
9+
For dynamic logic (auth, caching, token refresh), use **plugins**, which run after app startup with full Nuxt context.
10+
11+
## Why Use Plugins
12+
13+
### 1. Serialization
14+
During build, Nuxt serializes module options into JSON.
15+
Complex objects like `ApolloClient` or custom `Link`s **lose references** and can’t be reused at runtime.
16+
17+
### 2. Context Isolation
18+
Modules execute in a **separate build context**, isolated from Nuxt’s runtime environment.
19+
Plugins, on the other hand, run inside the live app, where you can modify the Apollo client safely.
20+
21+
**Modules:** define static config & generate runtime code
22+
**Plugins:** handle runtime logic & modify live instances
23+
24+
## Basic Setup
25+
26+
Configure the Apollo client in `nuxt.config.ts`:
27+
28+
```ts
29+
export default defineNuxtConfig({
30+
graphql: {
31+
clients: {
32+
default: {
33+
httpEndpoint: ''
34+
}
35+
}
36+
}
37+
})
38+
```
39+
40+
## Plugin Customization
41+
42+
Create a plugin at `app/plugins/apollo.ts` to adjust the client at runtime:
43+
44+
```ts
45+
export default defineNuxtPlugin(() => {
46+
const { client } = useApolloClient()
47+
const access = useCookie('accessToken')
48+
49+
const httpLink = new HttpLink({
50+
uri: runtimeConfig.public.apiBase
51+
})
52+
53+
const authLink = new SetContextLink((prevContext) => {
54+
return {
55+
headers: {
56+
Authorization: `Bearer ${accessToken.value}`
57+
}
58+
}
59+
})
60+
61+
const retryLink = new RetryLink()
62+
63+
const errorLink = new ErrorLink(({ error, forward, operation }) => {
64+
console.log(error)
65+
})
66+
67+
68+
const cache = client.cache as InMemoryCache
69+
cache.policies.addPossibleTypes(possibleTypes.possibleTypes)
70+
71+
const typePolicies: StrictTypedTypePolicies = {}
72+
cache.policies.addTypePolicies(typePolicies)
73+
74+
client.setLink(ApolloLink.from([authLink, errorLink, retryLink, httpLink]))
75+
})
76+
```
77+
78+
### Plugin Execution Order
79+
80+
Nuxt runs plugins **in registration order**, based on file name sorting.
81+
When customizing Apollo, ensure your plugin runs **before** any other plugin that uses Apollo Client.
82+
You can control this by prefixing the file name (e.g., `00.apollo.ts`) or following [Nuxt’s plugin registration order](https://nuxt.com/docs/4.x/guide/directory-structure/app/plugins#registration-order).
83+
84+
85+
## Best Practices
86+
87+
- Use `runtimeConfig` for environment-dependent URLs and secrets.
88+
- Manage `possibleTypes` and `typePolicies` within plugins.
89+
- Avoid storing tokens globally on the server.
90+
- Test on both SSR and CSR modes for consistent behavior.

packages/docs/nuxt/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export default defineNuxtConfig({
4343
})
4444
```
4545

46+
> 💡 **Tip:** This setup works for most use cases.
47+
> For advanced customization (e.g., runtime Apollo links, auth flow, or cache policies), see [Nuxt Custom Integration](/advance/nuxt-custom-integration).
48+
4649
Use anywhere in your app:
4750

4851
```ts

0 commit comments

Comments
 (0)