Input arguments for mutations in GraphQL - Time & Space Complexity
When we use input arguments in GraphQL mutations, we want to know how the time to complete the mutation changes as the input grows.
We ask: How does the work needed grow when we send more or bigger inputs?
Analyze the time complexity of the following code snippet.
mutation AddUsers($users: [UserInput!]!) {
addUsers(users: $users) {
id
name
}
}
This mutation takes a list of users as input and adds each user to the database, returning their ids and names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Processing each user in the input list one by one.
- How many times: Once for each user in the input list.
As the number of users in the input list grows, the work grows in a similar way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 user insertions |
| 100 | 100 user insertions |
| 1000 | 1000 user insertions |
Pattern observation: The work grows directly with the number of users; doubling users doubles the work.
Time Complexity: O(n)
This means the time to complete the mutation grows linearly with the number of input users.
[X] Wrong: "Adding multiple users at once takes the same time as adding one user."
[OK] Correct: Each user requires separate processing, so more users mean more work and more time.
Understanding how input size affects mutation time helps you explain performance in real projects and shows you think about efficient data handling.
"What if the mutation also validated each user's data with a complex check? How would the time complexity change?"