How to implement a set of GraphQL mutations in single transaction?
How to implement a set of GraphQL mutations in single transaction?
To implement a set of GraphQL mutations in a single transaction, you need to ensure that all the mutations are executed as part of a single atomic operation. This means that either all the mutations succeed, or if any one of them fails, all the changes are rolled back. Here are several approaches to achieve this, based on different GraphQL implementations and frameworks:
Dgraph supports atomic mutations through the use of the @transaction directive. This directive allows you to bundle multiple mutations into a single transaction. If any mutation within the transaction fails, the entire transaction is aborted and rolled back.
Example:
mutation @transaction(commit: true) {
addReview(input: [{comment: "reviewComment1"}]) {
review {
id
comment
}
}
addRegion(input: [{name: "regName1"}]) {
region {
id
name
}
}
}
In this example, both addReview and addRegion are part of a single transaction. If either mutation fails, the transaction is aborted, and all changes are rolled back.
Contember provides a transaction mutation wrapper that allows you to execute multiple mutations in a single transaction. If any mutation fails, the entire transaction is rolled back.
Example:
mutation {
transaction {
post1: createPost(data: {
title: "Hello world"
tags: [{create: {name: "graphql"}}]
}) {
ok
}
post2: createPost(data: {
title: "Lorem ipsum"
tags: [{create: {name: "contember"}}]
}) {
ok
}
}
}
Here, post1 and post2 are executed within a single transaction. If any mutation fails, the transaction is rolled back.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.