Input types in GraphQL - Time & Space Complexity
When using input types in GraphQL, it's important to understand how the size of the input affects the work the server does.
We want to know how the server's effort grows as the input data gets bigger.
Analyze the time complexity of the following code snippet.
input UserInput {
name: String!
emails: [String!]!
}
mutation addUser($input: UserInput!) {
addUser(input: $input) {
id
name
}
}
This mutation accepts a user input with a name and a list of emails, then adds the user.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Processing each email in the emails list.
- How many times: Once for each email in the input list.
As the number of emails grows, the server does more work to handle each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes 10 emails |
| 100 | Processes 100 emails |
| 1000 | Processes 1000 emails |
Pattern observation: The work grows directly with the number of emails.
Time Complexity: O(n)
This means the time to process the input grows in a straight line with the number of emails.
[X] Wrong: "The input size does not affect the time because it's just one mutation call."
[OK] Correct: Even though it's one call, the server must handle each email inside the input, so more emails mean more work.
Understanding how input size affects server work helps you explain performance clearly and shows you think about real user data.
"What if the emails list was nested inside another list? How would that change the time complexity?"