GraphQL
Error Handling
ErrorParseValidateMedium
What you will learn
- GraphQL returns 200 with errors array โ always check body.errors, not HTTP status
- Error classifications (VALIDATION, AUTHENTICATION, INTERNAL) belong in the extensions field โ never leak raw stack
- Test partial success: a query with 5 fields can return data for 3 AND errors for 2. Client must handle both arrays
Description
Handle GraphQL errors in response.
Test Scenario
const response = await request.post('/api/graphql', {
data: { query: '{ invalidField }' }
});
expect(response.status()).toBe(200); // Still 200!
const { errors } = await response.json();
expect(errors).toBeDefined();
expect(errors[0].message).toContain('invalidField');Endpoint
/api/graphqlTesting Tips
- โขGraphQL returns 200 even for errors
- โขCheck errors array in response
- โขPartial data with errors possible
// Playwright API Testing - GraphQLimport { test, expect } from '@playwright/test';test('GraphQL query', async ({ request }) => {const response = await request.post('/api/graphql', {data: {query: `query {users {idname}}`}});expect(response.ok()).toBeTruthy();const { data } = await response.json();expect(data.users).toBeDefined();});
Challenge ID: graphql-error-handling
2 frameworks available
API Playground
Send requests and inspect responses
Endpoint: /api/graphql
Press Enter to send