Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Persisted Queries with GraphQL
📖 Scenario: You are building a simple GraphQL API for a book store. To improve performance and security, you want to use persisted queries. Persisted queries store the GraphQL query strings on the server with unique IDs. Clients then send only the ID to run the query.
🎯 Goal: Build a basic persisted queries setup by creating a dictionary to store queries, a configuration for allowed queries, a function to retrieve queries by ID, and a final step to simulate running a persisted query by ID.
📋 What You'll Learn
Create a dictionary called persisted_queries with exact query IDs and query strings
Add a list called allowed_query_ids containing the allowed query IDs
Write a function called get_query_by_id that takes a query ID and returns the query string if allowed
Simulate running a persisted query by calling get_query_by_id with a valid ID and storing the result in executed_query
💡 Why This Matters
🌍 Real World
Persisted queries improve performance and security in GraphQL APIs by avoiding sending full query strings over the network repeatedly.
💼 Career
Understanding persisted queries is important for backend developers working with GraphQL to optimize API efficiency and protect against injection attacks.
Progress0 / 4 steps
1
Create the persisted queries dictionary
Create a dictionary called persisted_queries with these exact entries: 'q1' mapped to '{ books { title author } }', and 'q2' mapped to '{ authors { name booksCount } }'.
GraphQL
Hint
Use curly braces to create the dictionary and exact keys and values as strings.
2
Add allowed query IDs list
Create a list called allowed_query_ids containing the strings 'q1' and 'q2'.
GraphQL
Hint
Use square brackets to create the list with the exact strings.
3
Write the function to get query by ID
Write a function called get_query_by_id that takes a parameter query_id. It should return the query string from persisted_queries only if query_id is in allowed_query_ids. Otherwise, return None.
GraphQL
Hint
Use an if statement to check membership and dict.get() to retrieve the query string.
4
Simulate running a persisted query
Call the function get_query_by_id with the argument 'q1' and assign the result to a variable called executed_query.
GraphQL
Hint
Call the function with the string 'q1' and assign the result to executed_query.
Practice
(1/5)
1. What is the main benefit of using persisted queries in GraphQL?
easy
A. The server stores user credentials for faster login.
B. Clients send only a unique ID instead of the full query, saving bandwidth.
C. Queries are automatically optimized by the client.
D. It allows clients to write queries without validation.
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 B
Quick Check:
Persisted queries reduce data sent = Clients send only a unique ID instead of the full query, saving bandwidth. [OK]
Hint: Persisted queries send IDs, not full queries [OK]
Common Mistakes:
Thinking clients send full queries every time
Confusing persisted queries with client-side caching
Believing persisted queries store user data
2. Which of the following is the correct way to send a persisted query request in GraphQL?
easy
A. { "id": "12345", "variables": { "userId": "1" } }
B. { "query": "{ user(id: 1) { name } }" }
C. { "mutation": "updateUser" }
D. { "headers": { "Authorization": "token" } }
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.
Hint: Persisted queries use ID field, not full query [OK]
Common Mistakes:
Sending full query instead of ID
Using mutation key instead of id
Confusing headers with query payload
3. Given the following persisted query setup, what will the server return when the client sends { "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.