Transactions help keep your data safe and correct by making sure a group of actions all finish together or none at all.
Transaction handling in GraphQL
mutation {
startTransaction {
id
}
performOperations {
updateData(input: {...}) {
success
}
}
commitTransaction(id: "transaction_id") {
success
}
rollbackTransaction(id: "transaction_id") {
success
}
}GraphQL itself does not define transactions, but your backend can expose mutations to start, commit, or rollback transactions.
You usually call startTransaction first, then perform your data changes, and finally commit or rollback.
mutation {
startTransaction {
id
}
}mutation {
updateUser(id: "123", input: {name: "Alice"}) {
success
}
}mutation {
commitTransaction(id: "tx123") {
success
}
}mutation {
rollbackTransaction(id: "tx123") {
success
}
}This example starts a transaction, updates a user inside it, then commits the transaction to save changes.
mutation {
startTransaction {
id
}
}
# Suppose the returned id is "tx1"
mutation {
updateUser(id: "123", input: {name: "Alice"}, transactionId: "tx1") {
success
}
}
mutation {
commitTransaction(id: "tx1") {
success
}
}Not all GraphQL servers support transactions natively; check your backend's documentation.
Always commit or rollback to avoid leaving transactions open.
Use transaction IDs to link your operations together.
Transactions group multiple actions to keep data safe and consistent.
Start a transaction, do your changes, then commit or rollback.
GraphQL needs backend support to handle transactions.
Practice
Solution
Step 1: Understand what transactions do
Transactions ensure that a set of database operations are treated as a single unit.Step 2: Identify the purpose in GraphQL context
This means either all changes succeed or none do, keeping data consistent.Final Answer:
To group multiple actions so they all succeed or fail together -> Option AQuick Check:
Transactions = atomic group actions [OK]
- Thinking transactions speed up queries
- Confusing transactions with schema generation
- Assuming transactions encrypt data
Solution
Step 1: Identify how to start a transaction
Starting a transaction usually involves calling a method like startTransaction() asynchronously.Step 2: Check options for correctness
const transaction = await db.startTransaction(); correctly uses await and startTransaction(), while others are commit, rollback, or raw query.Final Answer:
const transaction = await db.startTransaction(); -> Option CQuick Check:
Start transaction = startTransaction() [OK]
- Using commit or rollback to start a transaction
- Using raw query strings instead of methods
- Missing await for async calls
updateUser()?
async function updateProfile(parent, args, context) {
const trx = await context.db.startTransaction();
try {
await trx.updateUser(args.userId, args.data);
await trx.commit();
return 'Success';
} catch (e) {
await trx.rollback();
return 'Failed';
}
}Solution
Step 1: Analyze the try block
The code tries to update the user and commit the transaction.Step 2: Understand error handling
If updateUser throws an error, the catch block rolls back and returns 'Failed'.Final Answer:
'Failed' -> Option BQuick Check:
Error during update = rollback + 'Failed' [OK]
- Assuming commit runs even on error
- Expecting unhandled exceptions instead of catch
- Thinking it returns 'Success' always
async function saveOrder(parent, args, context) {
const trx = context.db.startTransaction();
try {
await trx.insertOrder(args.order);
await trx.commit();
return 'Order saved';
} catch (error) {
await trx.rollback();
return 'Error saving order';
}
}Solution
Step 1: Check transaction start syntax
startTransaction() is asynchronous and must be awaited.Step 2: Identify missing await impact
Without await, trx is a Promise, causing errors on trx.insertOrder().Final Answer:
Missing await before startTransaction() -> Option AQuick Check:
Async startTransaction() needs await [OK]
- Forgetting await on async calls
- Calling commit before operations
- Ignoring rollback on errors
async function updateUserAndSettings(parent, args, context) {
const trx = await context.db.startTransaction();
try {
await trx.updateUser(args.userId, args.userData);
await trx.updateSettings(args.userId, args.settingsData);
await trx.commit();
return 'Update successful';
} catch (error) {
await trx.rollback();
return 'Update failed';
}
}Solution
Step 1: Understand transaction grouping
Both updates must be inside one transaction to succeed or fail together.Step 2: Analyze the code approach
The code wraps both updates in one transaction, committing only if both succeed.Final Answer:
Wrap both updates in a single transaction as shown -> Option DQuick Check:
Single transaction = atomic multi-update [OK]
- Using separate transactions causing partial updates
- Committing too early after first update
- Skipping transactions for multi-step changes
Ready to prove it?
Pass the test and this pattern moves unknown
