graphql/validation
The graphql/validation entry point validates parsed GraphQL documents against
a schema. It exports the default rule lists, the validation context, and every
specified and GraphQL.js-provided validation rule.
import { validate, specifiedRules } from 'graphql/validation';The same exports are also available from the root graphql entry point.
Contents
- Core Validation Exports
- Executable Validation Rule Exports
- Incremental Delivery Rule Exports
- SDL Validation Rule Exports
- Optional Rule Exports
- Custom Rules
const errors = validate(schema, document, specifiedRules, {
maxErrors: 25,
hideSuggestions: true,
});Descriptions are no longer visited during validation because descriptions do not affect GraphQL validity.
Core Validation Exports
| Export | Kind | Description |
|---|---|---|
validate() Changed in v17 | Function | Synchronously validates a document and returns an array of GraphQLError values. In v17, pass options as the fourth argument; the v16 custom TypeInfo argument was removed. |
ValidationContext Changed in v17 | Class | Context passed to executable validation rules. |
ValidationRule | Type | Function type for executable validation rules. |
specifiedRules Changed in v17 | Array | All executable validation rules required by the GraphQL specification and supported proposals. |
recommendedRules | Array | GraphQL.js recommended rules that are not part of the core specified rule list, including MaxIntrospectionDepthRule. |
const errors = validate(schema, parse(source));
if (errors.length > 0) {
return { errors };
}Executable Validation Rule Exports
| Export | Description |
|---|---|
ExecutableDefinitionsRule | Executable documents may contain only operations and fragments. |
FieldsOnCorrectTypeRule | Selected fields must exist on the parent type. |
FragmentsOnCompositeTypesRule | Fragments may condition only on composite types. |
KnownArgumentNamesRule | Field and directive arguments must be defined. |
KnownDirectivesRule | Directives must be defined and used in valid locations. |
KnownFragmentNamesRule | Fragment spreads must refer to defined fragments. |
KnownOperationTypesRule New in v17 | Operation types must be supported by the schema. |
KnownTypeNamesRule | Referenced types must exist in the schema. |
LoneAnonymousOperationRule | Anonymous operations must be the only operation in a document. |
NoFragmentCyclesRule | Fragments must not form cycles. |
NoUndefinedVariablesRule | Used variables must be defined by the operation or fragment arguments. |
NoUnusedFragmentsRule | Fragment definitions must be used. |
NoUnusedVariablesRule | Variable definitions must be used. |
OverlappingFieldsCanBeMergedRule | Fields with the same response name must be merge-compatible. |
PossibleFragmentSpreadsRule | Fragment spreads must be possible for the parent and condition types. |
ProvidedRequiredArgumentsRule | Required arguments must be provided. |
ScalarLeafsRule | Leaf fields must not have selections and composite fields must have selections. |
SingleFieldSubscriptionsRule | Subscriptions must select exactly one root field. |
UniqueArgumentNamesRule | Arguments at one location must be unique. |
UniqueDirectivesPerLocationRule | Non-repeatable directives must not repeat at one location. |
UniqueFragmentNamesRule | Fragment names must be unique. |
UniqueInputFieldNamesRule | Input object field names must be unique. |
UniqueOperationNamesRule | Operation names must be unique. |
UniqueVariableNamesRule | Variable names in an operation or fragment must be unique. |
ValuesOfCorrectTypeRule | Literal values must be valid for their expected input type. |
VariablesAreInputTypesRule | Variables must use input types. |
VariablesInAllowedPositionRule | Variable usages must be valid for their location. |
Incremental Delivery Rule Exports
These rules are new in v17 and validate @defer / @stream usage.
| Export | Description |
|---|---|
DeferStreamDirectiveLabelRule New in v17 | @defer and @stream labels must be unique where required. |
DeferStreamDirectiveOnRootFieldRule New in v17 | @defer and @stream cannot be used on invalid root fields. |
DeferStreamDirectiveOnValidOperationsRule New in v17 | @defer and @stream must appear only on operations that can support them. |
StreamDirectiveOnListFieldRule New in v17 | @stream must be used on list fields. |
SDL Validation Rule Exports
| Export | Description |
|---|---|
LoneSchemaDefinitionRule | A schema document may define the schema only once. |
UniqueOperationTypesRule | Each operation root type may be defined only once. |
UniqueTypeNamesRule | Type names must be unique. |
UniqueEnumValueNamesRule | Enum value names must be unique within an enum. |
UniqueFieldDefinitionNamesRule | Field names must be unique within object and interface types. |
UniqueArgumentDefinitionNamesRule | Argument names must be unique within a field or directive definition. |
UniqueDirectiveNamesRule | Directive definition names must be unique. |
PossibleTypeExtensionsRule | Type extensions must extend existing or well-formed types of the right kind. |
Optional Rule Exports
| Export | Description |
|---|---|
MaxIntrospectionDepthRule | Limits nested introspection depth. Included in recommendedRules. |
NoDeprecatedCustomRule | Reports usage of deprecated fields, arguments, input fields, and enum values. |
NoSchemaIntrospectionCustomRule | Disallows schema introspection fields. |
Custom Rules
Custom rules are functions that receive a ValidationContext and return an AST
visitor.
import { GraphQLError, specifiedRules, validate } from 'graphql';
function NoViewerFieldRule(context) {
return {
Field(node) {
if (node.name.value === 'viewer') {
context.reportError(
new GraphQLError('The viewer field is disabled.', { nodes: node }),
);
}
},
};
}
const errors = validate(schema, document, [
...specifiedRules,
NoViewerFieldRule,
]);