mirror of
https://github.com/vuejs/apollo.git
synced 2026-02-01 14:37:18 +00:00
feat: useMutation
This commit is contained in:
parent
764027ae25
commit
c8a7ecaeb4
@ -1,4 +1,5 @@
|
||||
export * from './useQuery'
|
||||
export * from './useMutation'
|
||||
export * from './useResult'
|
||||
// export * from './useLoading'
|
||||
export * from './useApolloClient'
|
||||
|
||||
62
packages/vue-apollo-composable/src/useMutation.ts
Normal file
62
packages/vue-apollo-composable/src/useMutation.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { DocumentNode } from 'graphql'
|
||||
import { MutationOptions, OperationVariables } from 'apollo-client'
|
||||
import { ref } from '@vue/composition-api'
|
||||
import { useApolloClient } from './useApolloClient'
|
||||
import { ReactiveFunction } from './util/ReactiveFunction'
|
||||
|
||||
export interface UseMutationOptions<
|
||||
TResult = any,
|
||||
TVariables = OperationVariables
|
||||
> extends Omit<MutationOptions<TResult, TVariables>, 'mutation'> {
|
||||
clientId?: string
|
||||
}
|
||||
|
||||
export function useMutation<
|
||||
TResult = any,
|
||||
TVariables = OperationVariables
|
||||
> (
|
||||
document: DocumentNode,
|
||||
options: UseMutationOptions<TResult, TVariables> | ReactiveFunction<UseMutationOptions<TResult, TVariables>> = null,
|
||||
) {
|
||||
if (!options) options = {}
|
||||
|
||||
const loading = ref<boolean>(false)
|
||||
const error = ref<Error>(null)
|
||||
|
||||
// Apollo Client
|
||||
const { resolveClient } = useApolloClient()
|
||||
|
||||
async function mutate (variables: TVariables = null) {
|
||||
let currentOptions: UseMutationOptions<TResult, TVariables>
|
||||
if (typeof options === 'function') {
|
||||
currentOptions = options()
|
||||
} else {
|
||||
currentOptions = options
|
||||
}
|
||||
const client = resolveClient(currentOptions.clientId)
|
||||
error.value = null
|
||||
loading.value = true
|
||||
try {
|
||||
const result = await client.mutate({
|
||||
mutation: document,
|
||||
...currentOptions,
|
||||
variables: {
|
||||
...variables || {},
|
||||
...currentOptions.variables || {},
|
||||
},
|
||||
})
|
||||
loading.value = false
|
||||
return result
|
||||
} catch (e) {
|
||||
error.value = e
|
||||
loading.value = false
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
mutate,
|
||||
loading,
|
||||
error,
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user