Bird
Raised Fist0
GraphQLquery~20 mins

Persisted queries in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Persisted Queries Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of executing a persisted query with a missing hash?

Consider a GraphQL server that uses persisted queries identified by SHA-256 hashes. If a client sends a request with a hash that does not match any stored query, what will the server respond with?

AThe server ignores the hash and executes the query text sent by the client.
BThe server executes a default fallback query instead.
CThe server returns an error indicating the query was not found.
DThe server caches the new query under the given hash automatically.
Attempts:
2 left
💡 Hint

Think about what happens when the server cannot find a query matching the client's hash.

🧠 Conceptual
intermediate
2:00remaining
Why use persisted queries in GraphQL?

Which of the following is the main benefit of using persisted queries in GraphQL?

ATo reduce the size of requests by sending only query hashes instead of full query text.
BTo allow clients to send multiple queries in a single request.
CTo enable real-time subscriptions over WebSocket connections.
DTo automatically generate schema documentation.
Attempts:
2 left
💡 Hint

Think about network efficiency and bandwidth savings.

📝 Syntax
advanced
2:00remaining
Which option correctly shows how to register a persisted query in a GraphQL server?

Given a GraphQL server setup, which snippet correctly registers a persisted query with hash abc123 and query { user { id name } }?

ApersistedQueries.set('abc123', '{ user { id name } }');
BpersistedQueries.add('{ user { id name } }', 'abc123');
CpersistedQueries['abc123'] = '{ user { id name } }';
DpersistedQueries.register('{ user { id name } }', 'abc123');
Attempts:
2 left
💡 Hint

Consider common JavaScript Map methods for storing key-value pairs.

optimization
advanced
2:00remaining
How do persisted queries improve caching efficiency?

Which statement best explains how persisted queries help with caching on the server or CDN?

APersisted queries disable caching to ensure fresh data every time.
BPersisted queries compress the query text to reduce cache size.
CPersisted queries require clients to send full query text, improving cache hits.
DPersisted queries use fixed hashes, allowing caches to store and identify queries by hash instead of varying query text.
Attempts:
2 left
💡 Hint

Think about how identical keys help caches recognize repeated requests.

🔧 Debug
expert
2:00remaining
What error occurs if a client sends a persisted query hash but no query text and the hash is unknown?

A client sends a GraphQL request with only a persisted query hash that the server does not recognize, and no query text. What error will the server produce?

ATimeoutError because the server waits for query text indefinitely.
BPersistedQueryNotFound error indicating the hash is unknown.
CAuthenticationError because the client is unauthorized.
DSyntaxError due to missing query text.
Attempts:
2 left
💡 Hint

Consider the server's response when it cannot find the query for the given hash.

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

  1. Step 1: Understand what persisted queries do

    Persisted queries store the full GraphQL query on the server with a unique ID.
  2. Step 2: Identify the client-server interaction

    Clients send only the ID to run the query, reducing the data sent over the network.
  3. Final Answer:

    Clients send only a unique ID instead of the full query, saving bandwidth. -> Option B
  4. 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

  1. Step 1: Identify the persisted query request format

    Persisted queries send the unique query ID and variables, not the full query string.
  2. Step 2: Match the correct JSON structure

    { "id": "12345", "variables": { "userId": "1" } } sends an ID and variables, which is the correct persisted query format.
  3. Final Answer:

    { "id": "12345", "variables": { "userId": "1" } } -> Option A
  4. Quick Check:

    Persisted query request = ID + variables [OK]
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.

Options:
medium
A. []
B. [{"name": "User6"}, {"name": "User7"}, {"name": "User8"}, {"name": "User9"}, {"name": "User10"}]
C. [{"name": "User1"}, {"name": "User2"}, {"name": "User3"}, {"name": "User4"}, {"name": "User5"}]
D. Error: Query ID not found

Solution

  1. Step 1: Understand the query and variables

    The query with ID 'abc123' fetches users limited by the 'limit' variable, which is 5 here.
  2. Step 2: Determine the expected result

    Since the database has users User1 to User10, fetching the first 5 returns User1 to User5.
  3. Final Answer:

    [{"name": "User1"}, {"name": "User2"}, {"name": "User3"}, {"name": "User4"}, {"name": "User5"}] -> Option C
  4. Quick Check:

    Limit 5 users returns first 5 users [OK]
Hint: Variables control query output; check their values [OK]
Common Mistakes:
  • Assuming query returns last users
  • Thinking ID is invalid
  • Ignoring variables in query
4. A developer tries to use persisted queries but gets an error: Query ID not found. What is the most likely cause?
medium
A. The client sent a query ID that the server does not recognize.
B. The client sent the full query instead of the ID.
C. The server does not support GraphQL.
D. The client forgot to include variables.

Solution

  1. Step 1: Analyze the error message

    The error 'Query ID not found' means the server cannot find the query matching the sent ID.
  2. Step 2: Identify the cause

    This usually happens if the client sends an ID that was never registered or stored on the server.
  3. Final Answer:

    The client sent a query ID that the server does not recognize. -> Option A
  4. Quick Check:

    Unknown query ID causes 'Query ID not found' error [OK]
Hint: Check if query ID is registered on server [OK]
Common Mistakes:
  • Assuming full query sent causes this error
  • Blaming missing variables for this error
  • Thinking server lacks GraphQL support
5. You want to secure your GraphQL API by allowing only persisted queries. Which approach best achieves this?
hard
A. Disable persisted queries and use API keys instead.
B. Allow all queries but log those without IDs for review.
C. Require clients to send full queries and IDs together.
D. Reject any request that does not include a valid persisted query ID.

Solution

  1. 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.
  2. Step 2: Identify the best enforcement method

    Rejecting requests without valid IDs ensures only approved queries run, improving security.
  3. Final Answer:

    Reject any request that does not include a valid persisted query ID. -> Option D
  4. Quick Check:

    Only accept valid persisted query IDs to secure API [OK]
Hint: Block requests missing valid persisted query IDs [OK]
Common Mistakes:
  • Allowing all queries weakens security
  • Sending full queries defeats persisted query purpose
  • Relying only on API keys without query control