Read variables used in useMutation

Viewed 428

I have an Apollo GraphQL mutation. This mutation accepts two variables. I would like to read these variables once the mutation is fired (not as response from the backed). I need to know these variables because I want to use them inside the onCompleted function.

My code:

  const [tripleEntity] = useMutation(TRIPLE_ENTITY, {
    context: { clientName: 'redzor' },
    refetchQueries: ['listProjects']
  })

What I would like:

  const [tripleEntity] = useMutation(TRIPLE_ENTITY, {
    context: { clientName: 'redzor' },
    refetchQueries: ['listsEntities'],
    onCompleted(response, variables): {
       console.log(variables) // Where "variables" are the input necessary to fire this mutation
       myFunction(variables) // This is the function that needs the input variables
    }   
  })
1 Answers

onComplete is only passed the mutation result as a parameter -- nothing else. However, mutate returns a Promise, so using onCompleted is not necessary.

const [mutate] = useMutation(YOUR_MUTATION, {...})
const doSomething = async (options) => {
  const result = await mutate(options)
  yourFunction(options.variables)
}
Related