Delete bulk todos - mutation

In this part of the tutorial, you will learn how to delete existing todos by using GraphQL Mutations.

Let's define a graphql mutation to perform bulk delete of todos.

mutation clearCompleted {
delete_todos(where: {is_completed: {_eq: true}, is_public: {_eq: false}}) {
affected_rows
}
}

You will also need to pass in the values for the variables.

Try this mutation in GraphiQL against the application database to see what the response looks like.

Let's now integrate this graphql mutation into our elm app.

We will construct a GraphQL Mutation to update a private todo and integrate it with the elm app.

Construct GraphQL Mutation

We will be constructing a GraphQL mutation as above

deleteSingleTodoItem : SelectionSet (Maybe MutationResponse) RootMutation -> String -> Cmd Msg
deleteSingleTodoItem mutation authToken =
makeGraphQLMutation
authToken
mutation
(RemoteData.fromResult >> TodoDeleted)
+deleteAllCompletedTodo : SelectionSet (Maybe MutationResponse) RootMutation
+deleteAllCompletedTodo =
+ Mutation.delete_todos (setTodoListDeleteAllCompletedWhere True) mutationResponseSelection
+
+
+setTodoListValueForTodoStatus : Bool -> Boolean_comparison_exp
+setTodoListValueForTodoStatus status =
+ buildBoolean_comparison_exp
+ (\args ->
+ { args
+ | eq_ = Present status
+ }
+ )
+
+
+setTodoListDeleteAllCompletedWhere : Bool -> DeleteTodosRequiredArguments
+setTodoListDeleteAllCompletedWhere status =
+ DeleteTodosRequiredArguments
+ (buildTodos_bool_exp
+ (\args ->
+ { args
+ | is_completed = Present (setTodoListValueForTodoStatus status)
+ }
+ )
+ )
+
+
+delAllResponseSelection : SelectionSet MutationResponse Hasura.Object.Todos_mutation_response
+delAllResponseSelection =
+ SelectionSet.map MutationResponse
+ TodosMutation.affected_rows
+
+
+deleteAllCompletedItems : SelectionSet (Maybe MutationResponse) RootMutation -> String -> Cmd Msg
+deleteAllCompletedItems mutation authToken =
+ makeGraphQLMutation
+ authToken
+ mutation
+ (RemoteData.fromResult >> AllCompletedItemsDeleted)

Add Data Types

Lets add new data types required to perform this operation

type alias DeleteTodo =
RemoteData (Graphql.Http.Error (Maybe MutationResponse)) (Maybe MutationResponse)
+type alias AllDeleted =
+ RemoteData (Graphql.Http.Error (Maybe MutationResponse)) (Maybe MutationResponse)

Add new Msg type

Lets add Msg types required to perform this operation

type Msg
= EnteredEmail String
| EnteredPassword String
| EnteredUsername String
| MakeLoginRequest
| MakeSignupRequest
| ToggleAuthForm DisplayForm
| GotLoginResponse LoginResponseParser
| GotSignupResponse SignupResponseParser
| ClearAuthToken
| FetchPrivateDataSuccess TodoData
| InsertPrivateTodo
| UpdateNewTodo String
| InsertPrivateTodoResponse (GraphQLResponse MaybeMutationResponse)
| MarkCompleted Int Bool
| UpdateTodo UpdateTodoItemResponse
| DelTodo Int
| TodoDeleted DeleteTodo
+ | AllCompletedItemsDeleted AllDeleted
+ | DeleteAllCompletedItems

Handle new Msg types in update

TodoDeleted _ ->
( model
, fetchPrivateTodos model.authData.authToken
)
+ DeleteAllCompletedItems ->
+ ( model, deleteAllCompletedItems deleteAllCompletedTodo model.authData.authToken )
+
+ AllCompletedItemsDeleted _ ->
+ ( model
+ , fetchPrivateTodos model.authData.authToken
+ )

Update render functions

clearButton : Html Msg
clearButton =
- button [ class "clearComp" ]
+ button [ class "clearComp", onClick DeleteAllCompletedItems ]
[ text "Clear completed"
]
Did you find this page helpful?
Start with GraphQL on Hasura for Free
  • ArrowBuild apps and APIs 10x faster
  • ArrowBuilt-in authorization and caching
  • Arrow8x more performant than hand-rolled APIs
Promo
footer illustration
Brand logo
© 2024 Hasura Inc. All rights reserved
Github
Titter
Discord
Facebook
Instagram
Youtube
Linkedin