Skip to content

Commit

Permalink
feat(useMutation): onDone & onError
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Dec 1, 2019
1 parent 9a595b4 commit 74ffbd0
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/vue-apollo-composable/src/useMutation.ts
@@ -1,8 +1,10 @@
import { DocumentNode } from 'graphql'
import { MutationOptions, OperationVariables } from 'apollo-client'
import { ref } from '@vue/composition-api'
import { FetchResult } from 'apollo-link'
import { useApolloClient } from './useApolloClient'
import { ReactiveFunction } from './util/ReactiveFunction'
import { useEventHook } from './util/useEventHook'

export interface UseMutationOptions<
TResult = any,
Expand All @@ -23,6 +25,9 @@ export function useMutation<
const loading = ref<boolean>(false)
const error = ref<Error>(null)
const called = ref<boolean>(false)

const doneEvent = useEventHook<FetchResult<TResult, Record<string, any>, Record<string, any>>>()
const errorEvent = useEventHook<Error>()

// Apollo Client
const { resolveClient } = useApolloClient()
Expand Down Expand Up @@ -55,10 +60,12 @@ export function useMutation<
},
})
loading.value = false
doneEvent.trigger(result)
return result
} catch (e) {
error.value = e
loading.value = false
errorEvent.trigger(e)
throw e
}
}
Expand All @@ -68,5 +75,7 @@ export function useMutation<
loading,
error,
called,
onDone: doneEvent.on,
onError: errorEvent.on,
}
}

0 comments on commit 74ffbd0

Please sign in to comment.