Bird
Raised Fist0
GraphQLquery~20 mins

Transaction handling in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Transaction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the result of this transaction sequence?

Consider a GraphQL API that supports transactions with the following operations:

mutation {
  startTransaction {
    transactionId
  }
}

mutation {
  updateUser(id: 1, name: "Alice") {
    id
    name
  }
}

mutation {
  commitTransaction(transactionId: "tx123") {
    success
  }
}

Assuming the updateUser mutation is executed within the transaction started, what will be the final name of the user with id 1 after commit?

AThe user's name will be updated to "Alice" after commit.
BThe user's name will remain unchanged because commitTransaction does not apply changes.
CThe user's name will be "Alice" immediately after updateUser, even before commit.
DThe transaction will fail and the user's name will be rolled back to the previous value.
Attempts:
2 left
💡 Hint

Think about what commitTransaction does in a transaction context.

📝 Syntax
intermediate
1:30remaining
Identify the syntax error in this transaction mutation

Which of the following GraphQL mutations correctly starts a transaction?

Amutation { startTransaction: transactionId }
Bmutation startTransaction { transactionId }
Cmutation { startTransaction(transactionId) }
Dmutation { startTransaction { transactionId } }
Attempts:
2 left
💡 Hint

Recall the correct syntax for GraphQL mutations and field selection.

❓ optimization
advanced
2:30remaining
Optimize transaction usage for multiple updates

You want to update multiple user records atomically using GraphQL transactions. Which approach is best to minimize transaction overhead?

AStart a transaction, update each user one by one inside it, then commit once at the end.
BStart and commit a separate transaction for each user update.
CUpdate all users without transactions to improve speed.
DStart a transaction, update one user, commit, then start a new transaction for the next user.
Attempts:
2 left
💡 Hint

Think about how transactions reduce overhead when grouping operations.

🔧 Debug
advanced
2:00remaining
Why does this transaction rollback unexpectedly?

Given this sequence:

mutation { startTransaction { transactionId } }
mutation { updateUser(id: 2, name: "Bob") { id name } }
mutation { rollbackTransaction(transactionId: "tx999") { success } }

The updateUser mutation is executed, but the rollbackTransaction uses a wrong transactionId. What happens to the update?

AThe updateUser mutation fails because the transaction is invalid.
BThe updateUser change is rolled back despite the wrong transactionId.
CThe updateUser change remains uncommitted and visible only inside the transaction.
DThe updateUser change is committed automatically because rollback failed.
Attempts:
2 left
💡 Hint

Consider what happens if rollbackTransaction targets a non-existent transaction.

🧠 Conceptual
expert
3:00remaining
What is the isolation level effect in GraphQL transactions?

In a GraphQL API supporting transactions, what does the isolation level control?

AThe order in which mutations are executed inside a transaction.
BHow visible uncommitted changes are to other transactions.
CThe maximum time a transaction can stay open before auto-commit.
DThe number of concurrent transactions allowed per user.
Attempts:
2 left
💡 Hint

Think about what isolation means in database transactions.

Practice

(1/5)
1. What is the main purpose of using transactions in a database when working with GraphQL?
easy
A. To group multiple actions so they all succeed or fail together
B. To speed up query execution by caching results
C. To automatically generate GraphQL schemas
D. To encrypt data sent over the network

Solution

  1. Step 1: Understand what transactions do

    Transactions ensure that a set of database operations are treated as a single unit.
  2. Step 2: Identify the purpose in GraphQL context

    This means either all changes succeed or none do, keeping data consistent.
  3. Final Answer:

    To group multiple actions so they all succeed or fail together -> Option A
  4. Quick Check:

    Transactions = atomic group actions [OK]
Hint: Transactions keep multiple changes safe together [OK]
Common Mistakes:
  • Thinking transactions speed up queries
  • Confusing transactions with schema generation
  • Assuming transactions encrypt data
2. Which of the following is the correct way to start a transaction in a GraphQL resolver using a typical backend?
easy
A. const transaction = await db.rollback();
B. const transaction = db.commit();
C. const transaction = await db.startTransaction();
D. const transaction = db.query('BEGIN');

Solution

  1. Step 1: Identify how to start a transaction

    Starting a transaction usually involves calling a method like startTransaction() asynchronously.
  2. Step 2: Check options for correctness

    const transaction = await db.startTransaction(); correctly uses await and startTransaction(), while others are commit, rollback, or raw query.
  3. Final Answer:

    const transaction = await db.startTransaction(); -> Option C
  4. Quick Check:

    Start transaction = startTransaction() [OK]
Hint: Start transactions with async startTransaction() method [OK]
Common Mistakes:
  • Using commit or rollback to start a transaction
  • Using raw query strings instead of methods
  • Missing await for async calls
3. Given this GraphQL resolver snippet, what will be the output if an error occurs during 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';
  }
}
medium
A. 'Success'
B. 'Failed'
C. Throws an unhandled exception
D. Returns null

Solution

  1. Step 1: Analyze the try block

    The code tries to update the user and commit the transaction.
  2. Step 2: Understand error handling

    If updateUser throws an error, the catch block rolls back and returns 'Failed'.
  3. Final Answer:

    'Failed' -> Option B
  4. Quick Check:

    Error during update = rollback + 'Failed' [OK]
Hint: Errors trigger rollback and return 'Failed' [OK]
Common Mistakes:
  • Assuming commit runs even on error
  • Expecting unhandled exceptions instead of catch
  • Thinking it returns 'Success' always
4. Identify the error in this GraphQL resolver transaction handling code:
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';
  }
}
medium
A. Missing await before startTransaction()
B. No rollback in catch block
C. commit() should be called before insertOrder()
D. Returning string instead of object

Solution

  1. Step 1: Check transaction start syntax

    startTransaction() is asynchronous and must be awaited.
  2. Step 2: Identify missing await impact

    Without await, trx is a Promise, causing errors on trx.insertOrder().
  3. Final Answer:

    Missing await before startTransaction() -> Option A
  4. Quick Check:

    Async startTransaction() needs await [OK]
Hint: Always await async transaction start calls [OK]
Common Mistakes:
  • Forgetting await on async calls
  • Calling commit before operations
  • Ignoring rollback on errors
5. You want to update a user's profile and their related settings in one transaction using GraphQL. Which approach ensures both updates succeed or fail together?
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';
  }
}
hard
A. Use separate transactions for user and settings updates
B. Do not use transactions for related updates
C. Commit after each update separately
D. Wrap both updates in a single transaction as shown

Solution

  1. Step 1: Understand transaction grouping

    Both updates must be inside one transaction to succeed or fail together.
  2. Step 2: Analyze the code approach

    The code wraps both updates in one transaction, committing only if both succeed.
  3. Final Answer:

    Wrap both updates in a single transaction as shown -> Option D
  4. Quick Check:

    Single transaction = atomic multi-update [OK]
Hint: Group related updates in one transaction [OK]
Common Mistakes:
  • Using separate transactions causing partial updates
  • Committing too early after first update
  • Skipping transactions for multi-step changes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard