Complete the code to check if the GraphQL response has errors.
if (response.[1]) { console.log('Error found'); }
The errors field in a GraphQL response indicates if there were any errors during the query.
Complete the code to extract error messages from the GraphQL response.
const messages = response.errors.map(error => error.[1]);The message property contains the human-readable error message in each error object.
Fix the error in the code to properly handle GraphQL errors in a try-catch block.
try { const response = await fetchGraphQL(query); if (response.[1]) { throw new Error('GraphQL error'); } } catch (error) { console.error(error); }
Checking response.errors is the correct way to detect GraphQL errors before throwing.
Fill both blanks to log all error messages from the GraphQL response.
if (response.[1]) { response.errors.forEach(error => { console.log(error.[2]); }); }
The errors field holds the error list, and each error's message contains the text.
Fill all three blanks to create a function that returns error messages from a GraphQL response or an empty array if none.
function getErrorMessages(response) {
return response.[1] ? response.errors.map(error => error.[2]) : [3];
}This function checks if errors exist, maps their message properties, or returns an empty array if no errors.