graphql/type/validation

Functions

validateSchema()

Implements the “Type Validation” sub-sections of the specification’s “Type System” section.

Validation runs synchronously, returning an array of encountered errors, or an empty array if no errors were encountered and the Schema is valid.

Signature:

validateSchema(schema: GraphQLSchema): readonly GraphQLError[]

Arguments

NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.

Returns

TypeDescription
readonly GraphQLError[]Schema validation errors, or an empty array when the schema is valid.

Example

import { GraphQLObjectType, GraphQLSchema, GraphQLString, validateSchema } from 'graphql/type';
 
const schema = new GraphQLSchema({
  query: new GraphQLObjectType({ name: 'Query', fields: { name: { type: GraphQLString } } }),
});
const errors = validateSchema(schema);
 
// errors.length: 0

assertValidSchema()

Utility function which asserts a schema is valid by throwing an error if it is invalid.

Signature:

assertValidSchema(schema: GraphQLSchema): void

Arguments

NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.

Example

import { GraphQLObjectType, GraphQLSchema, GraphQLString, assertValidSchema } from 'graphql/type';
 
const schema = new GraphQLSchema({
  query: new GraphQLObjectType({ name: 'Query', fields: { name: { type: GraphQLString } } }),
});
assertValidSchema(schema);
 
// does not throw for a valid schema