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;}
模式是通过提供每种操作类型、查询和变异(可选)的根类型来创建的。然后将模式定义提供给验证器和执行器。
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};
当字段可以返回一组异构类型之一时,将使用接口类型来描述哪些类型是可能的,所有类型中有哪些字段是通用的,以及一个函数来确定解析字段时实际使用的类型。
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>;
当字段可以返回一组异构类型之一时,将使用联合类型来描述哪些类型是可能的,以及提供一个函数来确定解析字段时实际使用的类型。
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
一个GraphQLScalarType,表示一个 ID。