Skip to main content
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/graphql

Testing Tips

  • โ€ขGraphQL returns 200 even for errors
  • โ€ขCheck errors array in response
  • โ€ขPartial data with errors possible
// Playwright API Testing - GraphQL
import { test, expect } from '@playwright/test';
test('GraphQL query', async ({ request }) => {
const response = await request.post('/api/graphql', {
data: {
query: `
query {
users {
id
name
email
}
}
`
}
});
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