Skip to main content
GraphQL

Basic Query

QueryParseValidateEasy

What you will learn

  • Assert only the fields you asked for โ€” GraphQL returns exactly the query shape
  • Check for partial success: a 200 response can contain both `data` and `errors` arrays
  • Validate against the schema (SDL) โ€” type mismatches slip past ad-hoc assertions

Description

Execute a simple GraphQL query to fetch users.

Test Scenario

const query = `
  query {
    users {
      id
      name
      email
    }
  }
`;
const response = await request.post('/api/graphql', { data: { query } });
const { data } = await response.json();
expect(data.users.length).toBeGreaterThan(0);
Endpoint/api/graphql

Testing Tips

  • โ€ขSpecify exactly which fields you need
  • โ€ขGraphQL only returns requested fields
  • โ€ขNo over-fetching like REST
// Playwright - GraphQL Basic Query
import { test, expect } from '@playwright/test';
test('GraphQL query for users', async ({ request }) => {
const response = await request.post('/api/graphql', {
data: {
query: `
query GetUsers {
users {
id
name
email
}
}
`
}
});
expect(response.ok()).toBeTruthy();
const { data, errors } = await response.json();
expect(errors).toBeUndefined();
expect(data.users).toBeDefined();
expect(data.users.length).toBeGreaterThan(0);
});

Challenge ID: graphql-basic-query

2 frameworks available

API Playground

Send requests and inspect responses

Endpoint: /api/graphql

Press Enter to send