v17 APIgraphql/utilities

graphql/utilities

The graphql/utilities entry point contains helpers for introspection, building and printing schemas, AST and value conversion, schema coordinates, schema comparison, type-aware visitors, and input coercion.

import { buildSchema, printSchema, coerceInputValue } from 'graphql/utilities';

The same exports are also available from the root graphql entry point.

Contents

Introspection Exports

ExportKindDescription
getIntrospectionQuery()FunctionBuilds the standard introspection query string with configurable options.
introspectionFromSchema()FunctionExecutes introspection against a schema object and returns introspection data.
buildClientSchema()FunctionBuilds a non-executable schema from introspection data.
Type export
IntrospectionOptions
IntrospectionQuery
IntrospectionSchema
IntrospectionType
IntrospectionInputType
IntrospectionOutputType
IntrospectionScalarType
IntrospectionObjectType
IntrospectionInterfaceType
IntrospectionUnionType
IntrospectionEnumType
IntrospectionInputObjectType
IntrospectionTypeRef
IntrospectionInputTypeRef
IntrospectionOutputTypeRef
IntrospectionNamedTypeRef
IntrospectionListTypeRef
IntrospectionNonNullTypeRef
IntrospectionField
IntrospectionInputValue
IntrospectionEnumValue
IntrospectionDirective
const query = getIntrospectionQuery({ descriptions: true });
const data = await graphql({ schema, source: query });
const clientSchema = buildClientSchema(data.data);

Schema Build, Extend, Sort, and Print Exports

ExportKindDescription
buildSchema()FunctionBuilds a schema from SDL source text.
buildASTSchema()FunctionBuilds a schema from a parsed SDL document.
BuildSchemaOptionsTypeOptions for building schemas from SDL.
extendSchema()FunctionReturns a schema extended with SDL definitions.
lexicographicSortSchema()FunctionReturns a copy of a schema sorted by name.
printSchema()FunctionPrints a schema as SDL.
printType()FunctionPrints one named type as SDL.
printDirective() New in v17FunctionPrints one directive definition as SDL.
printIntrospectionSchema()FunctionPrints the built-in introspection schema.
const schema = buildSchema(`
  type Query {
    hello: String
  }
`);
 
console.log(printSchema(lexicographicSortSchema(schema)));

AST, Operation, and Type Utility Exports

ExportKindDescription
getOperationAST()FunctionGets the named operation from a document, or the only operation when no name is provided.
typeFromAST()FunctionResolves a GraphQL type reference AST against a schema.
concatAST()FunctionConcatenates multiple documents into one document.
separateOperations()FunctionSplits a document into one document per operation.
stripIgnoredCharacters()FunctionRemoves insignificant characters from a GraphQL document string.
TypeInfo Changed in v17ClassTracks current output type, parent type, input type, field, directive, argument, enum value, and default while visiting an AST.
visitWithTypeInfo()FunctionWraps a visitor so it maintains a TypeInfo instance.
TypedQueryDocumentNodeTypeDocumentNode wrapper type carrying inferred result and variable types.
const typeInfo = new TypeInfo(schema);
 
visit(
  document,
  visitWithTypeInfo(typeInfo, {
    Field() {
      console.log(typeInfo.getType()?.toString());
    },
  }),
);

Input Coercion and Literal Conversion Exports

v17 splits coercion from diagnostics. coerceInputValue() and coerceInputLiteral() return a value or undefined, while validateInputValue() and validateInputLiteral() report detailed errors. valueFromAST() and astFromValue() remain for compatibility and are deprecated for removal in v18.

ExportKindDescription
coerceInputValue() Changed in v17FunctionCoerces a JavaScript value for a GraphQL input type, returning undefined on failure.
validateInputValue() New in v17FunctionValidates a JavaScript value and reports every diagnostic through a callback.
coerceInputLiteral() New in v17FunctionCoerces a GraphQL value AST for a GraphQL input type, returning undefined on failure.
validateInputLiteral() New in v17FunctionValidates a GraphQL value AST and reports diagnostics through a callback.
valueToLiteral() New in v17FunctionConverts an external JavaScript input value to a GraphQL const value AST.
replaceVariables() New in v17FunctionReplaces variable nodes in a value AST with literal values from coerced variable maps.
valueFromAST() Deprecated in v17FunctionDeprecated literal coercion helper. Use coerceInputLiteral().
valueFromASTUntyped()FunctionConverts a value AST to an untyped JavaScript value.
astFromValue() Deprecated in v17FunctionDeprecated value-to-AST helper. Use valueToLiteral().
const errors = [];
validateInputValue(rawValue, inputType, (error, path) => {
  errors.push({ message: error.message, path });
});
 
if (errors.length === 0) {
  const value = coerceInputValue(rawValue, inputType);
}

Type Comparator Exports

ExportKindDescription
isEqualType()FunctionChecks whether two GraphQL types are exactly equal.
isTypeSubTypeOf()FunctionChecks whether one type is a valid subtype of another in a schema.
doTypesOverlap()FunctionChecks whether two composite types can apply to the same runtime object.

Schema Coordinate Exports

ExportKindDescription
resolveSchemaCoordinate()FunctionResolves a schema coordinate string against a schema.
resolveASTSchemaCoordinate()FunctionResolves a parsed schema coordinate AST against a schema.
ResolvedSchemaElementTypeUnion of named type, field, input field, enum value, argument, directive, and directive argument results.
const element = resolveSchemaCoordinate(schema, 'User.name');
 
if (element.kind === 'FIELD') {
  console.log(element.field.type.toString());
}

Schema Change Exports

findSchemaChanges() is the v17 schema comparison API and returns breaking, dangerous, and safe changes in one call. Legacy wrappers remain available for compatibility and are deprecated for removal in v18.

ExportKindDescription
findSchemaChanges() New in v17FunctionReturns breaking, dangerous, and safe schema changes.
findBreakingChanges() Deprecated in v17FunctionDeprecated wrapper returning only breaking changes.
findDangerousChanges() Deprecated in v17FunctionDeprecated wrapper returning only dangerous changes.
BreakingChangeTypeConst object and typeBreaking schema change categories.
DangerousChangeTypeConst object and typePotentially dangerous schema change categories.
SafeChangeType New in v17Const object and typeSafe schema change categories.
BreakingChangeTypeBreaking change result shape.
DangerousChangeTypeDangerous change result shape.
SafeChange New in v17TypeSafe change result shape.
const changes = findSchemaChanges(oldSchema, newSchema);
 
for (const change of changes) {
  console.log(change.type, change.description);
}

Operation Root and Name Helper Exports

These helpers remain available in v17 for compatibility and are deprecated in favor of schema.getRootType(operation) and assertName().

ExportKindDescription
getOperationRootType() Deprecated in v17FunctionDeprecated operation root lookup helper. Use schema.getRootType(operation).
assertValidName() Deprecated in v17FunctionDeprecated name assertion helper. Use assertName() from graphql/type.
isValidNameError() Deprecated in v17FunctionDeprecated name validation helper. Use assertName() with normal error handling.
const rootType = getOperationRootType(schema, operation);
const safeName = assertValidName(candidateName);