feat: useMutation

This commit is contained in:
Guillaume Chau 2019-11-29 10:19:44 +01:00
parent 764027ae25
commit c8a7ecaeb4
2 changed files with 63 additions and 0 deletions

View File

@ -1,4 +1,5 @@
export * from './useQuery'
export * from './useMutation'
export * from './useResult'
// export * from './useLoading'
export * from './useApolloClient'

View 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,
}
}