graphql/validation/validation-rules
Functions
ExecutableDefinitionsRule()
Executable definitions
A GraphQL document is only valid for execution if all definitions are either operation or fragment definitions.
See https://spec.graphql.org/draft/#sec-Executable-Definitions
Signature:
ExecutableDefinitionsRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { ExecutableDefinitionsRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
type Extra { field: String }
`);
const invalidErrors = validate(schema, invalidDocument, [ExecutableDefinitionsRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ name }
`);
const validErrors = validate(schema, validDocument, [ExecutableDefinitionsRule]);
// validErrors.length: 0FieldsOnCorrectTypeRule()
Fields on correct type
A GraphQL document is only valid if all fields selected are defined by the parent type, or are an allowed meta field such as __typename.
See https://spec.graphql.org/draft/#sec-Field-Selections
Signature:
FieldsOnCorrectTypeRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { FieldsOnCorrectTypeRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ missing }
`);
const invalidErrors = validate(schema, invalidDocument, [FieldsOnCorrectTypeRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ name }
`);
const validErrors = validate(schema, validDocument, [FieldsOnCorrectTypeRule]);
// validErrors.length: 0FragmentsOnCompositeTypesRule()
Fragments on composite type
Fragments use a type condition to determine if they apply, since fragments can only be spread into a composite type (object, interface, or union), the type condition must also be a composite type.
See https://spec.graphql.org/draft/#sec-Fragments-On-Composite-Types
Signature:
FragmentsOnCompositeTypesRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { FragmentsOnCompositeTypesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
fragment Bad on String { length }
`);
const invalidErrors = validate(schema, invalidDocument, [FragmentsOnCompositeTypesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
fragment Good on Human { name }
`);
const validErrors = validate(schema, validDocument, [FragmentsOnCompositeTypesRule]);
// validErrors.length: 0KnownArgumentNamesRule()
Known argument names
A GraphQL field is only valid if all supplied arguments are defined by that field.
See https://spec.graphql.org/draft/#sec-Argument-Names See https://spec.graphql.org/draft/#sec-Directives-Are-In-Valid-Locations
Signature:
KnownArgumentNamesRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { KnownArgumentNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ human(unknown: "1") { name } }
`);
const invalidErrors = validate(schema, invalidDocument, [KnownArgumentNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ human(id: "1") { name } }
`);
const validErrors = validate(schema, validDocument, [KnownArgumentNamesRule]);
// validErrors.length: 0KnownDirectivesRule()
Known directives
A GraphQL document is only valid if all @directives are known by the
schema and legally positioned.
See https://spec.graphql.org/draft/#sec-Directives-Are-Defined
Signature:
KnownDirectivesRule(context: ValidationContext | SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { KnownDirectivesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ name @unknown }
`);
const invalidErrors = validate(schema, invalidDocument, [KnownDirectivesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ name @include(if: true) }
`);
const validErrors = validate(schema, validDocument, [KnownDirectivesRule]);
// validErrors.length: 0KnownFragmentNamesRule()
Known fragment names
A GraphQL document is only valid if all ...Fragment fragment spreads refer
to fragments defined in the same document.
See https://spec.graphql.org/draft/#sec-Fragment-spread-target-defined
Signature:
KnownFragmentNamesRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { KnownFragmentNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ ...Missing }
`);
const invalidErrors = validate(schema, invalidDocument, [KnownFragmentNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
fragment NameFields on Query { name } query { ...NameFields }
`);
const validErrors = validate(schema, validDocument, [KnownFragmentNamesRule]);
// validErrors.length: 0KnownTypeNamesRule()
Known type names
A GraphQL document is only valid if referenced types (specifically variable definitions and fragment conditions) are defined by the type schema.
See https://spec.graphql.org/draft/#sec-Fragment-Spread-Type-Existence
Signature:
KnownTypeNamesRule(context: ValidationContext | SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { KnownTypeNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
fragment Bad on Missing { name }
`);
const invalidErrors = validate(schema, invalidDocument, [KnownTypeNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
fragment Good on Human { name }
`);
const validErrors = validate(schema, validDocument, [KnownTypeNamesRule]);
// validErrors.length: 0LoneAnonymousOperationRule()
Lone anonymous operation
A GraphQL document is only valid if when it contains an anonymous operation (the query short-hand) that it contains only that one operation definition.
See https://spec.graphql.org/draft/#sec-Lone-Anonymous-Operation
Signature:
LoneAnonymousOperationRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { LoneAnonymousOperationRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
query { name } query Other { name }
`);
const invalidErrors = validate(schema, invalidDocument, [LoneAnonymousOperationRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ name }
`);
const validErrors = validate(schema, validDocument, [LoneAnonymousOperationRule]);
// validErrors.length: 0LoneSchemaDefinitionRule()
Lone Schema definition
A GraphQL document is only valid if it contains only one schema definition.
Signature:
LoneSchemaDefinitionRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { LoneSchemaDefinitionRule } from 'graphql/validation';
const invalidDocument = parse(`
schema { query: Query } schema { query: Query } type Query { name: String }
`);
// invalidDocument fails LoneSchemaDefinitionRule.
const validDocument = parse(`
schema { query: Query } type Query { name: String }
`);
// validDocument passes LoneSchemaDefinitionRule.MaxIntrospectionDepthRule()
Implements the max introspection depth validation rule.
Signature:
MaxIntrospectionDepthRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { MaxIntrospectionDepthRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ __schema { types { fields { type { fields { type { fields { name } } } } } } } }
`);
const invalidErrors = validate(schema, invalidDocument, [MaxIntrospectionDepthRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ __schema { queryType { name } } }
`);
const validErrors = validate(schema, validDocument, [MaxIntrospectionDepthRule]);
// validErrors.length: 0NoFragmentCyclesRule()
No fragment cycles
The graph of fragment spreads must not form any cycles including spreading itself. Otherwise an operation could infinitely spread or infinitely execute on cycles in the underlying data.
See https://spec.graphql.org/draft/#sec-Fragment-spreads-must-not-form-cycles
Signature:
NoFragmentCyclesRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { NoFragmentCyclesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
fragment A on Human { ...B } fragment B on Human { ...A } query { human(id: "1") { ...A } }
`);
const invalidErrors = validate(schema, invalidDocument, [NoFragmentCyclesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
fragment A on Human { name } query { human(id: "1") { ...A } }
`);
const validErrors = validate(schema, validDocument, [NoFragmentCyclesRule]);
// validErrors.length: 0NoUndefinedVariablesRule()
No undefined variables
A GraphQL operation is only valid if all variables encountered, both directly and via fragment spreads, are defined by that operation.
See https://spec.graphql.org/draft/#sec-All-Variable-Uses-Defined
Signature:
NoUndefinedVariablesRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { NoUndefinedVariablesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
query ($id: ID!) { human(id: $missing) { name } }
`);
const invalidErrors = validate(schema, invalidDocument, [NoUndefinedVariablesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
query ($id: ID!) { human(id: $id) { name } }
`);
const validErrors = validate(schema, validDocument, [NoUndefinedVariablesRule]);
// validErrors.length: 0NoUnusedFragmentsRule()
No unused fragments
A GraphQL document is only valid if all fragment definitions are spread within operations, or spread within other fragments spread within operations.
See https://spec.graphql.org/draft/#sec-Fragments-Must-Be-Used
Signature:
NoUnusedFragmentsRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { NoUnusedFragmentsRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
fragment Unused on Human { name } query { name }
`);
const invalidErrors = validate(schema, invalidDocument, [NoUnusedFragmentsRule]);
// invalidErrors.length: 1
const validDocument = parse(`
fragment Used on Human { name } query { human(id: "1") { ...Used } }
`);
const validErrors = validate(schema, validDocument, [NoUnusedFragmentsRule]);
// validErrors.length: 0NoUnusedVariablesRule()
No unused variables
A GraphQL operation is only valid if all variables defined by an operation are used, either directly or within a spread fragment.
See https://spec.graphql.org/draft/#sec-All-Variables-Used
Signature:
NoUnusedVariablesRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { NoUnusedVariablesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
query ($id: ID!) { name }
`);
const invalidErrors = validate(schema, invalidDocument, [NoUnusedVariablesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
query ($id: ID!) { human(id: $id) { name } }
`);
const validErrors = validate(schema, validDocument, [NoUnusedVariablesRule]);
// validErrors.length: 0OverlappingFieldsCanBeMergedRule()
Overlapping fields can be merged
A selection set is only valid if all fields (including spreading any fragments) either correspond to distinct response names or can be merged without ambiguity.
See https://spec.graphql.org/draft/#sec-Field-Selection-Merging
Signature:
OverlappingFieldsCanBeMergedRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { OverlappingFieldsCanBeMergedRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ dog { value: barkVolume value: name } }
`);
const invalidErrors = validate(schema, invalidDocument, [OverlappingFieldsCanBeMergedRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ dog { barkVolume name } }
`);
const validErrors = validate(schema, validDocument, [OverlappingFieldsCanBeMergedRule]);
// validErrors.length: 0PossibleFragmentSpreadsRule()
Possible fragment spread
A fragment spread is only valid if the type condition could ever possibly be true: if there is a non-empty intersection of the possible parent types, and possible types which pass the type condition.
Signature:
PossibleFragmentSpreadsRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { PossibleFragmentSpreadsRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ dog { ... on Cat { meowVolume } } }
`);
const invalidErrors = validate(schema, invalidDocument, [PossibleFragmentSpreadsRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ dog { ... on Dog { barkVolume } } }
`);
const validErrors = validate(schema, validDocument, [PossibleFragmentSpreadsRule]);
// validErrors.length: 0PossibleTypeExtensionsRule()
Possible type extension
A type extension is only valid if the type is defined and has the same kind.
Signature:
PossibleTypeExtensionsRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { PossibleTypeExtensionsRule } from 'graphql/validation';
const invalidDocument = parse(`
extend type Missing { name: String } type Query { name: String }
`);
// invalidDocument fails PossibleTypeExtensionsRule.
const validDocument = parse(`
type Query { name: String } extend type Query { other: String }
`);
// validDocument passes PossibleTypeExtensionsRule.ProvidedRequiredArgumentsRule()
Provided required arguments
A field or directive is only valid if all required (non-null without a default value) field arguments have been provided.
Signature:
ProvidedRequiredArgumentsRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { ProvidedRequiredArgumentsRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ human { name } }
`);
const invalidErrors = validate(schema, invalidDocument, [ProvidedRequiredArgumentsRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ human(id: "1") { name } }
`);
const validErrors = validate(schema, validDocument, [ProvidedRequiredArgumentsRule]);
// validErrors.length: 0ScalarLeafsRule()
Scalar leafs
A GraphQL document is valid only if all leaf fields (fields without sub selections) are of scalar or enum types.
Signature:
ScalarLeafsRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { ScalarLeafsRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ name { length } }
`);
const invalidErrors = validate(schema, invalidDocument, [ScalarLeafsRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ name }
`);
const validErrors = validate(schema, validDocument, [ScalarLeafsRule]);
// validErrors.length: 0SingleFieldSubscriptionsRule()
Subscriptions must only include a non-introspection field.
A GraphQL subscription is valid only if it contains a single root field and that root field is not an introspection field.
See https://spec.graphql.org/draft/#sec-Single-root-field
Signature:
SingleFieldSubscriptionsRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { SingleFieldSubscriptionsRule } from 'graphql/validation';
const schema = buildSchema(`type Query { name: String }
type Subscription { a: String, b: String }`);
const invalidDocument = parse(`
subscription { a b }
`);
const invalidErrors = validate(schema, invalidDocument, [SingleFieldSubscriptionsRule]);
// invalidErrors.length: 1
const validDocument = parse(`
subscription { a }
`);
const validErrors = validate(schema, validDocument, [SingleFieldSubscriptionsRule]);
// validErrors.length: 0UniqueArgumentDefinitionNamesRule()
Unique argument definition names
A GraphQL Object or Interface type is only valid if all its fields have uniquely named arguments. A GraphQL Directive is only valid if all its arguments are uniquely named.
Signature:
UniqueArgumentDefinitionNamesRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { UniqueArgumentDefinitionNamesRule } from 'graphql/validation';
const invalidDocument = parse(`
type Query { field(arg: String, arg: Int): String }
`);
// invalidDocument fails UniqueArgumentDefinitionNamesRule.
const validDocument = parse(`
type Query { field(arg: String): String }
`);
// validDocument passes UniqueArgumentDefinitionNamesRule.UniqueArgumentNamesRule()
Unique argument names
A GraphQL field or directive is only valid if all supplied arguments are uniquely named.
See https://spec.graphql.org/draft/#sec-Argument-Names
Signature:
UniqueArgumentNamesRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { UniqueArgumentNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ human(id: "1", id: "2") { name } }
`);
const invalidErrors = validate(schema, invalidDocument, [UniqueArgumentNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ human(id: "1") { name } }
`);
const validErrors = validate(schema, validDocument, [UniqueArgumentNamesRule]);
// validErrors.length: 0UniqueDirectiveNamesRule()
Unique directive names
A GraphQL document is only valid if all defined directives have unique names.
Signature:
UniqueDirectiveNamesRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { UniqueDirectiveNamesRule } from 'graphql/validation';
const invalidDocument = parse(`
directive @tag on FIELD directive @tag on QUERY type Query { name: String }
`);
// invalidDocument fails UniqueDirectiveNamesRule.
const validDocument = parse(`
directive @tag on FIELD type Query { name: String }
`);
// validDocument passes UniqueDirectiveNamesRule.UniqueDirectivesPerLocationRule()
Unique directive names per location
A GraphQL document is only valid if all non-repeatable directives at a given location are uniquely named.
See https://spec.graphql.org/draft/#sec-Directives-Are-Unique-Per-Location
Signature:
UniqueDirectivesPerLocationRule(context: ValidationContext | SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { UniqueDirectivesPerLocationRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ name @include(if: true) @include(if: false) }
`);
const invalidErrors = validate(schema, invalidDocument, [UniqueDirectivesPerLocationRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ name @include(if: true) }
`);
const validErrors = validate(schema, validDocument, [UniqueDirectivesPerLocationRule]);
// validErrors.length: 0UniqueEnumValueNamesRule()
Unique enum value names
A GraphQL enum type is only valid if all its values are uniquely named.
Signature:
UniqueEnumValueNamesRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { UniqueEnumValueNamesRule } from 'graphql/validation';
const invalidDocument = parse(`
enum Status { ACTIVE ACTIVE } type Query { status: Status }
`);
// invalidDocument fails UniqueEnumValueNamesRule.
const validDocument = parse(`
enum Status { ACTIVE INACTIVE } type Query { status: Status }
`);
// validDocument passes UniqueEnumValueNamesRule.UniqueFieldDefinitionNamesRule()
Unique field definition names
A GraphQL complex type is only valid if all its fields are uniquely named.
Signature:
UniqueFieldDefinitionNamesRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { UniqueFieldDefinitionNamesRule } from 'graphql/validation';
const invalidDocument = parse(`
type Query { name: String name: String }
`);
// invalidDocument fails UniqueFieldDefinitionNamesRule.
const validDocument = parse(`
type Query { name: String other: String }
`);
// validDocument passes UniqueFieldDefinitionNamesRule.UniqueFragmentNamesRule()
Unique fragment names
A GraphQL document is only valid if all defined fragments have unique names.
See https://spec.graphql.org/draft/#sec-Fragment-Name-Uniqueness
Signature:
UniqueFragmentNamesRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { UniqueFragmentNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
fragment A on Human { name } fragment A on Human { pet { name } } query { human(id: "1") { ...A } }
`);
const invalidErrors = validate(schema, invalidDocument, [UniqueFragmentNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
fragment A on Human { name } query { human(id: "1") { ...A } }
`);
const validErrors = validate(schema, validDocument, [UniqueFragmentNamesRule]);
// validErrors.length: 0UniqueInputFieldNamesRule()
Unique input field names
A GraphQL input object value is only valid if all supplied fields are uniquely named.
See https://spec.graphql.org/draft/#sec-Input-Object-Field-Uniqueness
Signature:
UniqueInputFieldNamesRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { UniqueInputFieldNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ search(filter: { name: "a", name: "b" }) }
`);
const invalidErrors = validate(schema, invalidDocument, [UniqueInputFieldNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ search(filter: { name: "a" }) }
`);
const validErrors = validate(schema, validDocument, [UniqueInputFieldNamesRule]);
// validErrors.length: 0UniqueOperationNamesRule()
Unique operation names
A GraphQL document is only valid if all defined operations have unique names.
See https://spec.graphql.org/draft/#sec-Operation-Name-Uniqueness
Signature:
UniqueOperationNamesRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { UniqueOperationNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
query Same { name } query Same { dog { name } }
`);
const invalidErrors = validate(schema, invalidDocument, [UniqueOperationNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
query One { name } query Two { dog { name } }
`);
const validErrors = validate(schema, validDocument, [UniqueOperationNamesRule]);
// validErrors.length: 0UniqueOperationTypesRule()
Unique operation types
A GraphQL document is only valid if it has only one type per operation.
Signature:
UniqueOperationTypesRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { UniqueOperationTypesRule } from 'graphql/validation';
const invalidDocument = parse(`
schema { query: Query query: Other } type Query { name: String } type Other { name: String }
`);
// invalidDocument fails UniqueOperationTypesRule.
const validDocument = parse(`
schema { query: Query } type Query { name: String }
`);
// validDocument passes UniqueOperationTypesRule.UniqueTypeNamesRule()
Unique type names
A GraphQL document is only valid if all defined types have unique names.
Signature:
UniqueTypeNamesRule(context: SDLValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | SDLValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { parse } from 'graphql/language';
import { UniqueTypeNamesRule } from 'graphql/validation';
const invalidDocument = parse(`
type Query { name: String } type Query { other: String }
`);
// invalidDocument fails UniqueTypeNamesRule.
const validDocument = parse(`
type Query { name: String } type Other { name: String }
`);
// validDocument passes UniqueTypeNamesRule.UniqueVariableNamesRule()
Unique variable names
A GraphQL operation is only valid if all its variables are uniquely named.
Signature:
UniqueVariableNamesRule(context: ASTValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ASTValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { UniqueVariableNamesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
query ($id: ID, $id: ID) { human(id: $id) { name } }
`);
const invalidErrors = validate(schema, invalidDocument, [UniqueVariableNamesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
query ($id: ID!) { human(id: $id) { name } }
`);
const validErrors = validate(schema, validDocument, [UniqueVariableNamesRule]);
// validErrors.length: 0ValuesOfCorrectTypeRule()
Value literals of correct type
A GraphQL document is only valid if all value literals are of the type expected at their position.
See https://spec.graphql.org/draft/#sec-Values-of-Correct-Type
Signature:
ValuesOfCorrectTypeRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { ValuesOfCorrectTypeRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
{ count(limit: "many") }
`);
const invalidErrors = validate(schema, invalidDocument, [ValuesOfCorrectTypeRule]);
// invalidErrors.length: 1
const validDocument = parse(`
{ count(limit: 1) }
`);
const validErrors = validate(schema, validDocument, [ValuesOfCorrectTypeRule]);
// validErrors.length: 0VariablesAreInputTypesRule()
Variables are input types
A GraphQL operation is only valid if all the variables it defines are of input types (scalar, enum, or input object).
See https://spec.graphql.org/draft/#sec-Variables-Are-Input-Types
Signature:
VariablesAreInputTypesRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { VariablesAreInputTypesRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
query ($dog: Dog) { name }
`);
const invalidErrors = validate(schema, invalidDocument, [VariablesAreInputTypesRule]);
// invalidErrors.length: 1
const validDocument = parse(`
query ($id: ID!) { human(id: $id) { name } }
`);
const validErrors = validate(schema, validDocument, [VariablesAreInputTypesRule]);
// validErrors.length: 0VariablesInAllowedPositionRule()
Variables in allowed position
Variable usages must be compatible with the arguments they are passed to.
See https://spec.graphql.org/draft/#sec-All-Variable-Usages-are-Allowed
Signature:
VariablesInAllowedPositionRule(context: ValidationContext): ASTVisitor
Arguments
| Name | Type | Description |
|---|---|---|
| context | ValidationContext | The validation context used while checking the document. |
Returns
| Type | Description |
|---|---|
ASTVisitor | A visitor that reports validation errors for this rule. |
Example
import { buildSchema, parse, validate } from 'graphql';
import { VariablesInAllowedPositionRule } from 'graphql/validation';
const schema = buildSchema(`interface Pet { name: String }
type Dog implements Pet { name: String, barkVolume: Int }
type Cat implements Pet { name: String, meowVolume: Int }
type Human { name: String, pet: Pet, dog: Dog, cat: Cat }
input Filter { name: String, age: Int }
type Query {
name: String
oldName: String @deprecated(reason: "Use name")
dog: Dog
cat: Cat
human(id: ID!): Human
pets: [Pet]
search(filter: Filter): String
count(limit: Int): Int
}`);
const invalidDocument = parse(`
query ($id: String) { human(id: $id) { name } }
`);
const invalidErrors = validate(schema, invalidDocument, [VariablesInAllowedPositionRule]);
// invalidErrors.length: 1
const validDocument = parse(`
query ($id: ID!) { human(id: $id) { name } }
`);
const validErrors = validate(schema, validDocument, [VariablesInAllowedPositionRule]);
// validErrors.length: 0Constants
recommendedRules
Technically these aren’t part of the spec but they are strongly encouraged validation rules.
readonly (context: ASTValidationContext): ASTVisitor[]
specifiedRules
This set includes all validation rules defined by the GraphQL spec.
The order of the rules in this list has been adjusted to lead to the most clear output when encountering multiple validation errors.
ReadonlyArray<ValidationRule>