Dynamically Update Local State using Apollo Link State with Mutation Variables

Ben Awad
InstructorBen Awad

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 4 years ago

In this lesson, you will learn how to use variables to update local state in React with your Apollo Link State Mutations.

Instructor: [00:01] To add a variable to our mutation, we need to start by updating our GQL. I'm going to add parentheses and then give the variable a name. I'm going to call it amount. Then we have to specify the data type, which is int. It's required.

[00:15] I can pass the amount into the mutation. I'm going to say amount is equal to my variable called amount. This amount, I need to pass in when I call the increment mutation. We're calling it right here.

[00:27] I'm going to say onclick. We're going to call increment. I need to pass in variables. The variable I need to pass in is this amount. I'm going to say amount is equal to two.

[00:40] I can use this amount in my mutation up here. It's going to be the second parameter. I called it amount. Now I can use that in the actual data that I write.

[00:52] When I save this, I can click the plus. We're incrementing by two. I can change this to anything I want to. Let's do four. Now we increment by four.