Mutation syntax in GraphQL - Time & Space Complexity
When we use GraphQL mutations, we want to know how the time to complete the mutation changes as we add more data.
We ask: How does the work grow when the mutation handles more items?
Analyze the time complexity of the following mutation syntax.
mutation AddBooks($books: [BookInput!]!) {
addBooks(books: $books) {
id
title
author
}
}
This mutation adds multiple books at once, sending a list of book details to be saved.
Look for repeated actions inside the mutation.
- Primary operation: Processing each book in the input list.
- How many times: Once for each book in the list.
As you add more books, the work grows with the number of books.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times processing |
| 100 | 100 times processing |
| 1000 | 1000 times processing |
Pattern observation: The work grows directly with the number of books you add.
Time Complexity: O(n)
This means the time to complete the mutation grows in a straight line with the number of books.
[X] Wrong: "Adding more books won't affect the time much because it's just one mutation call."
[OK] Correct: Each book still needs to be processed, so more books mean more work inside the mutation.
Understanding how mutation time grows helps you explain how your API handles bigger requests smoothly.
"What if the mutation also updated related author records for each book? How would the time complexity change?"
Practice
mutation in GraphQL?Solution
Step 1: Understand GraphQL operations
GraphQL has queries to fetch data and mutations to change data.Step 2: Identify mutation role
Mutations are used to add, update, or delete data on the server.Final Answer:
To change data on the server like adding, updating, or deleting records -> Option DQuick Check:
Mutation = change data [OK]
- Confusing mutations with queries
- Thinking mutations define schema
- Assuming mutations create servers
Solution
Step 1: Recognize mutation keyword usage
Mutations start with the keywordmutationfollowed by braces containing the mutation call.Step 2: Check correct structure
mutation { addUser(name: "Alice") { id name } } correctly usesmutation { addUser(...) { ... } }syntax.Final Answer:
mutation { addUser(name: "Alice") { id name } } -> Option AQuick Check:
Mutation starts with 'mutation' keyword and braces [OK]
- Using 'query' instead of 'mutation'
- Placing mutation name before 'mutation' keyword
- Missing braces around mutation call
mutation {
createPost(title: "Hello") {
id
title
}
}Solution
Step 1: Analyze mutation call and requested fields
The mutation callscreatePostwith title "Hello" and requestsidandtitlefields back.Step 2: Understand expected return
The server returns the new post's id and title as requested.Final Answer:
{ "data": { "createPost": { "id": 1, "title": "Hello" } } } -> Option CQuick Check:
Requested fields returned in data [OK]
- Expecting no return data
- Confusing syntax error with correct syntax
- Ignoring requested fields in response
mutation {
updateUser(id: 5, name: "Bob")
}Solution
Step 1: Check mutation syntax
Mutation calls must specify which fields to return inside braces after the call.Step 2: Identify missing return fields
This mutation lacks braces with requested fields afterupdateUser, causing syntax error.Final Answer:
Missing requested return fields after mutation call -> Option AQuick Check:
Mutations must request return fields [OK]
- Omitting return fields
- Using 'query' keyword for mutations
- Misplacing parentheses
Solution
Step 1: Confirm mutation keyword and structure
Mutation must start withmutationkeyword followed by braces containing the mutation call.Step 2: Check mutation call and requested fields
mutation { deleteUser(id: 10) { name email } } correctly callsdeleteUserwithid: 10and requestsnameandemailfields.Final Answer:
mutation { deleteUser(id: 10) { name email } } -> Option BQuick Check:
Correct mutation syntax with requested return fields [OK]
- Placing mutation name after keyword incorrectly
- Not requesting return fields
- Using wrong keyword order
Ready to prove it?
Pass the test and this pattern moves unknown
