GraphQL

Tutorial 11 - Using Mutations to Delete an Item

13min
introduction in this article we'll look at how to delete a record with a graphql mutation, but first of all, let's look at what deleting a record means (and how to undo it!) what is soft deletion? when we delete records with graphql, they are soft deleted by default if you accidentally delete data, it may have been "soft deleted", and you may be able to recover it if you act quickly think of soft deletion as being a bit like a recycle bin records are given a deleted at property with a date in 30 days time in platformos's datetime format (iso 8601) when the date is reached, the record will be permanently deleted they are removed from the results of all queries by default (this includes all siteglide tags) you can get ordinary records queries to display soft deleted records (only), see below query seedeletedrecords { records( per page 20, filter { deleted at { exists true } }, sort { deleted at { order asc } } ) { results { id deleted at } } } the results will be the same as in a normal query; it's helpful to add deleted at to the results so you can see when they will be permanently deleted { "data" { "record delete" { "id" "3", "deleted at" "2024 01 02t15 15 55 666z" } } } recovering soft deleted records if a record has been soft deleted by mistake, and has not yet been permanently deleted, you can recover it by setting the deleted at property to null make sure to use the query above to find the id of the record you want to recover and pass it in as a variable to the mutation below mutation recoversoftdeletedrecord($id id!) { record update(id $id, record {deleted at "null"}) { id deleted at } } after changing deleted at to null, the record becomes an ordinary record it appears in queries and is no longer scheduled to be deleted the mutation results should confirm that deleted at is no longer set { "data" { "record update" { "id" "3", "deleted at" null } } } if you want to extend the amount of time the record stays in soft deleted state, you can change the deleted at property to set the deletion date to be further in the future (but this is an advanced method we won't cover here) deleting a record step 1) find the id of the record you wish to delete use another query to find the id step 2) add the mutation keyword and a name mutation deleterecord { } step 3) add the records delete mutation type mutation deleterecord { record delete() {} } step 4) add the id of the record you wish to delete, with a variable if you like mutation deleterecord($id id!) { record delete( id $id ) {} } step 5) add a result to confirm the mutation's success mutation deleterecord($id id!) { record delete( id $id ) { id deleted at } } a successful deletion may look like this { "data" { "record delete" { "id" "3", "deleted at" "2024 01 02t15 15 55 666z" } } } summary and further learning you've now learned how to perform all four crud operations using graphql create read (query) update delete if you wish to programatically create, update or delete multiple records at once, it may be more efficient to use the following mutation types instead of looping records create all records update all records delete all be very careful with those and make sure you test with data which is backed up we recommend using any table false as a precaution in these mutation types, you use a filter to determine which records should be affected instead of only passing id you need to add count to the results next time next time we'll look at how you can use a query to access data from multiple relational database tables, or in other words, view data from siteglide datasources