GraphQL LogoGraphQL

graphql/utilities

graphql/utilities 模組包含可與 GraphQL 語言和類型物件一起使用的常見實用運算。你可以從 graphql/utilities 模組或根 graphql 模組匯入。例如

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

概觀#

內省

架構語言

訪客

值驗證

內省#

introspectionQuery#

var introspectionQuery: string

GraphQL 查詢,查詢伺服器的內省系統,以取得足夠的資訊來重現該伺服器的類型系統。

buildClientSchema#

function buildClientSchema(
introspection: IntrospectionQuery
): GraphQLSchema

為客戶端工具建立 GraphQLSchema。

根據執行內省查詢的客戶端結果,建立並傳回一個 GraphQLSchema 執行個體,然後可以使用所有 GraphQL.js 工具,但無法用於執行查詢,因為內省不代表「解析器」、「剖析」或「序列化」函式或任何其他伺服器內部機制。

架構表示法#

buildSchema#

function buildSchema(source: string | Source): GraphQLSchema {

從 GraphQL 架構語言建立一個 GraphQLSchema 物件。該架構將使用預設解析器。有關 GraphQL 架構語言的更多詳細資訊,請參閱 架構語言文件 或這個 架構語言秘笈

printSchema#

function printSchema(schema: GraphQLSchema): string {

以架構語言格式列印提供的架構。

printIntrospectionSchema#

function printIntrospectionSchema(schema: GraphQLSchema): string {

以架構語言格式列印內建的內省架構。

buildASTSchema#

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

這會採用 graphql/language/schemaparseSchemaIntoAST 產生的架構文件 AST,並建構一個 GraphQLSchema 執行個體,然後可以使用所有 GraphQL.js 工具,但無法用於執行查詢,因為內省不代表「解析器」、「剖析」或「序列化」函式或任何其他伺服器內部機制。

typeFromAST#

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

給定 GraphQL AST 中 Type 的名稱和 Schema,從該 schema 中傳回對應的 GraphQLType。

astFromValue#

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

提供 JavaScript 值產生 GraphQL 輸入值 AST。

選擇性地,可以提供一個 GraphQL 類型,它將用於區分值原語。

Visitors#

TypeInfo#

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

TypeInfo 是個實用程式類別,給定一個 GraphQL schema,它可以在遞迴下降期間追蹤 GraphQL 文件 AST 中的當前欄位和類型定義,透過呼叫 enter(node)leave(node)

值驗證#

isValidJSValue#

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

給定一個 JavaScript 值和一個 GraphQL 類型,判斷該值是否會被接受為該類型。這主要用於驗證查詢變數的執行時期值。

isValidLiteralValue#

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

給定輸入類型,驗證器實用程式用於判斷值文字 AST 是否有效。

請注意,這只會驗證文字值,變數假設會提供正確類型的值。

繼續閱讀 →graphql/validation