GraphQL LogoGraphQL 中文网

graphql/验证

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)#

英:Overview

验证(Validation)#

英:Validation

validate(validate)#

function validate(
schema: GraphQLSchema,
ast: Document,
rules?: Array<any>
): Array<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(specifiedRules)#

var specifiedRules: Array<(context: ValidationContext): any>

该集包含 GraphQL 规范定义的所有验证规则

英:This set includes all validation rules defined by the GraphQL spec

GraphQL 中文网 - 粤ICP备13048890号