graphql/type
模組負責定義 GraphQL 類型和架構。您可以從 graphql/type
模組或根 graphql
模組匯入。例如
import { GraphQLSchema } from "graphql" // ES6var { GraphQLSchema } = require("graphql") // CommonJS
架構
定義
class GraphQLScalarTypeGraphQL 中的純量類型。
class GraphQLObjectTypeGraphQL 中包含欄位的物件類型。
class GraphQLInterfaceTypeGraphQL 中的介面類型,定義實作將包含的欄位。
class GraphQLUnionTypeGraphQL 中的聯合類型,定義實作清單。
class GraphQLEnumTypeGraphQL 中的列舉類型,定義有效值清單。
class GraphQLInputObjectTypeGraphQL 中的輸入物件類型,表示結構化輸入。
class GraphQLList包覆其他類型的類型包裝器,表示這些類型的清單。
class GraphQLNonNull包覆其他類型的類型包裝器,表示這些類型的非空版本。
謂詞
非修飾詞
純量
class GraphQLSchema { constructor(config: GraphQLSchemaConfig)}
type GraphQLSchemaConfig = { query: GraphQLObjectType; mutation?: ?GraphQLObjectType;}
Schema 是透過提供每種作業類型(查詢和突變(選用))的根類型來建立的。然後將 Schema 定義提供給驗證器和執行器。
var MyAppSchema = new GraphQLSchema({ query: MyAppQueryRootType mutation: MyAppMutationRootType});
class GraphQLScalarType<InternalType> { constructor(config: GraphQLScalarTypeConfig<InternalType>)}
type GraphQLScalarTypeConfig<InternalType> = { name: string; description?: ?string; serialize: (value: mixed) => ?InternalType; parseValue?: (value: mixed) => ?InternalType; parseLiteral?: (valueAST: Value) => ?InternalType;}
任何要求和輸入值到引數的葉值都是純量(或列舉),並定義為名稱和一系列用於確保有效性的序列化函數。
var OddType = new GraphQLScalarType({ name: "Odd", serialize: oddValue, parseValue: oddValue, parseLiteral(ast) { if (ast.kind === Kind.INT) { return oddValue(parseInt(ast.value, 10)) } return null },})
function oddValue(value) { return value % 2 === 1 ? value : null}
class GraphQLObjectType { constructor(config: GraphQLObjectTypeConfig)}
type GraphQLObjectTypeConfig = { name: string; interfaces?: GraphQLInterfacesThunk | Array<GraphQLInterfaceType>; fields: GraphQLFieldConfigMapThunk | GraphQLFieldConfigMap; isTypeOf?: (value: any, info?: GraphQLResolveInfo) => boolean; description?: ?string}
type GraphQLInterfacesThunk = () => Array<GraphQLInterfaceType>;
type GraphQLFieldConfigMapThunk = () => GraphQLFieldConfigMap;
// See below about resolver functions.type GraphQLFieldResolveFn = ( source?: any, args?: {[argName: string]: any}, context?: any, info?: GraphQLResolveInfo) => any
type GraphQLResolveInfo = { fieldName: string, fieldNodes: Array<Field>, returnType: GraphQLOutputType, parentType: GraphQLCompositeType, schema: GraphQLSchema, fragments: { [fragmentName: string]: FragmentDefinition }, rootValue: any, operation: OperationDefinition, variableValues: { [variableName: string]: any },}
type GraphQLFieldConfig = { type: GraphQLOutputType; args?: GraphQLFieldConfigArgumentMap; resolve?: GraphQLFieldResolveFn; deprecationReason?: string; description?: ?string;}
type GraphQLFieldConfigArgumentMap = { [argName: string]: GraphQLArgumentConfig;};
type GraphQLArgumentConfig = { type: GraphQLInputType; defaultValue?: any; description?: ?string;}
type GraphQLFieldConfigMap = { [fieldName: string]: GraphQLFieldConfig;};
您定義的大部分 GraphQL 類型都是物件類型。物件類型有名稱,但最重要的是描述其欄位。
當兩個類型需要互相參照,或類型需要在欄位中參照自身時,您可以使用函數表達式(又稱為閉包或 thunk)來提供欄位(延遲)。
請注意,解析器函式會將 source
物件作為第一個參數提供。不過,如果未提供解析器函式,則會使用預設解析器,它會在 source
上尋找與欄位同名的函式。如果找到,則會以 (args, context, info)
呼叫函式。由於它是 source
上的函式,因此該值始終可以使用 this
參照。
var AddressType = new GraphQLObjectType({ name: "Address", fields: { street: { type: GraphQLString }, number: { type: GraphQLInt }, formatted: { type: GraphQLString, resolve(obj) { return obj.number + " " + obj.street }, }, },})
var PersonType = new GraphQLObjectType({ name: "Person", fields: () => ({ name: { type: GraphQLString }, bestFriend: { type: PersonType }, }),})
class GraphQLInterfaceType { constructor(config: GraphQLInterfaceTypeConfig)}
type GraphQLInterfaceTypeConfig = { name: string, fields: GraphQLFieldConfigMapThunk | GraphQLFieldConfigMap, resolveType?: (value: any, info?: GraphQLResolveInfo) => ?GraphQLObjectType, description?: ?string};
當一個欄位可以傳回一組異質類型的其中一個時,會使用 Interface 類型來描述哪些類型是可能的,所有類型中有哪些欄位是共用的,以及一個函式來判斷在解析欄位時實際使用哪個類型。
var EntityType = new GraphQLInterfaceType({ name: "Entity", fields: { name: { type: GraphQLString }, },})
class GraphQLUnionType { constructor(config: GraphQLUnionTypeConfig)}
type GraphQLUnionTypeConfig = { name: string, types: GraphQLObjectsThunk | Array<GraphQLObjectType>, resolveType?: (value: any, info?: GraphQLResolveInfo) => ?GraphQLObjectType; description?: ?string;};
type GraphQLObjectsThunk = () => Array<GraphQLObjectType>;
當一個欄位可以傳回一組異質類型的其中一個時,會使用 Union 類型來描述哪些類型是可能的,以及提供一個函式來判斷在解析欄位時實際使用哪個類型。
var PetType = new GraphQLUnionType({ name: "Pet", types: [DogType, CatType], resolveType(value) { if (value instanceof Dog) { return DogType } if (value instanceof Cat) { return CatType } },})
class GraphQLEnumType { constructor(config: GraphQLEnumTypeConfig)}
type GraphQLEnumTypeConfig = { name: string; values: GraphQLEnumValueConfigMap; description?: ?string;}
type GraphQLEnumValueConfigMap = { [valueName: string]: GraphQLEnumValueConfig;};
type GraphQLEnumValueConfig = { value?: any; deprecationReason?: string; description?: ?string;}
type GraphQLEnumValueDefinition = { name: string; value?: any; deprecationReason?: string; description?: ?string;}
請求和輸入值的某些葉值是列舉。GraphQL 會將列舉值序列化為字串,不過在內部,列舉可以用任何類型的類型表示,通常是整數。
注意:如果在定義中未提供值,則會將列舉值的「名稱」用作其內部值。
var RGBType = new GraphQLEnumType({ name: "RGB", values: { RED: { value: 0 }, GREEN: { value: 1 }, BLUE: { value: 2 }, },})
class GraphQLInputObjectType { constructor(config: GraphQLInputObjectConfig)}
type GraphQLInputObjectConfig = { name: string; fields: GraphQLInputObjectConfigFieldMapThunk | GraphQLInputObjectConfigFieldMap; description?: ?string;}
type GraphQLInputObjectConfigFieldMapThunk = () => GraphQLInputObjectConfigFieldMap;
type GraphQLInputObjectFieldConfig = { type: GraphQLInputType; defaultValue?: any; description?: ?string;}
type GraphQLInputObjectConfigFieldMap = { [fieldName: string]: GraphQLInputObjectFieldConfig;};
type GraphQLInputObjectField = { name: string; type: GraphQLInputType; defaultValue?: any; description?: ?string;}
type GraphQLInputObjectFieldMap = { [fieldName: string]: GraphQLInputObjectField;};
輸入物件定義一個結構化的欄位集合,可以提供給欄位參數。
使用 NonNull
會確保查詢必須提供一個值
var GeoPoint = new GraphQLInputObjectType({ name: "GeoPoint", fields: { lat: { type: new GraphQLNonNull(GraphQLFloat) }, lon: { type: new GraphQLNonNull(GraphQLFloat) }, alt: { type: GraphQLFloat, defaultValue: 0 }, },})
class GraphQLList { constructor(type: GraphQLType)}
清單是一種類型標記,一個指向另一種類型的包裝類型。清單通常在定義物件類型欄位時建立。
var PersonType = new GraphQLObjectType({ name: "Person", fields: () => ({ parents: { type: new GraphQLList(PersonType) }, children: { type: new GraphQLList(PersonType) }, }),})
class GraphQLNonNull { constructor(type: GraphQLType)}
非空值是一種類型標記,一個指向另一種類型的包裝類型。非空值類型強制其值永遠不會為空,並確保如果在請求期間發生這種情況,就會引發錯誤。它對於你可以對非空性做出有力保證的欄位很有用,例如資料庫列的 id 欄位通常永遠不會為空。
var RowType = new GraphQLObjectType({ name: "Row", fields: () => ({ id: { type: new GraphQLNonNull(String) }, }),})
function isInputType(type: ?GraphQLType): boolean
這些類型可以用作參數和指令的輸入類型。
function isOutputType(type: ?GraphQLType): boolean
這些類型可以用作欄位的結果輸出類型
function isLeafType(type: ?GraphQLType): boolean
這些類型可以描述可以是葉值值的類型
function isCompositeType(type: ?GraphQLType): boolean
這些類型可能會描述選取集的父項脈絡
function isAbstractType(type: ?GraphQLType): boolean
這些類型可能會描述物件類型的組合
function getNullableType(type: ?GraphQLType): ?GraphQLNullableType
如果給定的類型是非可為空類型,這會移除非可為空類型並傳回基礎類型。
function getNamedType(type: ?GraphQLType): ?GraphQLNamedType
如果給定的類型是非可為空類型或清單,這會重複移除非可為空類型和清單包裝器,並傳回基礎類型。
var GraphQLInt: GraphQLScalarType
表示整數的 GraphQLScalarType
。
var GraphQLFloat: GraphQLScalarType
表示浮點數的 GraphQLScalarType
。
var GraphQLString: GraphQLScalarType
表示字串的 GraphQLScalarType
。
var GraphQLBoolean: GraphQLScalarType
表示布林值的 GraphQLScalarType
。
var GraphQLID: GraphQLScalarType
表示 ID 的 GraphQLScalarType
。