Join our newsletter!
Get exclusive content, resources, and more!
1-2 emails per week, no spam
Outline
-
Boost Your React Apps with Apollo: First Steps
- Series Introduction
- What are GraphQL and Apollo?
- A Note on the Beta
- Course Repository Overview
- Meet GraphQL Playground
- Create a Client and Install Apollo
- Prettier and Apollo GraphQL for VS Code
- Set Up Apollo with React
- Your First Query
- Test the Query in the Browser
- Exercise: Modify Query
- Solution & Next Steps
-
Boost Your React Apps with Apollo: Fetching & Updating Data
- Tutorial & Code Overview
- Adding Total Points to the Habits Query
- Create Data with Mutations
- Use the Mutation in the UI
- Exercise: Use Error and Loading in the UI
- Solution: Use Error and Loading in the UI
- Refetching Queries after Mutations
- Exercise: Delete Habits with Refetching
- Solution: Delete Habits with Refetching
- Fixing a Concurrency Bug by Awaiting Refetch Queries
- What's Next
-
Boost Your React Apps with Apollo: Beyond the Basics
- Tutorial & Sample Code Overview
- Updating the UI When a Mutation Completes
- Exercise: onCompleted
- Solution: onCompleted
- Introduction to Caching in Apollo
- Updating Apollo's Cache after Adding a Habit
- Exercise: Updating the Cache after Deleting a Habit
- Solution: Updating the Cache after Deleting a Habit
- Retching vs Caching
- Polling the Server
- Calling Queries Manually with useLazyQuery
- Fixing Adding and Deleting Entries
- Fragments
- Reusing Fragments
- Exercise: Use Fragment with Entries Query
- Solution: Use Fragment with Entries Query
- Where to Go Next
Outline
-
Boost Your React Apps with Apollo: First Steps
- Series Introduction
- What are GraphQL and Apollo?
- A Note on the Beta
- Course Repository Overview
- Meet GraphQL Playground
- Create a Client and Install Apollo
- Prettier and Apollo GraphQL for VS Code
- Set Up Apollo with React
- Your First Query
- Test the Query in the Browser
- Exercise: Modify Query
- Solution & Next Steps
-
Boost Your React Apps with Apollo: Fetching & Updating Data
- Tutorial & Code Overview
- Adding Total Points to the Habits Query
- Create Data with Mutations
- Use the Mutation in the UI
- Exercise: Use Error and Loading in the UI
- Solution: Use Error and Loading in the UI
- Refetching Queries after Mutations
- Exercise: Delete Habits with Refetching
- Solution: Delete Habits with Refetching
- Fixing a Concurrency Bug by Awaiting Refetch Queries
- What's Next
-
Boost Your React Apps with Apollo: Beyond the Basics
- Tutorial & Sample Code Overview
- Updating the UI When a Mutation Completes
- Exercise: onCompleted
- Solution: onCompleted
- Introduction to Caching in Apollo
- Updating Apollo's Cache after Adding a Habit
- Exercise: Updating the Cache after Deleting a Habit
- Solution: Updating the Cache after Deleting a Habit
- Retching vs Caching
- Polling the Server
- Calling Queries Manually with useLazyQuery
- Fixing Adding and Deleting Entries
- Fragments
- Reusing Fragments
- Exercise: Use Fragment with Entries Query
- Solution: Use Fragment with Entries Query
- Where to Go Next
Now that we've figured out our mutation, let's add this feature to the UI.
First, we'll import the AddHabit
component I've provided for you into App.js
:
import AddHabit from "./AddHabit";
Then, we can use that component below our total points:
<AddHabit />
The AddHabit
component is pretty straightforward. It uses a ref
to get the text input and then an event handler calls setDescription
from useState
. Our challenge is to use that description
piece of state in the input
for the createHabit
mutation.
We'll do all of this in AddHabit.js
. First, let's import useMutation
and gql
from Apollo:
import { useMutation, gql } from "@apollo/client";
Next, we'll add the mutation above the component:
const CREATE_HABIT_MUTATION = gql`
mutation CREATE_HABIT_MUTATION($input: NewHabitInput) {
createHabit(input: $input) {
id
description
points
entries {
id
notes
date
completed
}
}
}
`;
Then, we'll use the useMutation
hook inside of the component:
const [createHabit, { error, loading }] = useMutation(CREATE_HABIT_MUTATION);
This means we can now call createHabit
in the addHabit
function that gets called when the user presses enter or clicks the "Add Habit" button:
const addHabit = (e) => {
e.preventDefault();
const input = { description };
createHabit({ variables: { input } });
descriptionInput.current.value = "";
setDescription("");
};
Note that, to pass variables into the mutation, we'll need to provide an object with a variables
key that is also an object. This "object soup" can get a little confusing, so hang in there. It helps to look at the useMutation
API docs to see what's going on there.
Up next, I'll hand this off to you with an exercise to improve the UI.