API 参考:工具函数(Utilities)
API 参考:工具函数(Utilities)
graphql/utilities 模块包含用于 GraphQL 语言和类型对象的常用计算。你可以从 graphql/utilities 模块或是 graphql 根模块引入。如下:
js
import { introspectionQuery } from 'graphql'; // ES6
var { introspectionQuery } = require('graphql'); // CommonJS
概览
内省(Introspection)
introspectionQuery:GraphQL 内省查询,包含足够的信息以重现类型系统。buildClientSchema:通过使用introspectionQuery查询 schema 的结果,生成客户端 schema。
Schema 语言
buildSchema:基于 GraphQL schema language 构建一个 Schema 对象。printSchema:使用标准格式打印 schema。printIntrospectionSchema:使用标准格式打印 schema 的内省特性。buildASTSchema:基于分析后的 AST Schema 构建 schema。typeFromAST:在 GraphQLSchema 的 AST 中查找一个类型引用。astFromValue:基于一个 JavaScript 值生成一个 GraphQL Input Value AST。
访问器(Visitors)
TypeInfo:在 visitor 遍历 AST 时追踪类型和字段定义。
值验证
isValidJSValue:判断一个 JavaScript 值是否是有效的 GraphQL 类型的值。isValidLiteralValue:判断一个 AST 中的字面量值是否是有效的 GraphQL 类型的值。
内省(Introspection)
introspectionQuery
js
var introspectionQuery: string
GraphQL 内省查询,用于查询服务器的内省系统,得到足够的信息以重现服务器类型系统。
buildClientSchema
js
function buildClientSchema(
introspection: IntrospectionQuery
): GraphQLSchema
构建供客户端工具使用的 GraphQLSchema。
给定一次内省查询的运行结果,该函数会创建并返回一个 GraphQLSchema 实例。该实例可用于所有 GraphQL.js 工具,但不能用于执行查询,因为内省结果并不包含“解析器”、“分析”或“序列化”函数,也不包含其他服务器内部机制。
Schema 表示
buildSchema
js
function buildSchema(source: string | Source): GraphQLSchema {
基于 GraphQL schema language 创建一个 GraphQLSchema 对象。schema 将会使用默认解析器。关于 GraphQL schema language 的更多细节,请查看 schema language 文档 或者 schema language 速查表。
printSchema
js
function printSchema(schema: GraphQLSchema): string {
使用 Schema Language 格式打印给定的 schema。
printIntrospectionSchema
js
function printIntrospectionSchema(schema: GraphQLSchema): string {
使用 Schema Language 格式打印内建的内省 schema。
buildASTSchema
js
function buildASTSchema(
ast: SchemaDocument,
queryTypeName: string,
mutationTypeName: ?string
): GraphQLSchema
该函数接收一个 schema 文档的 AST(可由 graphql/language/schema 中的 parseSchemaIntoAST 生成),并构建一个 GraphQLSchema 实例。该实例可用于所有 GraphQL.js 工具,但不能用于执行查询,因为内省结果并不包含“解析器”、“分析”或“序列化”函数,也不包含其他服务器内部机制。
typeFromAST
js
function typeFromAST(
schema: GraphQLSchema,
inputTypeAST: Type
): ?GraphQLType
给定一个出现在 GraphQL AST 和 Schema 中的类型名称,返回其在 schema 中对应的 GraphQLType。
astFromValue
js
function astFromValue(
value: any,
type: GraphQLInputType
): ?Value
基于一个 JavaScript 值生成一个 GraphQL Input Value AST。
如果提供了 GraphQL 类型,则用于在原生值之间消除歧义。
Visitors(访问器)
TypeInfo
js
class TypeInfo {
constructor(schema: GraphQLSchema)
getType(): ?GraphQLOutputType {
getParentType(): ?GraphQLCompositeType {
getInputType(): ?GraphQLInputType {
getFieldDef(): ?GraphQLFieldDefinition {
getDirective(): ?GraphQLDirective {
getArgument(): ?GraphQLArgument {
}
TypeInfo 是一个工具类,在递归分析 GraphQL 文档 AST 的过程中,可以通过调用 enter(node) 和 leave(node),在任意位置追踪指定 GraphQL schema 中的当前字段和类型定义。
值验证
isValidJSValue
js
function isValidJSValue(value: any, type: GraphQLInputType): string[]
给定一个 JavaScript 值和 GraphQL 类型,判断这个值是否能被该类型接受。这个功能在验证运行时查询参数值的时候特别有用。
isValidLiteralValue
js
function isValidLiteralValue(
type: GraphQLInputType,
valueAST: Value
): string[]
验证器的工具函数,用于判断 AST 字面量值是否是一个给定输入类型的有效值。
注意,该函数只验证字面量值,并假设变量值会以正确的类型提供。
帮助我们改进文档
发现翻译问题或内容错误?请告诉我们。
