v16 APIgraphql/utilitiesSchema Construction

graphql/utilities/schema-construction

Functions

buildASTSchema()

Builds a GraphQLSchema from a parsed schema definition language document.

If no schema definition is provided, then it will look for types named Query, Mutation and Subscription.

The resulting schema has no resolver functions, so execution will use the default field resolver.

Signature:

buildASTSchema(documentAST: DocumentNode, options?: BuildSchemaOptions): GraphQLSchema

Arguments

NameTypeDescription
documentASTDocumentNodeThe parsed GraphQL document AST.
options?BuildSchemaOptionsOptional configuration for this operation.

Returns

TypeDescription
GraphQLSchemaThe schema built from the provided SDL document.

Example

import { parse } from 'graphql/language';
import { buildASTSchema } from 'graphql/utilities';
 
const document = parse('type Query { hello: String }');
const schema = buildASTSchema(document);
 
// schema.getQueryType()?.name: 'Query'

buildSchema()

Builds a GraphQLSchema directly from a schema definition language source.

Signature:

buildSchema(source: string | Source, options?: BuildSchemaOptions & ParseOptions): GraphQLSchema

Arguments

NameTypeDescription
sourcestring | SourceThe GraphQL source text or source object.
options?BuildSchemaOptions & ParseOptionsOptional configuration for this operation.

Returns

TypeDescription
GraphQLSchemaThe schema built from the provided SDL document.

Example

import { buildSchema } from 'graphql/utilities';
 
const schema = buildSchema('type Query { hello: String }');
 
// schema.getQueryType()?.name: 'Query'

extendSchema()

Produces a new schema given an existing schema and a document which may contain GraphQL type extensions and definitions. The original schema will remain unaltered.

Because a schema represents a graph of references, a schema cannot be extended without effectively making an entire copy. We do not know until it’s too late if subgraphs remain unchanged.

This algorithm copies the provided schema, applying extensions while producing the copy. The original schema remains unaltered.

Signature:

extendSchema(schema: GraphQLSchema, documentAST: DocumentNode, options?: { assumeValidSDL?: boolean }): GraphQLSchema

Arguments

NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
documentASTDocumentNodeThe parsed GraphQL document AST.
options?{ assumeValidSDL?: boolean }Optional configuration for this operation.

Returns

TypeDescription
GraphQLSchemaA new schema with the extensions and definitions applied.

Example

import { extendSchema } from 'graphql/utilities';
 
const result = extendSchema(schema, documentAST);
 
// result contains the extendSchema return value

lexicographicSortSchema()

Sort GraphQLSchema.

This function returns a sorted copy of the given GraphQLSchema.

Signature:

lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema

Arguments

NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.

Returns

TypeDescription
GraphQLSchemaA copy of the schema with types, fields, arguments, and values sorted lexicographically.

Example

import { lexicographicSortSchema } from 'graphql/utilities';
 
const result = lexicographicSortSchema(schema);
 
// result contains the lexicographicSortSchema return value

Types

BuildSchemaOptions

Interface. Options used when building a schema from SDL or a parsed SDL document.

Members

NameTypeDescription
assumeValidSDL?booleanSet to true to assume the SDL is valid.
Default: false