Sending a mutation with a variable

Hi,

I have a React text input like this:

<input
    type="text"
    id="input"
    onBlur={event =>
        submitMessage(event.target.value)
    }
/>

This is inside a larger component which also queries data.

I would like to make a mutation with that submitMessage handler, but I’m not clear on how to pass the event.target.value into the mutation. So far I have:

    const [submitMessage, { loading, error }] = useMutation(
        MESSAGE_MUTATION,
        {
            variables: {
                SendMessageInput: {
                    body: body
                }
            }
        }
    )

How would I use the value of the input to populate a variable to mutate that data?

Thanks

Ben

1 Like

Hello! Instead of providing variables to the useMutation hook, you can provide those variables just in time to the submitMessage method, which it looks like you’re calling in a scope where you can access event.

For details, see this section of the docs.

Ah great, super helpful thanks Stephen.