Bird
Raised Fist0
GraphQLquery~20 mins

Error handling on client 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
🎖️
GraphQL Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output when a GraphQL query returns an error?

Consider a GraphQL query that requests a user's name but the server returns an error due to a missing user ID. What will the client receive?

GraphQL
query GetUserName {
  user(id: null) {
    name
  }
}
A{"data": {"user": null}, "errors": [{"message": "User ID is required"}]}
B{"data": null}
C{"error": "User ID is required"}
D{"data": {"user": {"name": ""}}, "errors": []}
Attempts:
2 left
💡 Hint

GraphQL responses include both data and errors fields when errors occur.

🧠 Conceptual
intermediate
2:00remaining
How should a client handle partial data with errors in GraphQL?

A GraphQL query returns some data but also includes errors. What is the best practice for the client to handle this?

ADiscard all data and show only the error messages.
BRetry the query automatically without informing the user.
CDisplay the data and show error messages to the user.
DIgnore the errors and use the data as is.
Attempts:
2 left
💡 Hint

GraphQL allows partial data with errors; clients should inform users appropriately.

📝 Syntax
advanced
2:00remaining
Which GraphQL client code snippet correctly handles errors in a query response?

Given the following client code snippets, which one properly checks for errors in the GraphQL response?

GraphQL
const response = await fetchGraphQL(query);
// Which snippet correctly handles errors?
Aif (response.errors) { console.error(response.errors); } else { console.log(response.data); }
Bif (response.data.errors) { console.error(response.data.errors); } else { console.log(response.data); }
Cif (response.error) { console.error(response.error); } else { console.log(response.data); }
Dif (response.data) { console.error(response.data); } else { console.log(response.errors); }
Attempts:
2 left
💡 Hint

GraphQL responses have an errors field at the top level, not inside data.

optimization
advanced
2:00remaining
How can a client optimize error handling to reduce UI disruptions?

When a GraphQL query returns errors, how can the client minimize negative impact on user experience?

AIgnore all errors to avoid confusing the user.
BBatch multiple queries to reduce the number of error messages shown.
CShow detailed error stack traces to the user immediately.
DReload the entire page on any error.
Attempts:
2 left
💡 Hint

Batching queries can reduce the frequency of errors shown to users.

🔧 Debug
expert
2:00remaining
Why does this GraphQL client code fail to catch errors properly?

Examine the client code below. It does not detect errors returned by the GraphQL server. What is the cause?

GraphQL
const response = await fetchGraphQL(query);
if (!response.data) {
  console.error('No data received');
} else {
  console.log('Data:', response.data);
}
AThe code should check 'response.data.errors' to find errors.
BThe code incorrectly checks 'response.data' instead of 'response.error'.
CThe code should use try-catch around the fetchGraphQL call to catch errors.
DThe code does not check the 'errors' field in the response, so errors are ignored.
Attempts:
2 left
💡 Hint

GraphQL errors are in a separate 'errors' field, not inside 'data'.

Practice

(1/5)
1. What is the main purpose of error handling on the client side in GraphQL applications?
easy
A. To speed up the server response time
B. To keep the app stable and show clear messages to users
C. To change the GraphQL schema dynamically
D. To store data permanently on the client

Solution

  1. Step 1: Understand client-side error handling

    Error handling on the client is about managing problems that happen when fetching or processing data, so the app doesn't crash.
  2. Step 2: Identify the main goal

    The goal is to keep the app stable and inform users clearly about what went wrong.
  3. Final Answer:

    To keep the app stable and show clear messages to users -> Option B
  4. Quick Check:

    Error handling = stability + clear messages [OK]
Hint: Error handling means stability and clear user messages [OK]
Common Mistakes:
  • Thinking error handling speeds up server
  • Confusing error handling with schema changes
  • Assuming error handling stores data
2. Which of the following is the correct way to catch errors from a GraphQL query using promises on the client?
easy
A. client.query(...).then(...).catch(error => handleError(error))
B. client.query(...).catch(...).then(error => handleError(error))
C. client.query(...).try(error => handleError(error))
D. client.query(...).error(error => handleError(error))

Solution

  1. Step 1: Recall promise error handling syntax

    Promises use .then() for success and .catch() for errors.
  2. Step 2: Match correct chaining

    The correct order is .then(...).catch(...). client.query(...).then(...).catch(error => handleError(error)) matches this.
  3. Final Answer:

    client.query(...).then(...).catch(error => handleError(error)) -> Option A
  4. Quick Check:

    Promise catch method = .catch() [OK]
Hint: Use .then() before .catch() to handle errors [OK]
Common Mistakes:
  • Placing .catch() before .then()
  • Using non-existent .try() or .error() methods
  • Ignoring promise chaining order
3. Given the following code snippet, what will be logged if the GraphQL query fails?
client.query({ query: GET_USER })
  .then(response => console.log('User:', response.data.user))
  .catch(error => console.log('Error:', error.message))
medium
A. User: undefined
B. No output
C. User: null
D. Error: [error message from server]

Solution

  1. Step 1: Understand promise flow on failure

    If the query fails, the .catch() block runs, logging the error message.
  2. Step 2: Identify logged output

    The console logs 'Error:' followed by the error message from the server.
  3. Final Answer:

    Error: [error message from server] -> Option D
  4. Quick Check:

    Failed query triggers .catch() logging error [OK]
Hint: Failed queries run .catch() logging error message [OK]
Common Mistakes:
  • Assuming .then() runs on failure
  • Expecting 'User: undefined' instead of error
  • Thinking no output appears on error
4. Identify the error in this client-side GraphQL error handling code:
try {
  const response = await client.query({ query: GET_POSTS });
  console.log(response.data.posts);
} catch {
  console.log('Failed to fetch posts');
}
medium
A. client.query does not return a promise
B. Using await inside try block is invalid
C. Missing error parameter in catch block
D. console.log cannot be used inside catch

Solution

  1. Step 1: Review try-catch syntax

    The catch block should include an error parameter to access error details.
  2. Step 2: Identify missing error parameter

    The code uses catch { ... } without (error), preventing access to the error details.
  3. Final Answer:

    Missing error parameter in catch block -> Option C
  4. Quick Check:

    catch needs (error) parameter [OK]
Hint: Always include error parameter in catch block [OK]
Common Mistakes:
  • Omitting error parameter in catch
  • Thinking await is invalid in try
  • Believing client.query is not a promise
5. You want to show a friendly message to users when a GraphQL mutation fails due to network issues. Which approach correctly handles this on the client?
async function submitData() {
  try {
    const result = await client.mutate({ mutation: ADD_ITEM, variables: { name: 'Book' } });
    console.log('Item added:', result.data.addItem.id);
  } catch (error) {
    if (error.networkError) {
      alert('Network problem, please try again later.');
    } else {
      alert('An error occurred.');
    }
  }
}
hard
A. This code correctly distinguishes network errors and shows messages
B. The catch block should not check error properties
C. Using alert is not allowed in error handling
D. await cannot be used with client.mutate

Solution

  1. Step 1: Analyze error handling logic

    The catch block checks if the error is a network error and shows a specific message.
  2. Step 2: Confirm correct usage of await and alerts

    Using await with client.mutate is valid, and alerts are acceptable for user messages.
  3. Final Answer:

    This code correctly distinguishes network errors and shows messages -> Option A
  4. Quick Check:

    Check error.networkError to show friendly messages [OK]
Hint: Check error.networkError to show specific messages [OK]
Common Mistakes:
  • Ignoring networkError property in catch
  • Thinking alerts are disallowed
  • Misunderstanding await usage with mutate