⬡ GraphQL
Beginner
How does error handling work in GraphQL?
Answer
GraphQL has a unique error model. When a resolver throws an error, GraphQL catches it and adds it to the errors array in the response — the rest of the query continues executing (partial results). If the errored field is non-null (!), GraphQL propagates the null upward to the nearest nullable parent. The response can have both data and errors simultaneously. Error objects contain: message, locations (query position), path (field path), and extensions (custom data). In Apollo Server, throw new GraphQLError('message', { extensions: { code: 'NOT_FOUND' } }). Common error codes: UNAUTHENTICATED, FORBIDDEN, NOT_FOUND, BAD_USER_INPUT, INTERNAL_SERVER_ERROR. Never expose raw database errors to clients — mask internal errors in production.