Bird
Raised Fist0
GraphQLquery~20 mins

Custom error extensions in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this GraphQL error response with custom extensions?

Consider a GraphQL query that triggers an error with custom extensions. What will the error response contain?

GraphQL
{
  "errors": [
    {
      "message": "User not found",
      "extensions": {
        "code": "USER_NOT_FOUND",
        "timestamp": "2024-06-01T12:00:00Z"
      }
    }
  ]
}
A{"errors":[{"message":"User not found","extensions":{"code":"USER_NOT_FOUND","timestamp":"2024-06-01T12:00:00Z"}}]}
B{"data":null,"errors":[{"message":"User not found"}]}
C{"errors":[{"message":"User not found","code":"USER_NOT_FOUND"}]}
D{"errors":[{"message":"User not found","extensions":{"errorCode":"USER_NOT_FOUND"}}]}
Attempts:
2 left
💡 Hint

Custom error extensions appear inside the extensions field in the error object.

🧠 Conceptual
intermediate
1:30remaining
Which field in a GraphQL error response holds custom error details?

In GraphQL, where do you put extra information like error codes or debug data in an error response?

AThe <code>message</code> field
BThe <code>locations</code> field
CThe <code>data</code> field
DThe <code>extensions</code> field
Attempts:
2 left
💡 Hint

Think about where extra metadata about errors is stored.

📝 Syntax
advanced
2:00remaining
Identify the invalid GraphQL error extension format

Which of the following GraphQL error objects has an invalid extensions field format?

A{"message":"Timeout","extensions":"TIMEOUT_ERROR"}
B{"message":"Invalid input","extensions":{"code":"BAD_INPUT"}}
C{"message":"Unauthorized","extensions":{"code":"UNAUTH"}}
D{"message":"Not found","extensions":{"code":"NOT_FOUND","retry":true}}
Attempts:
2 left
💡 Hint

The extensions field must be an object, not a string.

🔧 Debug
advanced
2:30remaining
Why does this GraphQL error extension not appear in the client response?

A server returns this error object but the client does not see the extensions field. What is the likely cause?

{
  "errors": [
    {
      "message": "Access denied",
      "extensions": null
    }
  ]
}
AThe <code>message</code> field is missing
BThe client filters out <code>extensions</code> by default
CThe server must send <code>extensions</code> as an object, not null
DGraphQL does not support <code>extensions</code> in errors
Attempts:
2 left
💡 Hint

Check the type of the extensions field.

❓ optimization
expert
3:00remaining
How to efficiently include error codes in GraphQL responses for multiple errors?

You want to send multiple errors with custom codes in a GraphQL response. Which approach is best for performance and clarity?

AOmit error codes to reduce response size
BInclude a separate <code>extensions</code> object with a <code>code</code> field for each error
CEmbed error codes inside the <code>message</code> string for each error
DSend one <code>extensions</code> object at the root with all error codes combined
Attempts:
2 left
💡 Hint

Think about how GraphQL error objects are structured.

Practice

(1/5)
1. What is the main purpose of using extensions in GraphQL error responses?
easy
A. To change the data returned by the query
B. To hide errors from the client
C. To speed up the query execution
D. To provide extra information like error codes and details

Solution

  1. Step 1: Understand the role of error extensions

    Extensions add extra info to errors, such as codes or metadata.
  2. Step 2: Compare with other options

    Extensions do not change data, speed, or hide errors; they enrich error info.
  3. Final Answer:

    To provide extra information like error codes and details -> Option D
  4. Quick Check:

    Extensions add info = B [OK]
Hint: Extensions add details to errors, not data or speed [OK]
Common Mistakes:
  • Thinking extensions change query data
  • Assuming extensions hide errors
  • Confusing extensions with performance tools
