Skip to main content
GraphQL

Nested Query

QueryParseValidateMedium

What you will learn

  • N+1 query problem: one outer query triggering N inner queries โ€” detect via resolver tracing
  • Query depth limits are your production safety net โ€” test that deep queries are rejected
  • Circular relations require explicit termination โ€” assert that Aโ†’Bโ†’A terminates, not stack-overflows

Description

Fetch related data in a single query (user with orders).

Test Scenario

const query = `
  query {
    user(id: "1") {
      name
      orders {
        id
        total
        items {
          product {
            name
            price
          }
        }
      }
    }
  }
`;
Endpoint/api/graphql

Testing Tips

  • โ€ขTraverse relationships in one query
  • โ€ขAvoid N+1 query problem
  • โ€ขUse fragments for reusability
// 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-nested-query

2 frameworks available

API Playground

Send requests and inspect responses

Endpoint: /api/graphql

Press Enter to send