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
| Name | Type | Description |
|---|---|---|
| documentAST | DocumentNode | The parsed GraphQL document AST. |
| options? | BuildSchemaOptions | Optional configuration for this operation. |
Returns
| Type | Description |
|---|---|
GraphQLSchema | The 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
| Name | Type | Description |
|---|---|---|
| source | string | Source | The GraphQL source text or source object. |
| options? | BuildSchemaOptions & ParseOptions | Optional configuration for this operation. |
Returns
| Type | Description |
|---|---|
GraphQLSchema | The 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
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | The GraphQL schema to use. |
| documentAST | DocumentNode | The parsed GraphQL document AST. |
| options? | { assumeValidSDL?: boolean } | Optional configuration for this operation. |
Returns
| Type | Description |
|---|---|
GraphQLSchema | A new schema with the extensions and definitions applied. |
Example
import { extendSchema } from 'graphql/utilities';
const result = extendSchema(schema, documentAST);
// result contains the extendSchema return valuelexicographicSortSchema()
Sort GraphQLSchema.
This function returns a sorted copy of the given GraphQLSchema.
Signature:
lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema
Arguments
| Name | Type | Description |
|---|---|---|
| schema | GraphQLSchema | The GraphQL schema to use. |
Returns
| Type | Description |
|---|---|
GraphQLSchema | A 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 valueTypes
BuildSchemaOptions
Interface. Options used when building a schema from SDL or a parsed SDL document.
Members
| Name | Type | Description |
|---|---|---|
| assumeValidSDL? | boolean | Set to true to assume the SDL is valid. Default: false |