In GraphQL, updates are called ‘mutations’. Mutations and queries may return a union type - a union type in GraphQL is a value that can only be one of a fixed set of types. We use union types to communicate validation errors back if we are unable to execute the request.
This union type is expressed in the SDL like so:
union CreateCaseResult = ValidationErrors | CaseInformation
Where CaseInformation
is the type returned if the operation was executed successfully, and ValidationErrors
is the type returned if the request failed (due to issues validating the input to the mutation).
When executing a mutation or query which uses this kind of union type, you need to write your query expecting either value in response e.g.
mutation ($input: CreateCaseForIndividualsInput!)
{
createCaseForIndividuals(input: $input)
{
... on CaseInformation
{
caseId
caseStatus
}
... on ValidationErrors
{
errors
{
message
__typename
fieldPath
}
}
}
}
When one or more validation errors occur, you will find the list of errors returned as an array in json, with each error’s type returned in __typename
and a human readable error message in the message
field. The field fieldPath
is an array of strings, which identifies the element within the input
parameter where the validation failure occurred, this will include the zero-based index/position with an array of values e.g. ["individuals", "1", "emailAddress"]
would be the field path to the email address of the second individual in the list of individuals, to indicate a validation issue with the email address.