Union Errors
Introduction to Union Types in GraphQL GraphQL's union types allow fields to return one of several different types of error responses. This is particularly useful when a query might return a successful result or various types of errors, such as validation or generic errors.
Key Components of the Union Type Implementation
- Success Type: Represents the normal, successful response. Example: Organization type containing the organization details.
- Error Types: ValidationError: Includes details about validation issues, such as missing or invalid fields. GenericError: Handles any generic errors that don’t fall under validation, such as internal server issues.
Usage Examples in Client-Side Applications To handle union types in client-side applications, you can leverage the __typename field, which helps identify the exact type of the response.
Handling Success and Error on the Client:
The migration to union types in your GraphQL responses significantly enhances the API’s usability, clarity, and error management. By leveraging this approach, you can provide a more robust and predictable contract to API consumers, ensuring that both successful and error states are handled consistently.
Generic Error
Validation Error
- When an API request fails, the API Gateway can return a GenericError to provide essential information about the failure. This type of error is used for system-level issues such as server malfunctions, internal failures, or when the request cannot be processed due to infrastructure-related problems.
- The GenericError is designed to give the client a basic but meaningful description of the error, so they can identify the problem and take appropriate action..
- When the Generic Error filter is configured, the API Gateway examines the incoming message and attempts to infer the type of message to be returned.
- Query body’s information
- typename - Indicates the type of error, which in this case will be GenericError. It helps clients distinguish between different error types, such as GenericError vs. ValidationError.
- errorMessage - A brief description of the issue that occurred. This message helps explain the error, though it may not provide specific technical details..
- errorCode - displays the error code.
- correlationId - An id used to correlate logs and metrics across services and processes.
- For example lets hit this query in Profile API , For an incoming JSON message, the API Gateway sends an appropriate JSON response
Request
Response
- Validation Error occurs when a request is malformed or the request failed our validation rules.
- It occurs when the required parameters are missing (either in body or in query string).
- Query body’s information
- typename - It shows the type of error that has occurred, either generic or validation.
- errorMessage - elaborates the requirement for the input values.
- errorCode - displays the error code.
- correlationId - an id used to correlate logs and metrics across services and processes -
- errorDetails - Displays the detailed information about the error.
- For Example For example lets hit this query in Profile API.
Request
Response
All GraphQL queries must be embedded with the below query