Skip to main content
GraphQL

Directives (@include, @skip)

QueryParseValidateHard

What you will learn

  • @include and @skip conditionally request fields โ€” test that the unselected fields really don't execute server-side
  • @deprecated on a field surfaces in introspection โ€” clients can phase out usage gracefully. Test presence in schema
  • Custom directives (@auth, @rateLimit) must apply BEFORE resolver execution โ€” a request past the directive gate already bypassed the check

Description

Conditionally include or skip fields based on variables.

Test Scenario

const query = `
  query GetUser($withEmail: Boolean!) {
    user(id: "1") {
      name
      email @include(if: $withEmail)
    }
  }
`;
const variables = { withEmail: false };
// Response will not include email field
Endpoint/api/graphql

Testing Tips

  • โ€ข@include(if: $bool) includes field
  • โ€ข@skip(if: $bool) skips field
  • โ€ขDynamic query structure
// 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-directives

2 frameworks available

API Playground

Send requests and inspect responses

Endpoint: /api/graphql

Press Enter to send