Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Mutations  ›  Lesson

Multiple Fields in a Mutation

Multiple Fields in Mutation 12 minThe top-level fields of a mutation run one after another in the order written, so later writes see earlier ones, but a failure in a later field does not undo an earlier one.

Multiple Fields in a Mutation

A shop admin runs a weekend sale: drop the price of The Quiet Ledger, then post a staff review to promote it. Both changes can go in one mutation request. But when one request does two writes, you need to know which happens first and what happens if the second fails.

The rule

Mutation fields run in order, each finishing before the next begins.

A mutation can list several top-level fields:

mutation WeekendSale {
  updatePrice(bookId: "b2", price: 299) {
    title
    price
  }
  addReview(input: { bookId: "b2", rating: 5, text: "Staff pick for the sale." }) {
    id
    book { price }
  }
}

GraphQL runs the top-level fields of a mutation serially: updatePrice runs completely, including its selected fields, and only then does addReview start. So the review's book { price } is guaranteed to show the new price, 299.

Queries are different. Their top-level fields have no side effects, so a server is free to resolve them in parallel. Only the top level of a mutation is serial; the fields nested inside each result resolve the normal way.

Two more rules follow from what you already know:

  • Same field twice needs aliases. Posting two reviews means two addReview fields with different arguments, so name them: first: addReview(...) and second: addReview(...).
  • Order is not a transaction. If addReview fails, the price change has already happened and stays. The response carries an error for the failed field. If two changes must succeed or fail together, give the schema one field, such as startSale, whose resolver does both inside a database transaction.

The shortcut

Read a multi-field mutation like a numbered list: step 1 finishes, then step 2 sees its result. Then ask, "if step 2 fails, am I happy with step 1 on its own?"

Checking the weekend saleStep 1 of 4
  1. Number the fields

    1: updatePrice to 299. 2: addReview on the same book.

The trap is assuming a mutation is all-or-nothing. GraphQL guarantees order, not rollback.

Drills

Answer fast — trust the rule.

quick drill

How does a server run the two top-level fields of a mutation?

quick drill

How may a server run the two top-level fields of a query?

quick drill

In WeekendSale, addReview fails. What happened to the price?

quick drill

You want to post two reviews in one mutation. What must you add?

quick drill

A price change and a stock update must succeed or fail together. What is the best design?

Remember

  • Mutation fields run in order, each finishing before the next begins.
  • Read them as a numbered list and ask what happens if a later step fails.
  • Trap: order is not a transaction; combine writes into one field when they must be atomic.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTwo reviews in one request

A reading club posts two reviews of book b1 in one request. Write the mutation, explain in which order the two writes happen, what the response keys are, and what the club should expect if the second review is rejected.