Persisted queries in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using persisted queries in GraphQL, we want to understand how the time to find and run a saved query changes as we store more queries.
We ask: How does the system's work grow when the number of saved queries grows?
Analyze the time complexity of the following code snippet.
query GetUser($id: ID!) {
user(id: $id) {
id
name
}
}
# Persisted queries are stored with a unique key.
# When a request comes, the server looks up the query by key and executes it.
# This example shows the lookup step.
This snippet represents a typical persisted query lookup by a unique key before execution.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Searching for the query by its key in the stored collection.
- How many times: Once per request, but the search may scan multiple stored queries depending on data structure.
As the number of saved queries grows, the time to find one depends on how the queries are stored.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks if stored in a list |
| 100 | About 100 checks if stored in a list |
| 1000 | About 1000 checks if stored in a list |
Pattern observation: If stored in a simple list, the search grows linearly with the number of saved queries.
Time Complexity: O(n)
This means the time to find a persisted query grows directly with how many queries are saved if using a simple search.
[X] Wrong: "Looking up a persisted query always takes the same time no matter how many queries are saved."
[OK] Correct: If the queries are stored in a list or array, the server may need to check many entries, so time grows with the number of queries.
Understanding how data lookup time grows helps you design systems that stay fast as they grow. This skill is useful when working with APIs and databases in real projects.
What if we changed the storage from a list to a hash map? How would the time complexity change?
Practice
persisted queries in GraphQL?Solution
Step 1: Understand what persisted queries do
Persisted queries store the full GraphQL query on the server with a unique ID.Step 2: Identify the client-server interaction
Clients send only the ID to run the query, reducing the data sent over the network.Final Answer:
Clients send only a unique ID instead of the full query, saving bandwidth. -> Option BQuick Check:
Persisted queries reduce data sent = Clients send only a unique ID instead of the full query, saving bandwidth. [OK]
- Thinking clients send full queries every time
- Confusing persisted queries with client-side caching
- Believing persisted queries store user data
Solution
Step 1: Identify the persisted query request format
Persisted queries send the unique query ID and variables, not the full query string.Step 2: Match the correct JSON structure
{ "id": "12345", "variables": { "userId": "1" } } sends an ID and variables, which is the correct persisted query format.Final Answer:
{ "id": "12345", "variables": { "userId": "1" } } -> Option AQuick Check:
Persisted query request = ID + variables [OK]
- Sending full query instead of ID
- Using mutation key instead of id
- Confusing headers with query payload
{ "id": "abc123", "variables": { "limit": 5 } } if the query with ID abc123 fetches the first limit users?Assume the database has 10 users named User1 to User10.
Options:
Solution
Step 1: Understand the query and variables
The query with ID 'abc123' fetches users limited by the 'limit' variable, which is 5 here.Step 2: Determine the expected result
Since the database has users User1 to User10, fetching the first 5 returns User1 to User5.Final Answer:
[{"name": "User1"}, {"name": "User2"}, {"name": "User3"}, {"name": "User4"}, {"name": "User5"}] -> Option CQuick Check:
Limit 5 users returns first 5 users [OK]
- Assuming query returns last users
- Thinking ID is invalid
- Ignoring variables in query
Query ID not found. What is the most likely cause?Solution
Step 1: Analyze the error message
The error 'Query ID not found' means the server cannot find the query matching the sent ID.Step 2: Identify the cause
This usually happens if the client sends an ID that was never registered or stored on the server.Final Answer:
The client sent a query ID that the server does not recognize. -> Option AQuick Check:
Unknown query ID causes 'Query ID not found' error [OK]
- Assuming full query sent causes this error
- Blaming missing variables for this error
- Thinking server lacks GraphQL support
Solution
Step 1: Understand API security with persisted queries
Allowing only persisted queries means the server accepts requests only if they have a valid stored query ID.Step 2: Identify the best enforcement method
Rejecting requests without valid IDs ensures only approved queries run, improving security.Final Answer:
Reject any request that does not include a valid persisted query ID. -> Option DQuick Check:
Only accept valid persisted query IDs to secure API [OK]
- Allowing all queries weakens security
- Sending full queries defeats persisted query purpose
- Relying only on API keys without query control
