Bird
Raised Fist0
GraphQLquery~10 mins

Transaction handling in GraphQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a transaction in GraphQL.

GraphQL
mutation StartTransaction {
  [1] {
    success
  }
}
Drag options to blanks, or click blank then click option'
AbeginTransaction
BcommitTransaction
CrollbackTransaction
DendTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitTransaction to start instead of commit at the end.
Using rollbackTransaction which cancels changes.
2fill in blank
medium

Complete the code to commit a transaction after changes.

GraphQL
mutation CommitChanges {
  [1] {
    success
  }
}
Drag options to blanks, or click blank then click option'
AcommitTransaction
BbeginTransaction
CrollbackTransaction
DsaveTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using rollbackTransaction which cancels changes.
Using beginTransaction which starts but does not save.
3fill in blank
hard

Fix the error in the mutation to cancel a transaction.

GraphQL
mutation CancelTransaction {
  [1] {
    success
  }
}
Drag options to blanks, or click blank then click option'
AcommitTransaction
BrollbackTransaction
CbeginTransaction
DcancelTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitTransaction which saves changes.
Using beginTransaction which starts a transaction.
4fill in blank
hard

Fill both blanks to start and then commit a transaction.

GraphQL
mutation ManageTransaction {
  [1] {
    success
  }
  [2] {
    success
  }
}
Drag options to blanks, or click blank then click option'
AbeginTransaction
BcommitTransaction
CrollbackTransaction
DendTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping commit and rollback.
Using endTransaction which is not standard.
5fill in blank
hard

Fill all three blanks to start, rollback, and then commit a transaction correctly.

GraphQL
mutation ComplexTransaction {
  [1] {
    success
  }
  [2] {
    success
  }
  [3] {
    success
  }
}
Drag options to blanks, or click blank then click option'
AbeginTransaction
BcommitTransaction
CrollbackTransaction
DendTransaction
Attempts:
3 left
💡 Hint
Common Mistakes
Committing before rollback.
Using endTransaction which is not standard.

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