2. Which of the following is the correct way to add a custom error extension in a GraphQL error object?
easy
A. { message: 'Error', extensions: ['BAD_REQUEST'] }
B. { message: 'Error', code: 'BAD_REQUEST' }
C. { message: 'Error', extensions: { code: 'BAD_REQUEST' } }
D. { error: 'Error', extensions: 'BAD_REQUEST' }

Solution

  1. Step 1: Identify correct error object structure

    The error object must have a message and an extensions object with code inside.
  2. Step 2: Check each option's syntax

    { message: 'Error', extensions: { code: 'BAD_REQUEST' } } correctly nests code inside extensions; others misuse or misplace fields.
  3. Final Answer:

    { message: 'Error', extensions: { code: 'BAD_REQUEST' } } -> Option C
  4. Quick Check:

    Extensions is an object with code = D [OK]
Hint: Extensions must be an object with keys like code [OK]
Common Mistakes:
  • Putting code outside extensions
  • Using extensions as a string or array
  • Misnaming fields like error instead of message
3. Given this GraphQL error response:
{"errors":[{"message":"Not found","extensions":{"code":"NOT_FOUND","timestamp":"2024-06-01T12:00:00Z"}}]}

What will the client see in the extensions field?
medium
A. {"code":"NOT_FOUND","timestamp":"2024-06-01T12:00:00Z"}
B. "Not found"
C. {"message":"Not found"}
D. An empty object {}

Solution

  1. Step 1: Locate the extensions field in the error

    The extensions field contains code and timestamp as key-value pairs.
  2. Step 2: Understand what client receives

    The client sees the extensions object exactly as given, with code and timestamp.
  3. Final Answer:

    {"code":"NOT_FOUND","timestamp":"2024-06-01T12:00:00Z"} -> Option A
  4. Quick Check:

    Extensions content = A [OK]
Hint: Extensions field shows all custom error details as object [OK]
Common Mistakes:
  • Confusing message with extensions
  • Expecting extensions to be a string
  • Ignoring extra fields like timestamp
4. You wrote this error response in your GraphQL server:
{ message: 'Unauthorized', extensions: 'AUTH_ERROR' }

Why does this cause a problem?
medium
A. Extensions must be an object, not a string
B. Message cannot be 'Unauthorized'
C. Extensions field is missing
D. The error object must not have extensions

Solution

  1. Step 1: Check the type of extensions field

    Extensions must be an object with key-value pairs, not a string.
  2. Step 2: Identify the error cause

    Using a string for extensions causes syntax or runtime errors in GraphQL.
  3. Final Answer:

    Extensions must be an object, not a string -> Option A
  4. Quick Check:

    Extensions type error = A [OK]
Hint: Extensions must always be an object, never a string [OK]
Common Mistakes:
  • Using string instead of object for extensions
  • Ignoring GraphQL error format rules
  • Assuming message content causes error
5. You want to send a GraphQL error with a custom extension that includes an error code and a retry flag set to false. Which of these error objects correctly implements this?
hard
A. { message: 'Timeout', extensions: { code: 'TIMEOUT', retry: 0 } }
B. { message: 'Timeout', extensions: { code: 'TIMEOUT', retry: false } }
C. { message: 'Timeout', extensions: { code: 'TIMEOUT', retry: 'no' } }
D. { message: 'Timeout', extensions: { code: 'TIMEOUT', retry: null } }

Solution

  1. Step 1: Check the data types for extensions fields

    The retry flag should be a boolean false, not string or null or number.
  2. Step 2: Validate each option's retry value

    { message: 'Timeout', extensions: { code: 'TIMEOUT', retry: false } } uses boolean false correctly; others use wrong types.
  3. Final Answer:

    { message: 'Timeout', extensions: { code: 'TIMEOUT', retry: false } } -> Option B
  4. Quick Check:

    Boolean retry flag = C [OK]
Hint: Use boolean false for retry, not string or null [OK]
Common Mistakes:
  • Using string 'no' instead of boolean false
  • Using null or 0 for boolean flags
  • Ignoring correct data types in extensions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard