graphql/validation
graphql/validation
模块完成了 GraphQL 结果的验证阶段。你可以从 graphql/validation
模块或根 graphql
模块导入。例如:
¥The graphql/validation
module fulfills the Validation phase of fulfilling a
GraphQL result. You can import either from the graphql/validation
module, or from the root graphql
module. For example:
import { validate } from "graphql/validation" // ES6
var { validate } = require("graphql/validation") // CommonJS
概述
¥Overview
function validate
根据提供的结构验证 AST。¥
function validate
Validates an AST against a provided Schema.var specifiedRules
GraphQL 规范中描述的标准验证规则列表。¥
var specifiedRules
A list of standard validation rules described in the GraphQL specification.
验证
¥Validation
validate
function validate(
schema: GraphQLSchema,
ast: Document,
rules?: any[]
): GraphQLError[]
实现规范的 “验证” 部分。
¥Implements the “Validation” section of the spec.
验证同步运行,返回遇到的错误的数组,如果未遇到错误且文档有效,则返回空数组。
¥Validation runs synchronously, returning an array of encountered errors, or an empty array if no errors were encountered and the document is valid.
可以提供特定验证规则的列表。如果未提供,将使用 GraphQL 规范定义的默认规则列表。
¥A list of specific validation rules may be provided. If not provided, the default list of rules defined by the GraphQL specification will be used.
每个验证规则都是一个返回访问者的函数(请参阅语言/访问者 API)。访问者方法预计会在无效时返回 GraphQLErrors 或 GraphQLErrors 数组。
¥Each validation rules is a function which returns a visitor (see the language/visitor API). Visitor methods are expected to return GraphQLErrors, or Arrays of GraphQLErrors when invalid.
访问者还可以提供 visitSpreadFragments: true
,这将改变访问者的行为以跳过顶层定义的片段,而是在遇到扩展时访问这些片段。
¥Visitors can also supply visitSpreadFragments: true
which will alter the
behavior of the visitor to skip over top level defined fragments, and instead
visit those fragments at every point a spread is encountered.
specifiedRules
let specifiedRules: Array<(context: ValidationContext) => any>
该集包含 GraphQL 规范定义的所有验证规则
¥This set includes all validation rules defined by the GraphQL spec