Delete
Using the following type definitions for these examples:
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)
}
The following delete Mutations and response type will be generated for the above type definitions:
type DeleteInfo {
nodesDeleted: Int!
relationshipsDeleted: Int!
}
type Mutation {
deletePosts(where: PostWhere, delete: PostDeleteInput): DeleteInfo!
deleteUsers(where: UserWhere, delete: UserDeleteInput): DeleteInfo!
}
Note that the DeleteInfo
type is the common return type for all delete Mutations.
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 will delete the post using the autogenerated ID that would have been returned after that post’s creation.
nodesDeleted
would equal 1 (the post) and relationshipsDeleted
would also equal equal 1 (the HAS_POST
relationship between the Post and its author).
Nested Delete
Say that if when you delete a User, you want to delete all of their Posts as well. This can be achieved using a single nested delete operations:
mutation {
deleteUsers(
where: {
name: "Jane Doe"
},
delete: {
posts: [
where: { }
]
}
) {
nodesDeleted
relationshipsDeleted
}
}
You may look at that empty where
argument and wonder what that’s doing. By the time the traversal has reached that argument, it has the context of only 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
}
}
Slightly easier to reason with, but the output Cypher statement will have a redundant WHERE
clause!
Was this page helpful?