APIs & Services
...
Getting Started
Error Handling
Union Errors
8min
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 const query = gql query getorganization($id id !) { getorganization(id $id) { on organization { id name website } on validationerror { message field } on genericerror { message errorcode } } }; apolloclient query({ query, variables { id "1" } }) then(response => { const result = response data getorganization; if (result typename === 'organization') { console log('organization details ', result); } else if (result typename === 'validationerror') { console log('validation error ', result message); } else if (result typename === 'genericerror') { console log('generic error ', result message); } }); 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 error structure generic error on genericerror { typename correlationid errorcode errormessage } validation error on validationerror { typename errormessage errorcode correlationid errordetails { code message path } } generic errors 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 query myquery { organizationbulkidsearch(filter {ids ""}) { on organizationpagedresult { typename items { id } } on genericerror { typename correlationid errormessage errorcode } on validationerror { typename correlationid errorcode errormessage errordetails { code path message } } } } response { "data" { "organizationbulkidsearch" { " typename" "genericerror", "correlationid" "", "errormessage" "search phase execution exception \[query shard exception] reason failed to create query ids can't be empty", "errorcode" "500" } } } validation error 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 query personkeywordsearch inline fragments { personkeywordsearch (filter {searchkeyword ""}, pageinfo {first 30} ) { on personpagedresult { typename items { typename name id firstname middlename lastname } pageinforesponse { totalcount pageinfo { startcursor endcursor hasnextpage } } } on genericerror { typename correlationid errorcode errormessage } on validationerror { typename errormessage errorcode correlationid errordetails { code message path } } } } response { "data" { "personkeywordsearch" { " typename" "validationerror", "errormessage" "minimum two characters needed to perform keyword search", "errorcode" "400", "correlationid" "", "errordetails" null } } } important note all graphql queries must be embedded with the below query on genericerror { typename correlationid errorcode errormessage } on validationerror { typename errormessage errorcode correlationid errordetails { code message path } }