Input type for complex arguments in GraphQL - Time & Space Complexity
When we use input types for complex arguments in GraphQL, we want to know how the time to process these inputs changes as the input size grows.
We ask: How does the work grow when the input has more fields or nested data?
Analyze the time complexity of the following GraphQL mutation using a complex input type.
mutation AddOrder($order: OrderInput!) {
addOrder(order: $order) {
id
status
}
}
input OrderInput {
customerId: ID!
items: [OrderItemInput!]!
}
input OrderItemInput {
productId: ID!
quantity: Int!
}
This mutation takes an order with multiple items as input and processes it.
Look for repeated work inside the input processing.
- Primary operation: Looping through each item in the
itemslist. - How many times: Once for each item in the order.
As the number of items in the order grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 items | Process 10 items |
| 100 items | Process 100 items |
| 1000 items | Process 1000 items |
Pattern observation: The work grows directly with the number of items; doubling items doubles the work.
Time Complexity: O(n)
This means the time to process the input grows in a straight line with the number of items.
[X] Wrong: "Processing a complex input type always takes constant time because it is just one argument."
[OK] Correct: Even though it is one argument, if it contains a list of items, the server must handle each item separately, so the time grows with the list size.
Understanding how input size affects processing time helps you explain performance clearly and shows you can think about real-world data handling.
What if the items list was nested deeper inside another input object? How would that affect the time complexity?