Schema evolution strategies in GraphQL - Time & Space Complexity
When we change a GraphQL schema over time, we want to know how these changes affect the work the server does.
We ask: How does the effort to handle queries grow as the schema evolves?
Analyze the time complexity of the following schema evolution approach.
type Query {
user(id: ID!): User
users: [User]
}
type User {
id: ID!
name: String
email: String
# New field added in evolution
phone: String
}
This snippet shows adding a new optional field phone to the User type without removing old fields.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Resolving each field requested in a query for each user.
- How many times: Once per user per requested field, including new and old fields.
As the number of users grows, the server resolves more fields for each user.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 users x fields requested |
| 100 | 100 users x fields requested |
| 1000 | 1000 users x fields requested |
Pattern observation: The work grows linearly with the number of users and the number of fields requested.
Time Complexity: O(n × f)
This means the time grows with the number of items (users) and the number of fields requested in the query.
[X] Wrong: "Adding a new field to the schema does not affect query execution time at all."
[OK] Correct: Even if the field is optional, if clients request it, the server must resolve it for each item, increasing work.
Understanding how schema changes affect query execution helps you design APIs that stay fast and reliable as they grow.
What if we removed old fields instead of adding new ones? How would the time complexity change?