What if your app could send tiny messages instead of big query texts every time?
Why Persisted queries in GraphQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a web app where users send many GraphQL queries. Each time, the app sends the full query text over the internet.
This means slow loading and lots of repeated data sent again and again.
Sending full queries every time wastes bandwidth and slows down responses.
It also risks typos or changes causing errors, and makes caching difficult.
Persisted queries let you save query texts on the server once.
Clients then send only a small ID to run the saved query.
This speeds up communication, reduces errors, and improves caching.
client sends full query text every time
client sends query ID only, server runs saved query
Persisted queries make apps faster and more reliable by sending less data and avoiding repeated query parsing.
A mobile app uses persisted queries to reduce data use and speed up loading, especially on slow networks.
Sending full queries each time is slow and error-prone.
Persisted queries store queries once and reuse them by ID.
This improves speed, reduces data use, and lowers errors.
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
