Skip to content

Commit 5fd1378

Browse files
authored
Merge pull request #18 from guendev/fix-config
feat: add support for more ApolloClient configuration options
2 parents 1a9a3b2 + 0fb2a64 commit 5fd1378

File tree

6 files changed

+79
-42
lines changed

6 files changed

+79
-42
lines changed

.changeset/chatty-states-shave.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@vue3-apollo/core": patch
3+
"@vue3-apollo/nuxt": patch
4+
---
5+
6+
feat: add support for more ApolloClient configuration options

packages/nuxt/playground/nuxt.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@ export default defineNuxtConfig({
22
apollo: {
33
clients: {
44
default: {
5+
defaultOptions: {
6+
query: {
7+
errorPolicy: 'none'
8+
}
9+
},
510
httpEndpoint: 'https://graphqlplaceholder.vercel.app/graphql'
611
}
12+
},
13+
defaultOptions: {
14+
query: {
15+
errorPolicy: 'all'
16+
}
717
}
818
},
919
compatibilityDate: '2025-10-10',

packages/nuxt/src/runtime/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
1919
for (const [clientId, config] of Object.entries(apolloConfig.clients)) {
2020
apolloClients[clientId] = await createApolloClient({
2121
clientId,
22-
config: defu(config, omit(apolloConfig, ['clients'])),
22+
config: defu(config, omit(apolloConfig, ['clients', 'autoImports'])),
2323
nuxtApp
2424
})
2525
}

packages/nuxt/src/runtime/utils/createApolloClient.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { useCookie } from 'nuxt/app'
1414
import type { ApolloClientConfig } from '../../type'
1515
import type { ApolloErrorHookPayload } from '../types'
1616

17+
import { pick } from '../utils/pick'
18+
1719
const APOLLO_STATE_KEY_PREFIX = 'apollo:'
1820

1921
interface CreateApolloClientParams {
@@ -150,21 +152,28 @@ export async function createApolloClient({ clientId, config, nuxtApp }: CreateAp
150152

151153
const retryLink = new RetryLink()
152154

153-
const defaultOptions: ApolloClient.DefaultOptions = defu({
154-
query: {
155-
fetchPolicy: import.meta.server ? 'network-only' : 'cache-first'
155+
const mergedConfig: Omit<ApolloClient.Options, 'cache' | 'devtools' | 'link' | 'ssrForceFetchDelay' | 'ssrMode'> = defu(
156+
{
157+
defaultOptions: import.meta.server
158+
? {
159+
query: {
160+
fetchPolicy: 'network-only'
161+
},
162+
watchQuery: {
163+
fetchPolicy: 'network-only'
164+
}
165+
}
166+
: {}
156167
},
157-
watchQuery: {
158-
fetchPolicy: import.meta.server ? 'network-only' : 'cache-first'
159-
}
160-
}, config.defaultOptions)
168+
pick(config, ['assumeImmutableResults', 'dataMasking', 'defaultOptions', 'localState', 'queryDeduplication'])
169+
)
161170

162171
// Create an Apollo Client instance
163172
const client = new ApolloClient({
173+
...mergedConfig,
164174
cache,
165-
defaultOptions,
166175
devtools: {
167-
enabled: config.devtools ?? config.devtools,
176+
enabled: config.devtools ?? import.meta.dev,
168177
name: clientId
169178
},
170179
// Combine the auth combinedLink, error combinedLink, and main combinedLink chain
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Creates a new object by picking specified keys from the source object.
3+
* Returns a type-safe object with only the picked keys.
4+
*
5+
* @template T - The type of the source object
6+
* @template K - The keys to pick from the object
7+
*
8+
* @param obj - The source object
9+
* @param keys - Array of keys to pick from the object
10+
*
11+
* @returns A new object with only the specified keys
12+
*
13+
* @example
14+
* ```ts
15+
* const user = { id: 1, name: 'John', email: 'john@example.com', password: 'secret' }
16+
* const publicUser = pick(user, ['id', 'name'])
17+
* // { id: 1, name: 'John' }
18+
* ```
19+
*
20+
* @example
21+
* ```ts
22+
* const options = { debounce: 300, throttle: 500, prefetch: true, cache: false }
23+
* const selected = pick(options, ['prefetch', 'cache'])
24+
* // { prefetch: true, cache: false }
25+
* ```
26+
*/
27+
export function pick<T extends Record<string, any>, K extends keyof T>(
28+
obj: T,
29+
keys: K[]
30+
): Pick<T, K> {
31+
const result = {} as Pick<T, K>
32+
33+
for (const key of keys) {
34+
if (key in obj) {
35+
result[key] = obj[key]
36+
}
37+
}
38+
39+
return result
40+
}

packages/nuxt/src/type.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ export interface ApolloModuleOptions extends ApolloSharedConfig {
5555
* @default true
5656
*/
5757
autoImports?: boolean
58-
59-
/**
60-
* Enable devtools integration
61-
* @default true
62-
*/
63-
devtools?: boolean
6458
}
6559

6660
/**
@@ -103,7 +97,7 @@ export interface ApolloSharedAuthConfig {
10397
/**
10498
* Shared configuration options for Apollo Client and Module
10599
*/
106-
export interface ApolloSharedConfig {
100+
export interface ApolloSharedConfig extends Pick<ApolloClient.Options, 'assumeImmutableResults' | 'dataMasking' | 'defaultOptions' | 'localState' | 'queryDeduplication'> {
107101
/**
108102
* Tùy chọn bổ sung cho HTTP link.
109103
* Cho phép tùy chỉnh headers, credentials, fetch options, v.v.
@@ -118,9 +112,9 @@ export interface ApolloSharedConfig {
118112
httpLinkOptions?: Omit<HttpLink.Options, 'uri'>
119113

120114
/**
121-
* Tùy chọn bổ sung cho WebSocket link.
122-
* Cho phép tùy chỉnh connection params, retry logic, keepAlive, v.v.
123-
* Chỉ có hiệu lực khi `wsEndpoint` được cấu hình.
115+
* Additional options for WebSocket link.
116+
* Allows customizing connection params, retry logic, keepAlive, etc.
117+
* Only effective when `wsEndpoint` is configured.
124118
*
125119
* @example
126120
* ```ts
@@ -138,28 +132,6 @@ export interface ApolloSharedConfig {
138132
*/
139133
devtools?: boolean
140134

141-
/**
142-
* Default options for Apollo Client operations.
143-
* Allows configuration of fetch policies, error handling, polling intervals, etc.
144-
*
145-
* @example
146-
* ```ts
147-
* {
148-
* watchQuery: {
149-
* fetchPolicy: 'cache-and-network'
150-
* },
151-
* query: {
152-
* fetchPolicy: 'network-only',
153-
* errorPolicy: 'all'
154-
* },
155-
* mutate: {
156-
* errorPolicy: 'all'
157-
* }
158-
* }
159-
* ```
160-
*/
161-
defaultOptions?: ApolloClient.DefaultOptions
162-
163135
/**
164136
* Configuration options for Apollo Client's InMemoryCache.
165137
* Allows customization of type policies, cache normalization, pagination, etc.

0 commit comments

Comments
 (0)