Delete data
A mutation can be turned into a delete operation by starting the mutation
name with delete
or remove
.
A mutation will also be a delete operation if it is annotated with @cql_delete
.
The mutation must take a single argument that is an input type for a mapped entity,
or individual primary key arguments like retrieving operations.
If successful, the mutation will return a Boolean value.
At runtime, all partition key fields must be present, and a prefix of the clustering columns can be present (if using a single object argument, other fields will be ignored). The delete operation targets a single row if it operates on a full primary key, or multiple rows otherwise.
Let’s add another book "Pride and Prejudice" with an insertBook()
, so that you can delete
the book using deleteBook()
to illustrate deleting data:
mutation insertAnotherBook {
insertBook(book: {
title: "Pride and Prejudice",
isbn: "", author: "Jane Austen" }
) {
title
isbn
author
}
}
mutation deletepap {
deleteBook(book: { title:"Pride and Prejudice"})
}
{
"data": {
"deleteBook": true
}
}
To check for the existence of a record before deleting, use either method discussed above.
This example shows the use of the directive @cql_delete( ifExists: true)
:
deleteLibCollection(libColl: LibCollectionInput!): Boolean @cql_delete(ifExists: true)
mutation insertCSRMPhoto {
insertLibColl(libColl: { type: "photo", lib_id: 12345, lib_name: "CSRM" }) {
type
lib_id
lib_name
}
}
mutation insertCSRMBook {
insertLibColl(libColl: { type: "book", lib_id: 12345, lib_name: "CSRM" }) {
type
lib_id
lib_name
}
}
mutation insertWSacPhoto {
insertLibColl(libColl: { type: "photo", lib_id: 12543, lib_name: "West Sacramento Historical Society" }) {
type
lib_id
lib_name
}
}
mutation insertDavisBook {
insertLibColl(libColl: { type: "book", lib_id: 66666, lib_name: "Davis Historical Museum" }) {
type
lib_id
lib_name
}
}
# Note that the lib_id is INCORRECT,
# SO THIS delete operation will fail
mutation deleteDHM {
deleteLibCollection(libColl: {
type:"book",
lib_id: 66665,
lib_name: "Davis Historical Museum"
})
}
"data": {
"deleteLibCollection": false
}
}