delete
Using these type definitions:
type Post {
id: ID! @id
content: String!
creator: User! @relationship(type: "HAS_POST", direction: IN)
}
type User {
id: ID! @id
name: String
posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT)
}
These delete
mutations and response types should be generated:
type DeleteInfo {
nodesDeleted: Int!
relationshipsDeleted: Int!
}
type Mutation {
deletePosts(where: PostWhere, delete: PostDeleteInput): DeleteInfo!
deleteUsers(where: UserWhere, delete: UserDeleteInput): DeleteInfo!
}
The |
Single delete
A single post can be deleted by executing the following GraphQL statement:
mutation {
deletePosts(where: {
id: "6042E807-47AE-4857-B7FE-1AADF522DE8B"
}) {
nodesDeleted
relationshipsDeleted
}
}
This should delete the post using the autogenerated ID that was returned after that post’s creation.
Consequently, nodesDeleted
should be equal 1
(the post) and relationshipsDeleted
should also equal 1
as the HAS_POST
relationship between the Post
and its author was deleted.
Nested delete
In case you want to delete a User
and all of their posts, you can use a single nested delete
mutation:
mutation {
deleteUsers(where: { name: "Jane Doe" }, delete: { posts: {} }) {
nodesDeleted
relationshipsDeleted
}
}
By the time the traversal has reached it, that empty where
argument has the context of only refer to posts that were created by Jane Doe, as the traversals to those Post
nodes were from her User
node.
Essentially, the above query is equivalent to:
mutation {
deleteUsers(
where: {
name: "Jane Doe"
},
delete: {
posts: [
where: {
node: {
creator: {
name: "Jane Doe"
}
}
}
]
}
) {
nodesDeleted
relationshipsDeleted
}
}
Note that the output Cypher statement should also have a redundant WHERE
clause: