-
-
Notifications
You must be signed in to change notification settings - Fork 522
/
useApolloClient.ts
102 lines (87 loc) · 3.32 KB
/
useApolloClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { hasInjectionContext, inject } from 'vue-demi'
import { ApolloClient } from '@apollo/client/core/index.js'
export const DefaultApolloClient = Symbol('default-apollo-client')
export const ApolloClients = Symbol('apollo-clients')
type ClientId = string
type ClientDict<T> = Record<ClientId, ApolloClient<T>>
type ResolveClient<TCacheShape, TReturn = ApolloClient<TCacheShape>> = (clientId?: ClientId) => TReturn
type NullableApolloClient<TCacheShape> = ApolloClient<TCacheShape> | undefined
export interface UseApolloClientReturn<TCacheShape> {
resolveClient: ResolveClient<TCacheShape>
readonly client: ApolloClient<TCacheShape>
}
function resolveDefaultClient<T> (providedApolloClients: ClientDict<T> | null, providedApolloClient: ApolloClient<T> | null): NullableApolloClient<T> {
const resolvedClient = providedApolloClients
? providedApolloClients.default
: (providedApolloClient ?? undefined)
return resolvedClient
}
function resolveClientWithId<T> (providedApolloClients: ClientDict<T> | null, clientId: ClientId): NullableApolloClient<T> {
return providedApolloClients?.[clientId]
}
export function useApolloClient<TCacheShape = any> (clientId?: ClientId): UseApolloClientReturn<TCacheShape> {
let resolveImpl: ResolveClient<TCacheShape, NullableApolloClient<TCacheShape>>
// Save current client in current closure scope
const savedCurrentClients = currentApolloClients
if (!hasInjectionContext()) {
resolveImpl = (id?: ClientId) => {
if (id) {
return resolveClientWithId(savedCurrentClients, id)
}
return resolveDefaultClient(savedCurrentClients, savedCurrentClients.default)
}
} else {
const providedApolloClients: ClientDict<TCacheShape> | null = inject(ApolloClients, null)
const providedApolloClient: ApolloClient<TCacheShape> | null = inject(DefaultApolloClient, null)
resolveImpl = (id?: ClientId) => {
if (id) {
const client = resolveClientWithId(providedApolloClients, id)
if (client) {
return client
}
return resolveClientWithId(savedCurrentClients, id)
}
const client = resolveDefaultClient(providedApolloClients, providedApolloClient)
if (client) {
return client
}
return resolveDefaultClient(savedCurrentClients, savedCurrentClients.default)
}
}
function resolveClient (id: ClientId | undefined = clientId) {
const client = resolveImpl(id)
if (!client) {
throw new Error(
`Apollo client with id ${
id ?? 'default'
} not found. Use an app.runWithContext() or provideApolloClient() if you are outside of a component setup.`,
)
}
return client
}
return {
resolveClient,
get client () {
return resolveClient()
},
}
}
let currentApolloClients: ClientDict<any> = {}
export function provideApolloClient<TCacheShape = any> (client: ApolloClient<TCacheShape>) {
currentApolloClients = {
default: client,
}
return function <TFnResult = any> (fn: () => TFnResult) {
const result = fn()
currentApolloClients = {}
return result
}
}
export function provideApolloClients<TCacheShape = any> (clients: ClientDict<TCacheShape>) {
currentApolloClients = clients
return function <TFnResult = any> (fn: () => TFnResult) {
const result = fn()
currentApolloClients = {}
return result
}
}