/
useMutation.ts
133 lines (120 loc) Β· 4.75 KB
/
useMutation.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { DocumentNode } from 'graphql'
import { MutationOptions, OperationVariables, FetchResult, TypedDocumentNode, ApolloError, ApolloClient } from '@apollo/client/core/index.js'
import { ref, onBeforeUnmount, isRef, Ref, getCurrentInstance } from 'vue-demi'
import { useApolloClient } from './useApolloClient'
import { ReactiveFunction } from './util/ReactiveFunction'
import { useEventHook } from './util/useEventHook'
import { trackMutation } from './util/loadingTracking'
import { toApolloError } from './util/toApolloError'
/**
* `useMutation` options for mutations that don't require `variables`.
*/
export interface UseMutationOptions<
TResult = any,
TVariables = OperationVariables
> extends Omit<MutationOptions<TResult, TVariables>, 'mutation'> {
clientId?: string
throws?: 'auto' | 'always' | 'never'
}
type DocumentParameter<TResult, TVariables> = DocumentNode | Ref<DocumentNode> | ReactiveFunction<DocumentNode> | TypedDocumentNode<TResult, TVariables> | Ref<TypedDocumentNode<TResult, TVariables>> | ReactiveFunction<TypedDocumentNode<TResult, TVariables>>
type OptionsParameter<TResult, TVariables> = UseMutationOptions<TResult, TVariables> | Ref<UseMutationOptions<TResult, TVariables>> | ReactiveFunction<UseMutationOptions<TResult, TVariables>>
export type MutateOverrideOptions<TResult> = Pick<UseMutationOptions<TResult, OperationVariables>, 'update' | 'optimisticResponse' | 'context' | 'updateQueries' | 'refetchQueries' | 'awaitRefetchQueries' | 'errorPolicy' | 'fetchPolicy' | 'clientId'>
export type MutateResult<TResult> = Promise<FetchResult<TResult, Record<string, any>, Record<string, any>> | null>
export type MutateFunction<TResult, TVariables> = (variables?: TVariables | null, overrideOptions?: MutateOverrideOptions<TResult>) => MutateResult<TResult>
export interface OnDoneContext {
client: ApolloClient<any>
}
export interface OnErrorContext {
client: ApolloClient<any>
}
export interface UseMutationReturn<TResult, TVariables> {
mutate: MutateFunction<TResult, TVariables>
loading: Ref<boolean>
error: Ref<ApolloError | null>
called: Ref<boolean>
onDone: (fn: (param: FetchResult<TResult, Record<string, any>, Record<string, any>>, context: OnDoneContext) => void) => {
off: () => void
}
onError: (fn: (param: ApolloError, context: OnErrorContext) => void) => {
off: () => void
}
}
export function useMutation<
TResult = any,
TVariables extends OperationVariables = OperationVariables
> (
document: DocumentParameter<TResult, TVariables>,
options: OptionsParameter<TResult, TVariables> = {},
): UseMutationReturn<TResult, TVariables> {
const vm = getCurrentInstance()
const loading = ref<boolean>(false)
vm && trackMutation(loading)
const error = ref<ApolloError | null>(null)
const called = ref<boolean>(false)
const doneEvent = useEventHook<[FetchResult<TResult, Record<string, any>, Record<string, any>>, OnDoneContext]>()
const errorEvent = useEventHook<[ApolloError, OnErrorContext]>()
// Apollo Client
const { resolveClient } = useApolloClient()
async function mutate (variables?: TVariables | null, overrideOptions: Omit<UseMutationOptions<TResult, TVariables>, 'variables'> = {}) {
let currentDocument: DocumentNode
if (typeof document === 'function') {
currentDocument = document()
} else if (isRef(document)) {
currentDocument = document.value
} else {
currentDocument = document
}
let currentOptions: UseMutationOptions<TResult, TVariables>
if (typeof options === 'function') {
currentOptions = options()
} else if (isRef(options)) {
currentOptions = options.value
} else {
currentOptions = options
}
const client = resolveClient(currentOptions.clientId)
error.value = null
loading.value = true
called.value = true
try {
const result = await client.mutate<TResult, TVariables>({
mutation: currentDocument,
...currentOptions,
...overrideOptions,
variables: (variables ?? currentOptions.variables)
? {
...(currentOptions.variables as TVariables),
...(variables as TVariables),
}
: undefined,
})
loading.value = false
doneEvent.trigger(result, {
client,
})
return result
} catch (e) {
const apolloError = toApolloError(e)
error.value = apolloError
loading.value = false
errorEvent.trigger(apolloError, {
client,
})
if (currentOptions.throws === 'always' || (currentOptions.throws !== 'never' && !errorEvent.getCount())) {
throw apolloError
}
}
return null
}
vm && onBeforeUnmount(() => {
loading.value = false
})
return {
mutate,
loading,
error,
called,
onDone: doneEvent.on,
onError: errorEvent.on,
}
}