Skip to content

Commit 0d6511b

Browse files
committed
feat: add support for more ApolloClient configuration options
1 parent 646c6fb commit 0d6511b

File tree

4 files changed

+66
-45
lines changed

4 files changed

+66
-45
lines changed

packages/nuxt/src/runtime/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import { createApolloClient } from './utils/createApolloClient'
88

99
export default defineNuxtPlugin(async (nuxtApp) => {
1010
const runTimeconfig = nuxtApp.$config
11-
const apolloConfig = runTimeconfig.public.apollo
11+
const globalConfig = runTimeconfig.public.apollo
1212

13-
if (!apolloConfig?.clients) {
13+
if (!globalConfig?.clients) {
1414
throw new Error('[vue3-apollo] No Apollo clients configured')
1515
}
1616

1717
const apolloClients: Record<string, ApolloClient> = {}
1818

19-
for (const [clientId, config] of Object.entries(apolloConfig.clients)) {
19+
for (const [clientId, config] of Object.entries(globalConfig.clients)) {
2020
apolloClients[clientId] = await createApolloClient({
2121
clientId,
22-
config: defu(config, omit(apolloConfig, ['clients'])),
22+
config: defu(config, omit(globalConfig, ['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)