4.3 Updating Tasks

Our users may want to edit their tasks or mark them as complete. We will have to create a form very similar to the one in Chapter 4.1. The only difference is that we need to query task data and prefill the form with it.

Step 1: Create Mutation

Let's start by creating an update mutation. This will be used when a user submits the form.

src/components/EditTaskForm/UpdateTaskMutation.graphql.ts
import gql from 'graphql-tag';

export const UpdateTaskMutation = gql`
	mutation UpdateTaskMutation(
		$id: UUID!
		$title: String
		$description: String
		$completed: Boolean
	) {
		updatetasksCollection(
			filter: { id: { eq: $id } }
			set: {
				title: $title,
				description: $description,
				completed: $completed
			}
		) {
			affectedCount
			records {
				id
				title
				description
				completed
			}
		}
	}
`;

Step 2: Generate Typings

We will now generate typescript interfaces for our GraphQL API. Execute the following command:

This will update the file src/graphql-types.ts.

Step 3: Create Form

Create the following component:

Explanation:

  • Line 22: We take task ID as a prop. This will be used as a query variable.

  • Line 28: A function that takes the query result as input (array of tasks) and returns a single task. This task is used as the initial value of the form.

  • Line 33: A function that takes form data and converts it into mutation variables. This function is called when a user submits the form.

  • Line 57: We tell the GraphQL client to fetch data for our list query when the mutation is successful. If we don't do this, even after we successfully update a task, going back to the list screen will show old data. This is because Apollo will show data from its local cache. By retching a query we update the local cache.

Step 4: Add Form to Screen

Let's add our form to the EditTaskScreen component:

When you run the app, you should results similar to the screenshot below:

Web
iOS

Last updated

Was this helpful?