GraphQL.JS 文档
graphql/实用工具

graphql/utilities

graphql/utilities 模块包含与 GraphQL 语言和类型对象一起使用的常见有用计算。你可以从 graphql/utilities 模块或根 graphql 模块导入。例如:

¥The graphql/utilities module contains common useful computations to use with the GraphQL language and type objects. You can import either from the graphql/utilities module, or from the root graphql module. For example:

import { introspectionQuery } from "graphql" // ES6
var { introspectionQuery } = require("graphql") // CommonJS

概述

¥Overview

内省

¥Introspection

结构语言

¥Schema Language

访客

¥Visitors

值验证

¥Value Validation

内省

¥Introspection

introspectionQuery

var introspectionQuery: string

一个 GraphQL 查询,用于查询服务器的自省系统以获取足够的信息来重现该服务器的类型系统。

¥A GraphQL query that queries a server’s introspection system for enough information to reproduce that server’s type system.

buildClientSchema

function buildClientSchema(
  introspection: IntrospectionQuery
): GraphQLSchema

构建 GraphQLSchema 供客户端工具使用。

¥Build a GraphQLSchema for use by client tools.

给定客户端运行内省查询的结果,创建并返回一个 GraphQLSchema 实例,该实例随后可与所有 GraphQL.js 工具一起使用,但不能用于执行查询,因为内省不代表 “解析器”、“parse” 或 “serialize” 函数 或任何其他服务器内部机制。

¥Given the result of a client running the introspection query, creates and returns a GraphQLSchema instance which can be then used with all GraphQL.js tools, but cannot be used to execute a query, as introspection does not represent the “resolver”, “parse” or “serialize” functions or any other server-internal mechanisms.

结构表示

¥Schema Representation

buildSchema

function buildSchema(source: string | Source): GraphQLSchema

从 GraphQL 结构语言创建 GraphQLSchema 对象。该结构将使用默认解析器。有关 GraphQL 结构语言的更多详细信息,请参阅 结构语言文档 或此 结构语言备忘单

¥Creates a GraphQLSchema object from GraphQL schema language. The schema will use default resolvers. For more detail on the GraphQL schema language, see the schema language docs or this schema language cheat sheet.

printSchema

function printSchema(schema: GraphQLSchema): string

以结构语言格式打印提供的结构。

¥Prints the provided schema in the Schema Language format.

printIntrospectionSchema

function printIntrospectionSchema(schema: GraphQLSchema): string

以结构语言格式打印内置内省结构。

¥Prints the built-in introspection schema in the Schema Language format.

buildASTSchema

function buildASTSchema(
  ast: SchemaDocument,
  queryTypeName: string,
  mutationTypeName: string
): GraphQLSchema

这采用了 parseSchemaIntoASTgraphql/language/schema 中生成的结构文档的 ast,并构造了一个 GraphQLSchema 实例,该实例随后可以与所有 GraphQL.js 工具一起使用,但不能用于执行查询,因为内省并不代表 “解析器”、“parse” 或 “serialize” 函数或任何其他服务器内部机制。

¥This takes the ast of a schema document produced by parseSchemaIntoAST in graphql/language/schema and constructs a GraphQLSchema instance which can be then used with all GraphQL.js tools, but cannot be used to execute a query, as introspection does not represent the “resolver”, “parse” or “serialize” functions or any other server-internal mechanisms.

typeFromAST

function typeFromAST(
  schema: GraphQLSchema,
  inputTypeAST: Type
): GraphQLType

给定 GraphQL AST 和结构中出现的类型名称,从该结构返回相应的 GraphQLType。

¥Given the name of a Type as it appears in a GraphQL AST and a Schema, return the corresponding GraphQLType from that schema.

astFromValue

function astFromValue(
  value: any,
  type: GraphQLInputType
): Value

根据给定的 JavaScript 值生成 GraphQL 输入值 AST。

¥Produces a GraphQL Input Value AST given a JavaScript value.

(可选)可以提供 GraphQL 类型,该类型将用于消除值原语之间的歧义。

¥Optionally, a GraphQL type may be provided, which will be used to disambiguate between value primitives.

访客

¥Visitors

TypeInfo

class TypeInfo {
  constructor(schema: GraphQLSchema)
  getType(): GraphQLOutputType
  getParentType(): GraphQLCompositeType
  getInputType(): GraphQLInputType
  getFieldDef(): GraphQLFieldDefinition
  getDirective(): GraphQLDirective
  getArgument(): GraphQLArgument
}

TypeInfo 是一个实用程序类,在给定 GraphQL 结构的情况下,它可以通过调用 enter(node)leave(node) 在递归下降期间跟踪 GraphQL 文档 AST 中任意点的当前字段和类型定义。

¥TypeInfo is a utility class which, given a GraphQL schema, can keep track of the current field and type definitions at any point in a GraphQL document AST during a recursive descent by calling enter(node) and leave(node).

值验证

¥Value Validation

isValidJSValue

function isValidJSValue(value: any, type: GraphQLInputType): string[]

给定一个 JavaScript 值和一个 GraphQL 类型,确定该类型是否接受该值。这主要用于验证查询变量的运行时值。

¥Given a JavaScript value and a GraphQL type, determine if the value will be accepted for that type. This is primarily useful for validating the runtime values of query variables.

isValidLiteralValue

function isValidLiteralValue(
  type: GraphQLInputType,
  valueAST: Value
): string[]

验证器的实用程序,用于确定给定输入类型的值字面量 AST 是否有效。

¥Utility for validators which determines if a value literal AST is valid given an input type.

请注意,这仅验证字面量值,假设变量提供正确类型的值。

¥Note that this only validates literal values, variables are assumed to provide values of the correct type.