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
| Name | Type | Description |
|---|---|---|
| config | Readonly<GraphQLSchemaConfig> | Configuration describing this object. |
Returns
| Type | Description |
|---|---|
GraphQLSchema |
Members
| Name | Type | Description |
|---|---|---|
| description | null | undefined | string | Human-readable description for this schema element, if provided. |
| extensions | Readonly<GraphQLSchemaExtensions> | Extension fields to include in the formatted result. |
| astNode | null | undefined | SchemaDefinitionNode | AST node from which this schema element was built, if available. |
| extensionASTNodes | readonly SchemaExtensionNode[] | AST extension nodes applied to this schema element. |
| __validationErrors | null | 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
| Type | Description |
|---|---|
null | undefined | GraphQLObjectType | The query root type, if this schema defines one. |
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getQueryType();
// result contains the getQueryType return valuegetMutationType()
Returns the root object type for mutation operations.
Signature:
getMutationType(): null | undefined | GraphQLObjectType
Returns
| Type | Description |
|---|---|
null | undefined | GraphQLObjectType | The mutation root type, if this schema defines one. |
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getMutationType();
// result contains the getMutationType return valuegetSubscriptionType()
Returns the root object type for subscription operations.
Signature:
getSubscriptionType(): null | undefined | GraphQLObjectType
Returns
| Type | Description |
|---|---|
null | undefined | GraphQLObjectType | The subscription root type, if this schema defines one. |
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getSubscriptionType();
// result contains the getSubscriptionType return valuegetRootType()
Returns the root object type for the requested operation kind.
Signature:
getRootType(operation: OperationTypeNode): null | undefined | GraphQLObjectType
Arguments
| Name | Type | Description |
|---|---|---|
| operation | OperationTypeNode | Operation kind to resolve. |
Returns
| Type | Description |
|---|---|
null | undefined | GraphQLObjectType | The 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 valuegetTypeMap()
Returns all named types known to this schema.
Signature:
getTypeMap(): object
Returns
| Type | Description |
|---|---|
object | A map of schema types keyed by type name. |
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getTypeMap();
// result contains the getTypeMap return valuegetType()
Returns the named type with the provided name.
Signature:
getType(name: string): GraphQLNamedType | undefined
Arguments
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name to look up. |
Returns
| Type | Description |
|---|---|
GraphQLNamedType | undefined | The named schema type, if one exists. |
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getType(name);
// result contains the getType return valuegetPossibleTypes()
Returns object types that may be returned for an abstract type.
Signature:
getPossibleTypes(abstractType: GraphQLAbstractType): readonly GraphQLObjectType[]
Arguments
| Name | Type | Description |
|---|---|---|
| abstractType | GraphQLAbstractType | Interface or union type to inspect. |
Returns
| Type | Description |
|---|---|
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 valuegetImplementations()
Returns objects and interfaces that implement an interface type.
Signature:
getImplementations(interfaceType: GraphQLInterfaceType): { objects: readonly GraphQLObjectType[]; interfaces: readonly GraphQLInterfaceType[] }
Arguments
| Name | Type | Description |
|---|---|---|
| interfaceType | GraphQLInterfaceType | Interface type to inspect. |
Returns
| Type | Description |
|---|---|
{ 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 valueisSubType()
Returns whether one type is a possible runtime subtype of an abstract type.
Signature:
isSubType(abstractType: GraphQLAbstractType, maybeSubType: GraphQLObjectType | GraphQLInterfaceType): boolean
Arguments
| Name | Type | Description |
|---|---|---|
| abstractType | GraphQLAbstractType | Interface or union type to inspect. |
| maybeSubType | GraphQLObjectType | GraphQLInterfaceType | Object or interface type to test as a possible subtype. |
Returns
| Type | Description |
|---|---|
boolean | True 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 valuegetDirectives()
Returns directives available in this schema.
Signature:
getDirectives(): readonly GraphQLDirective[]
Returns
| Type | Description |
|---|---|
readonly GraphQLDirective[] | Directives available in this schema. |
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getDirectives();
// result contains the getDirectives return valuegetDirective()
Returns the current directive definition.
Signature:
getDirective(name: string): null | undefined | GraphQLDirective
Arguments
| Name | Type | Description |
|---|---|---|
| name | string | The GraphQL name to look up. |
Returns
| Type | Description |
|---|---|
null | undefined | GraphQLDirective | The current directive definition, if known. |
Example
// Given a GraphQLSchema instance named schema:
const result = schema.getDirective(name);
// result contains the getDirective return valuetoConfig()
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
| Type | Description |
|---|---|
{ 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 valueFunctions
isSchema()
Test if the given value is a GraphQL schema.
Signature:
isSchema(schema: unknown): schema is GraphQLSchema
Arguments
| Name | Type | Description |
|---|---|---|
| schema | unknown | The GraphQL schema to use. |
Returns
| Type | Description |
|---|---|
schema is GraphQLSchema | True when the value matches this type. |
Example
import { isSchema, GraphQLString } from 'graphql/type';
const result = isSchema(GraphQLString);
// result: true for matching GraphQL typesassertSchema()
Returns the value as a GraphQLSchema, or throws if it is not a schema.
Signature:
assertSchema(schema: unknown): GraphQLSchema
Arguments
| Name | Type | Description |
|---|---|---|
| schema | unknown | The GraphQL schema to use. |
Returns
| Type | Description |
|---|---|
GraphQLSchema | The value typed as a GraphQLSchema. |
Example
import { assertSchema, GraphQLString } from 'graphql/type';
const type = assertSchema(GraphQLString);
// type: GraphQLStringTypes
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
| Name | Type | Description |
|---|---|---|
| description? | null | undefined | string | Human-readable description for this schema element, if provided. |
| query? | null | undefined | GraphQLObjectType | Root object type for query operations. |
| mutation? | null | undefined | GraphQLObjectType | Root object type for mutation operations. |
| subscription? | null | undefined | GraphQLObjectType | Root 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 | SchemaDefinitionNode | AST node from which this schema element was built, if available. |
| extensionASTNodes? | null | undefined | readonly SchemaExtensionNode[] | AST extension nodes applied to this schema element. |