0
0
GraphQLquery~5 mins

Input types in GraphQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Input types
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of emails grows, the server does more work to handle each one.

Input Size (n)Approx. Operations
10Processes 10 emails
100Processes 100 emails
1000Processes 1000 emails

Pattern observation: The work grows directly with the number of emails.

Final Time Complexity

Time Complexity: O(n)

This means the time to process the input grows in a straight line with the number of emails.

Common Mistake

[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.

Interview Connect

Understanding how input size affects server work helps you explain performance clearly and shows you think about real user data.

Self-Check

"What if the emails list was nested inside another list? How would that change the time complexity?"