Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

GraphQL · Function

validate

GraphQL validates a parsed operation against the schema before execution. Invalid documents produce request errors and do not begin field execution.

How it works

In GraphQL.js, validate checks a parsed document against a schema before execution. It returns validation errors; when the document is invalid, execution does not begin and the server reports a request error instead of partial data.

Syntax

validate(schema, documentAST, rules?)

Parameters

NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema used to validate the document.
documentASTDocumentNodeThe parsed GraphQL document to check.
rulesValidationRule[]Optional validation rules; the standard rules are used when omitted.

Returns

A list of validation errors; an empty list indicates that the document is valid.

Key points

  • Validation follows parsing and uses the schema’s type system to assess the operation.
  • It moves many query mistakes into an early, developer-visible checking phase.
  • Validation failures are reported through the response’s errors field.
  • The required checks include field selections and fragment usage, not just field names.
  • Only a successfully validated operation proceeds to execution.

Common mistakes

  • Validation happens before execution, so an invalid document cannot produce partial data.
  • Object-, interface-, and union-valued fields require a selection set.

Example: Validate a parsed GraphQL document

import { buildSchema, parse, validate } from "graphql";

const schema = buildSchema(`
type Query { user: User }
type User { id: ID!, name: String! }
`);

const document = parse(`query { user { id email } }`);
const errors = validate(schema, document);

console.log(errors.map(({ message }) => message));

Parses a query, validates it against a schema, and prints the validation messages.

When to use it

Catch invalid operations early

Check client queries during development or CI before sending them to a server.

Guard server execution

Reject malformed or schema-incompatible requests at the API boundary.

Check query structure

Verify that fragments and nested selections conform to the schema.

Interview questions

QWhat does GraphQL validation do?

The document is parsed first, then validated against the schema. Validation checks that the operation is valid for the API before execution starts.

Follow-up: What does a validation failure return? It produces a request error describing the problem, and field execution does not start.

QWhat happens when a query requests a non-existent field?

A queried field must exist on the relevant type. GraphQL therefore catches references to unknown fields during validation rather than waiting for resolver execution.

Follow-up: How would you prevent a missing-field error? Use the schema or introspection information to select only fields defined on the target type.

QWhen is a selection set required?

Fields returning an object, interface, or union need a selection set describing the fields to retrieve. Scalar and enum fields are leaf values and do not require nested selections.

Follow-up: Why is this useful? It makes the requested response shape explicit and lets the schema validate that shape before any resolver runs.

  • parse
  • GraphQLSchema
  • DocumentNode
  • validation rules
  • execution
  • fragments
  • selection set

Source: Validation. Checked 2026-10-15.