diff --git a/src/type/introspection.js b/src/type/introspection.js index 7db1b23711..fe5b68f16a 100644 --- a/src/type/introspection.js +++ b/src/type/introspection.js @@ -12,7 +12,11 @@ import inspect from '../jsutils/inspect'; import { astFromValue } from '../utilities/astFromValue'; import { print } from '../language/printer'; import { + type GraphQLType, + type GraphQLInputField, + type GraphQLEnumValue, type GraphQLField, + type GraphQLFieldConfigMap, GraphQLObjectType, GraphQLEnumType, GraphQLList, @@ -28,6 +32,8 @@ import { isAbstractType, isNamedType, } from './definition'; +import { type GraphQLSchema } from './schema'; +import { type GraphQLDirective } from './directives'; import { GraphQLString, GraphQLBoolean } from './scalars'; import { DirectiveLocation } from '../language/directiveLocation'; @@ -35,61 +41,63 @@ export const __Schema = new GraphQLObjectType({ name: '__Schema', description: 'A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.', - fields: () => ({ - types: { - description: 'A list of all types supported by this server.', - type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Type))), - resolve(schema) { - return objectValues(schema.getTypeMap()); + fields: () => + ({ + types: { + description: 'A list of all types supported by this server.', + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Type))), + resolve(schema) { + return objectValues(schema.getTypeMap()); + }, }, - }, - queryType: { - description: 'The type that query operations will be rooted at.', - type: GraphQLNonNull(__Type), - resolve: schema => schema.getQueryType(), - }, - mutationType: { - description: - 'If this server supports mutation, the type that mutation operations will be rooted at.', - type: __Type, - resolve: schema => schema.getMutationType(), - }, - subscriptionType: { - description: - 'If this server support subscription, the type that subscription operations will be rooted at.', - type: __Type, - resolve: schema => schema.getSubscriptionType(), - }, - directives: { - description: 'A list of all directives supported by this server.', - type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Directive))), - resolve: schema => schema.getDirectives(), - }, - }), + queryType: { + description: 'The type that query operations will be rooted at.', + type: GraphQLNonNull(__Type), + resolve: schema => schema.getQueryType(), + }, + mutationType: { + description: + 'If this server supports mutation, the type that mutation operations will be rooted at.', + type: __Type, + resolve: schema => schema.getMutationType(), + }, + subscriptionType: { + description: + 'If this server support subscription, the type that subscription operations will be rooted at.', + type: __Type, + resolve: schema => schema.getSubscriptionType(), + }, + directives: { + description: 'A list of all directives supported by this server.', + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Directive))), + resolve: schema => schema.getDirectives(), + }, + }: GraphQLFieldConfigMap), }); export const __Directive = new GraphQLObjectType({ name: '__Directive', description: "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.", - fields: () => ({ - name: { - type: GraphQLNonNull(GraphQLString), - resolve: obj => obj.name, - }, - description: { - type: GraphQLString, - resolve: obj => obj.description, - }, - locations: { - type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__DirectiveLocation))), - resolve: obj => obj.locations, - }, - args: { - type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), - resolve: directive => directive.args || [], - }, - }), + fields: () => + ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + locations: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__DirectiveLocation))), + resolve: obj => obj.locations, + }, + args: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), + resolve: directive => directive.args || [], + }, + }: GraphQLFieldConfigMap), }); export const __DirectiveLocation = new GraphQLEnumType({ @@ -180,186 +188,191 @@ export const __Type = new GraphQLObjectType({ name: '__Type', description: 'The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.', - fields: () => ({ - kind: { - type: GraphQLNonNull(__TypeKind), - resolve(type) { - if (isScalarType(type)) { - return TypeKind.SCALAR; - } else if (isObjectType(type)) { - return TypeKind.OBJECT; - } else if (isInterfaceType(type)) { - return TypeKind.INTERFACE; - } else if (isUnionType(type)) { - return TypeKind.UNION; - } else if (isEnumType(type)) { - return TypeKind.ENUM; - } else if (isInputObjectType(type)) { - return TypeKind.INPUT_OBJECT; - } else if (isListType(type)) { - return TypeKind.LIST; - } else if (isNonNullType(type)) { - return TypeKind.NON_NULL; - } + fields: () => + ({ + kind: { + type: GraphQLNonNull(__TypeKind), + resolve(type) { + if (isScalarType(type)) { + return TypeKind.SCALAR; + } else if (isObjectType(type)) { + return TypeKind.OBJECT; + } else if (isInterfaceType(type)) { + return TypeKind.INTERFACE; + } else if (isUnionType(type)) { + return TypeKind.UNION; + } else if (isEnumType(type)) { + return TypeKind.ENUM; + } else if (isInputObjectType(type)) { + return TypeKind.INPUT_OBJECT; + } else if (isListType(type)) { + return TypeKind.LIST; + } else if (isNonNullType(type)) { + return TypeKind.NON_NULL; + } - // Not reachable. All possible types have been considered. - /* istanbul ignore next */ - throw new Error(`Unexpected type: "${inspect((type: empty))}".`); + // Not reachable. All possible types have been considered. + /* istanbul ignore next */ + throw new Error(`Unexpected type: "${inspect((type: empty))}".`); + }, }, - }, - name: { - type: GraphQLString, - resolve: obj => obj.name, - }, - description: { - type: GraphQLString, - resolve: obj => obj.description, - }, - fields: { - type: GraphQLList(GraphQLNonNull(__Field)), - args: { - includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + name: { + type: GraphQLString, + resolve: obj => (obj.name !== undefined ? obj.name : undefined), }, - resolve(type, { includeDeprecated }) { - if (isObjectType(type) || isInterfaceType(type)) { - let fields = objectValues(type.getFields()); - if (!includeDeprecated) { - fields = fields.filter(field => !field.deprecationReason); + description: { + type: GraphQLString, + resolve: obj => + obj.description !== undefined ? obj.description : undefined, + }, + fields: { + type: GraphQLList(GraphQLNonNull(__Field)), + args: { + includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + }, + resolve(type, { includeDeprecated }) { + if (isObjectType(type) || isInterfaceType(type)) { + let fields = objectValues(type.getFields()); + if (!includeDeprecated) { + fields = fields.filter(field => !field.deprecationReason); + } + return fields; } - return fields; - } - return null; + return null; + }, }, - }, - interfaces: { - type: GraphQLList(GraphQLNonNull(__Type)), - resolve(type) { - if (isObjectType(type)) { - return type.getInterfaces(); - } + interfaces: { + type: GraphQLList(GraphQLNonNull(__Type)), + resolve(type) { + if (isObjectType(type)) { + return type.getInterfaces(); + } + }, }, - }, - possibleTypes: { - type: GraphQLList(GraphQLNonNull(__Type)), - resolve(type, args, context, { schema }) { - if (isAbstractType(type)) { - return schema.getPossibleTypes(type); - } + possibleTypes: { + type: GraphQLList(GraphQLNonNull(__Type)), + resolve(type, args, context, { schema }) { + if (isAbstractType(type)) { + return schema.getPossibleTypes(type); + } + }, }, - }, - enumValues: { - type: GraphQLList(GraphQLNonNull(__EnumValue)), - args: { - includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + enumValues: { + type: GraphQLList(GraphQLNonNull(__EnumValue)), + args: { + includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + }, + resolve(type, { includeDeprecated }) { + if (isEnumType(type)) { + let values = type.getValues(); + if (!includeDeprecated) { + values = values.filter(value => !value.deprecationReason); + } + return values; + } + }, }, - resolve(type, { includeDeprecated }) { - if (isEnumType(type)) { - let values = type.getValues(); - if (!includeDeprecated) { - values = values.filter(value => !value.deprecationReason); + inputFields: { + type: GraphQLList(GraphQLNonNull(__InputValue)), + resolve(type) { + if (isInputObjectType(type)) { + return objectValues(type.getFields()); } - return values; - } + }, }, - }, - inputFields: { - type: GraphQLList(GraphQLNonNull(__InputValue)), - resolve(type) { - if (isInputObjectType(type)) { - return objectValues(type.getFields()); - } + ofType: { + type: __Type, + resolve: obj => (obj.ofType !== undefined ? obj.ofType : undefined), }, - }, - ofType: { - type: __Type, - resolve: obj => obj.ofType, - }, - }), + }: GraphQLFieldConfigMap), }); export const __Field = new GraphQLObjectType({ name: '__Field', description: 'Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.', - fields: () => ({ - name: { - type: GraphQLNonNull(GraphQLString), - resolve: obj => obj.name, - }, - description: { - type: GraphQLString, - resolve: obj => obj.description, - }, - args: { - type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), - resolve: field => field.args || [], - }, - type: { - type: GraphQLNonNull(__Type), - resolve: obj => obj.type, - }, - isDeprecated: { - type: GraphQLNonNull(GraphQLBoolean), - resolve: obj => obj.isDeprecated, - }, - deprecationReason: { - type: GraphQLString, - resolve: obj => obj.deprecationReason, - }, - }), + fields: () => + ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + args: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), + resolve: field => field.args || [], + }, + type: { + type: GraphQLNonNull(__Type), + resolve: obj => obj.type, + }, + isDeprecated: { + type: GraphQLNonNull(GraphQLBoolean), + resolve: obj => obj.isDeprecated, + }, + deprecationReason: { + type: GraphQLString, + resolve: obj => obj.deprecationReason, + }, + }: GraphQLFieldConfigMap, mixed>), }); export const __InputValue = new GraphQLObjectType({ name: '__InputValue', description: 'Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.', - fields: () => ({ - name: { - type: GraphQLNonNull(GraphQLString), - resolve: obj => obj.name, - }, - description: { - type: GraphQLString, - resolve: obj => obj.description, - }, - type: { - type: GraphQLNonNull(__Type), - resolve: obj => obj.type, - }, - defaultValue: { - type: GraphQLString, - description: - 'A GraphQL-formatted string representing the default value for this input value.', - resolve(inputVal) { - const valueAST = astFromValue(inputVal.defaultValue, inputVal.type); - return valueAST ? print(valueAST) : null; + fields: () => + ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, }, - }, - }), + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + type: { + type: GraphQLNonNull(__Type), + resolve: obj => obj.type, + }, + defaultValue: { + type: GraphQLString, + description: + 'A GraphQL-formatted string representing the default value for this input value.', + resolve(inputVal) { + const valueAST = astFromValue(inputVal.defaultValue, inputVal.type); + return valueAST ? print(valueAST) : null; + }, + }, + }: GraphQLFieldConfigMap), }); export const __EnumValue = new GraphQLObjectType({ name: '__EnumValue', description: 'One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.', - fields: () => ({ - name: { - type: GraphQLNonNull(GraphQLString), - resolve: obj => obj.name, - }, - description: { - type: GraphQLString, - resolve: obj => obj.description, - }, - isDeprecated: { - type: GraphQLNonNull(GraphQLBoolean), - resolve: obj => obj.isDeprecated, - }, - deprecationReason: { - type: GraphQLString, - resolve: obj => obj.deprecationReason, - }, - }), + fields: () => + ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + isDeprecated: { + type: GraphQLNonNull(GraphQLBoolean), + resolve: obj => obj.isDeprecated, + }, + deprecationReason: { + type: GraphQLString, + resolve: obj => obj.deprecationReason, + }, + }: GraphQLFieldConfigMap), }); export const TypeKind = Object.freeze({ @@ -423,7 +436,7 @@ export const __TypeKind = new GraphQLEnumType({ * so the format for args is different. */ -export const SchemaMetaFieldDef: GraphQLField<*, *> = { +export const SchemaMetaFieldDef: GraphQLField = { name: '__schema', type: GraphQLNonNull(__Schema), description: 'Access the current type schema of this server.', @@ -431,7 +444,7 @@ export const SchemaMetaFieldDef: GraphQLField<*, *> = { resolve: (source, args, context, { schema }) => schema, }; -export const TypeMetaFieldDef: GraphQLField<*, *> = { +export const TypeMetaFieldDef: GraphQLField = { name: '__type', type: __Type, description: 'Request the type information of a single type.', @@ -439,7 +452,7 @@ export const TypeMetaFieldDef: GraphQLField<*, *> = { resolve: (source, { name }, context, { schema }) => schema.getType(name), }; -export const TypeNameMetaFieldDef: GraphQLField<*, *> = { +export const TypeNameMetaFieldDef: GraphQLField = { name: '__typename', type: GraphQLNonNull(GraphQLString), description: 'The name of the current Object type at runtime.',