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?"