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 fieldEndpoint
/api/graphqlTesting Tips
- โข@include(if: $bool) includes field
- โข@skip(if: $bool) skips field
- โขDynamic query structure
// 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-directives
2 frameworks available
API Playground
Send requests and inspect responses
Endpoint: /api/graphql
Press Enter to send