This course is no longer maintained and may be out-of-date. While it remains available for reference, its content may not reflect the latest updates, best practices, or supported features.
ミューテーションとキャッシュ更新
まずは統合を始めます。delete mutationを定義するために src/components/Todo/TodoItem.js
を開いて、以下のコードを追加します。
const TodoItem = ({index, todo, client}) => {+ const REMOVE_TODO = gql`+ mutation removeTodo ($id: Int!) {+ delete_todos(where: {id: {_eq: $id}}) {+ affected_rows+ }+ }+ `;+ const [removeTodoMutation] = useMutation(REMOVE_TODO);
todoを削除するボタンクリックを処理するための関数が予め定義されています。その関数を更新して removeTodoMutation
mutate関数を使ってみましょう。
const removeTodo = (e) => {e.preventDefault();e.stopPropagation();+ removeTodoMutation({+ variables: {id: todo.id},+ optimisticResponse: true,+ update: (cache) => {+ const existingTodos = cache.readQuery({ query: GET_MY_TODOS });+ const newTodos = existingTodos.todos.filter(t => (t.id !== todo.id));+ cache.writeQuery({+ query: GET_MY_TODOS,+ data: {todos: newTodos}+ });+ }+ });};
Did you find this page helpful?
Start with GraphQL on Hasura for Free
Build apps and APIs 10x faster
Built-in authorization and caching
8x more performant than hand-rolled APIs

