graphql/type/schema

Classes

GraphQLSchema

Schema Definition

A Schema is created by supplying the root types of each type of operation, query and mutation (optional). A schema definition is then supplied to the validator and executor.

Example:

const MyAppSchema = new GraphQLSchema({
  query: MyAppQueryRootType,
  mutation: MyAppMutationRootType,
})

Note: When the schema is constructed, by default only the types that are reachable by traversing the root types are included, other types must be explicitly referenced.

Example:

const characterInterface = new GraphQLInterfaceType({
  name: 'Character',
  ...
});
 
const humanType = new GraphQLObjectType({
  name: 'Human',
  interfaces: [characterInterface],
  ...
});
 
const droidType = new GraphQLObjectType({
  name: 'Droid',
  interfaces: [characterInterface],
  ...
});
 
const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      hero: { type: characterInterface, ... },
    }
  }),
  ...
  // Since this schema references only the `Character` interface it's
  // necessary to explicitly list the types that implement it if
  // you want them to be included in the final schema.
  types: [humanType, droidType],
})

Note: If an array of directives are provided to GraphQLSchema, that will be the exact list of directives represented and allowed. If directives is not provided then a default set of the specified directives (e.g. @include and @skip) will be used. If you wish to provide additional directives to these specified directives, you must explicitly declare them. Example:

const MyAppSchema = new GraphQLSchema({
  ...
  directives: specifiedDirectives.concat([ myCustomDirective ]),
})

Constructor

Creates a GraphQLSchema instance.

Signature:

new GraphQLSchema(config: Readonly<GraphQLSchemaConfig>)
Arguments
NameTypeDescription
configReadonly<GraphQLSchemaConfig>Configuration describing this object.
Returns
TypeDescription
GraphQLSchema

Members

NameTypeDescription
descriptionnull | undefined | stringHuman-readable description for this schema element, if provided.
extensionsReadonly<GraphQLSchemaExtensions>Extension fields to include in the formatted result.
astNodenull | undefined | SchemaDefinitionNodeAST node from which this schema element was built, if available.
extensionASTNodesreadonly SchemaExtensionNode[]AST extension nodes applied to this schema element.
__validationErrorsnull | undefined | readonly GraphQLError[]Cached schema validation errors, if validation has already run.

getQueryType()

Returns the root object type for query operations.

Signature:

getQueryType(): null | undefined | GraphQLObjectType
Returns
TypeDescription
null | undefined | GraphQLObjectTypeThe query root type, if this schema defines one.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getQueryType();
 
// result contains the getQueryType return value

getMutationType()

Returns the root object type for mutation operations.

Signature:

getMutationType(): null | undefined | GraphQLObjectType
Returns
TypeDescription
null | undefined | GraphQLObjectTypeThe mutation root type, if this schema defines one.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getMutationType();
 
// result contains the getMutationType return value

getSubscriptionType()

Returns the root object type for subscription operations.

Signature:

getSubscriptionType(): null | undefined | GraphQLObjectType
Returns
TypeDescription
null | undefined | GraphQLObjectTypeThe subscription root type, if this schema defines one.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getSubscriptionType();
 
// result contains the getSubscriptionType return value

getRootType()

Returns the root object type for the requested operation kind.

Signature:

getRootType(operation: OperationTypeNode): null | undefined | GraphQLObjectType
Arguments
NameTypeDescription
operationOperationTypeNodeOperation kind to resolve.
Returns
TypeDescription
null | undefined | GraphQLObjectTypeThe root object type for the operation kind, if this schema defines one.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getRootType(operation);
 
// result contains the getRootType return value

getTypeMap()

Returns all named types known to this schema.

Signature:

getTypeMap(): object
Returns
TypeDescription
objectA map of schema types keyed by type name.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getTypeMap();
 
// result contains the getTypeMap return value

getType()

Returns the named type with the provided name.

Signature:

getType(name: string): GraphQLNamedType | undefined
Arguments
NameTypeDescription
namestringThe GraphQL name to look up.
Returns
TypeDescription
GraphQLNamedType | undefinedThe named schema type, if one exists.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getType(name);
 
// result contains the getType return value

getPossibleTypes()

Returns object types that may be returned for an abstract type.

Signature:

getPossibleTypes(abstractType: GraphQLAbstractType): readonly GraphQLObjectType[]
Arguments
NameTypeDescription
abstractTypeGraphQLAbstractTypeInterface or union type to inspect.
Returns
TypeDescription
readonly GraphQLObjectType[]Object types that may satisfy the abstract type.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getPossibleTypes(abstractType);
 
// result contains the getPossibleTypes return value

getImplementations()

Returns objects and interfaces that implement an interface type.

Signature:

