Persisted queries help make GraphQL requests faster and safer by storing queries on the server. This way, clients only send a small ID instead of the full query text.
Persisted queries in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
1. Store your GraphQL query on the server and assign it a unique ID. 2. Client sends a request with the query ID instead of the full query. 3. Server looks up the query by ID and executes it.
The exact method to store and retrieve persisted queries depends on your GraphQL server setup.
Clients must know the query IDs beforehand to use persisted queries.
# Example of a stored query on the server: query GetUser { user(id: "123") { name email } } # This query is saved with ID: "getUser123"
# Client sends only the ID:
{
"id": "getUser123"
}# Server receives the ID, finds the query, and runs it. # Server returns the user data as usual.
This example shows how a client sends a query ID, the server looks up the full query, runs it, and returns the data.
# Simulated example using a JSON request for a persisted query # Client request: { "id": "getUser123" } # Server stored queries: { "getUser123": "query GetUser { user(id: \"123\") { name email } }" } # Server executes the stored query and returns: { "data": { "user": { "name": "Alice", "email": "alice@example.com" } } }
Persisted queries reduce bandwidth by sending only IDs instead of full queries.
They improve security by limiting which queries can run on the server.
Make sure to handle errors when an unknown query ID is sent.
Persisted queries store GraphQL queries on the server with unique IDs.
Clients send only the ID to run a query, saving data and improving speed.
This technique also helps secure your API by controlling allowed queries.
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