getImplementations(interfaceType: GraphQLInterfaceType): { objects: readonly GraphQLObjectType[]; interfaces: readonly GraphQLInterfaceType[] }
Arguments
NameTypeDescription
interfaceTypeGraphQLInterfaceTypeInterface type to inspect.
Returns
TypeDescription
{ objects: readonly GraphQLObjectType[]; interfaces: readonly GraphQLInterfaceType[] }Object and interface implementations of the interface.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getImplementations(interfaceType);
 
// result contains the getImplementations return value

isSubType()

Returns whether one type is a possible runtime subtype of an abstract type.

Signature:

isSubType(abstractType: GraphQLAbstractType, maybeSubType: GraphQLObjectType | GraphQLInterfaceType): boolean
Arguments
NameTypeDescription
abstractTypeGraphQLAbstractTypeInterface or union type to inspect.
maybeSubTypeGraphQLObjectType | GraphQLInterfaceTypeObject or interface type to test as a possible subtype.
Returns
TypeDescription
booleanTrue when the subtype may satisfy the abstract type.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.isSubType(abstractType, maybeSubType);
 
// result contains the isSubType return value

getDirectives()

Returns directives available in this schema.

Signature:

getDirectives(): readonly GraphQLDirective[]
Returns
TypeDescription
readonly GraphQLDirective[]Directives available in this schema.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getDirectives();
 
// result contains the getDirectives return value

getDirective()

Returns the current directive definition.

Signature:

getDirective(name: string): null | undefined | GraphQLDirective
Arguments
NameTypeDescription
namestringThe GraphQL name to look up.
Returns
TypeDescription
null | undefined | GraphQLDirectiveThe current directive definition, if known.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getDirective(name);
 
// result contains the getDirective return value

toConfig()

Returns a normalized configuration object for this object.

Signature:

toConfig(): { description: null | undefined | string; types: ReadonlyArray<GraphQLNamedType>; directives: ReadonlyArray<GraphQLDirective>; extensions: Readonly<GraphQLSchemaExtensions>; extensionASTNodes: ReadonlyArray<SchemaExtensionNode>; assumeValid: boolean }
Returns
TypeDescription
{ description: null | undefined | string; types: ReadonlyArray<GraphQLNamedType>; directives: ReadonlyArray<GraphQLDirective>; extensions: Readonly<GraphQLSchemaExtensions>; extensionASTNodes: ReadonlyArray<SchemaExtensionNode>; assumeValid: boolean }A configuration object that can be used to recreate this object.
Example
// Given a GraphQLSchema instance named schema:
const result = schema.toConfig();
 
// result contains the toConfig return value

Functions

isSchema()

Test if the given value is a GraphQL schema.

Signature:

isSchema(schema: unknown): schema is GraphQLSchema

Arguments

NameTypeDescription
schemaunknownThe GraphQL schema to use.

Returns

TypeDescription
schema is GraphQLSchemaTrue when the value matches this type.

Example

import { isSchema, GraphQLString } from 'graphql/type';
 
const result = isSchema(GraphQLString);
 
// result: true for matching GraphQL types

assertSchema()

Returns the value as a GraphQLSchema, or throws if it is not a schema.

Signature:

assertSchema(schema: unknown): GraphQLSchema

Arguments

NameTypeDescription
schemaunknownThe GraphQL schema to use.

Returns

TypeDescription
GraphQLSchemaThe value typed as a GraphQLSchema.

Example

import { assertSchema, GraphQLString } from 'graphql/type';
 
const type = assertSchema(GraphQLString);
 
// type: GraphQLString

Types

GraphQLSchemaExtensions

Interface. Custom extensions

Remarks: Use a unique identifier name for your extension, for example the name of your library or project. Do not use a shortened identifier as this increases the risk of conflicts. We recommend you add at most one extension field, an object which can contain all the values you need.

GraphQLSchemaConfig

Interface. Configuration used to construct a GraphQLSchema.

Members

NameTypeDescription
description?null | undefined | stringHuman-readable description for this schema element, if provided.
query?null | undefined | GraphQLObjectTypeRoot object type for query operations.
mutation?null | undefined | GraphQLObjectTypeRoot object type for mutation operations.
subscription?null | undefined | GraphQLObjectTypeRoot object type for subscription operations.
types?null | undefined | readonly GraphQLNamedType[]Object types that belong to this union type.
directives?null | undefined | readonly GraphQLDirective[]Directives available in this schema or applied to this AST node.
extensions?null | undefined | Readonly<GraphQLSchemaExtensions>Extension fields to include in the formatted result.
astNode?null | undefined | SchemaDefinitionNodeAST node from which this schema element was built, if available.
extensionASTNodes?null | undefined | readonly SchemaExtensionNode[]AST extension nodes applied to this schema element.