Common package containing utils and types for GraphQL tools
npx @tessl/cli install tessl/npm-graphql-tools--utils@10.9.0GraphQL Tools Utils provides a comprehensive collection of utilities and type definitions for GraphQL development, offering tools for schema manipulation, field collection, AST transformation, and resolver management. It serves as a foundational utility library for the GraphQL Tools ecosystem, providing essential building blocks for schema generation, schema stitching, mocking, and other GraphQL operations.
npm install @graphql-tools/utilsimport {
mapSchema,
filterSchema,
healSchema,
collectFields,
getDirectives,
buildOperationNodeForField,
astFromType,
astFromValue,
memoize1,
mergeDeep,
isAsyncIterable,
inspect,
type IResolvers,
type ExecutionRequest,
type SchemaMapper,
type MapperKind
} from "@graphql-tools/utils";For CommonJS:
const {
mapSchema,
filterSchema,
healSchema,
collectFields,
getDirectives,
buildOperationNodeForField,
astFromType,
astFromValue,
memoize1,
mergeDeep,
isAsyncIterable,
inspect
} = require("@graphql-tools/utils");import {
mapSchema,
MapperKind,
filterSchema,
collectFields
} from "@graphql-tools/utils";
import { buildSchema } from "graphql";
// Schema transformation
const schema = buildSchema(`
type Query {
hello: String
world: String
}
`);
// Transform schema by adding logging to resolvers
const transformedSchema = mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const originalResolve = fieldConfig.resolve || ((source) => source[fieldConfig.name]);
fieldConfig.resolve = async (source, args, context, info) => {
console.log(`Resolving ${info.fieldName}`);
return originalResolve(source, args, context, info);
};
return fieldConfig;
}
});
// Filter schema to remove specific fields
const filteredSchema = filterSchema({
schema,
rootFieldFilter: (operationName, fieldName) => fieldName !== 'world'
});
// Collect fields from selection set during execution
const collectedFields = collectFields(
schema,
fragments,
variableValues,
runtimeType,
selectionSet
);GraphQL Tools Utils is built around several key components:
Core schema transformation utilities for filtering, mapping, healing, and modifying GraphQL schemas. Essential for schema stitching, federation, and schema evolution.
function mapSchema(schema: GraphQLSchema, schemaMapper: SchemaMapper): GraphQLSchema;
function filterSchema(options: FilterSchemaOptions): GraphQLSchema;
function healSchema(schema: GraphQLSchema): GraphQLSchema;
function pruneSchema(schema: GraphQLSchema, options?: PruneSchemaOptions): GraphQLSchema;AST conversion and parsing utilities for working with GraphQL abstract syntax trees, building operations, and document processing.
function astFromType(type: GraphQLType): TypeNode;
function astFromValue(value: any, type: GraphQLInputType): ValueNode | undefined;
function buildOperationNodeForField(options: BuildOperationNodeOptions): OperationDefinitionNode;
function parseGraphQLSDL(location: string, rawSDL: string, options?: ParseOptions): Source;Field collection, iteration, and type manipulation utilities for analyzing GraphQL schemas and execution contexts.
function collectFields(
schema: GraphQLSchema,
fragments: Record<string, FragmentDefinitionNode>,
variableValues: Record<string, any>,
runtimeType: GraphQLObjectType,
selectionSet: SelectionSetNode
): FieldsAndPatches;
function forEachField(schema: GraphQLSchema, fn: FieldIteratorFn): void;
function getImplementingTypes(abstractType: GraphQLAbstractType, schema: GraphQLSchema): GraphQLObjectType[];Directive extraction, extension handling, and built-in directive support for GraphQL schema processing.
function getDirectives(
schema: GraphQLSchema,
node: ASTNode
): DirectiveUsage[];
function getDirective(
schema: GraphQLSchema,
node: ASTNode,
directiveName: string
): DirectiveUsage | undefined;
function getFieldsWithDirectives(schema: GraphQLSchema): Record<string, Record<string, DirectiveUsage[]>>;Execution framework types, resolver interfaces, and validation utilities for GraphQL execution and resolution.
interface ExecutionRequest<TVariables = any, TContext = any, TRootValue = any, TExtensions = any> {
document: DocumentNode;
variables?: TVariables;
operationName?: string;
rootValue?: TRootValue;
context?: TContext;
extensions?: TExtensions;
}
interface IResolvers<TSource = any, TContext = any, TArgs = any> {
[typeName: string]: {
[fieldName: string]: IFieldResolver<TSource, TContext, TArgs> | IResolverOptions<TSource, TContext, TArgs>;
} | GraphQLScalarType | IEnumResolver;
}
type Executor<TBaseContext = any, TBaseExtensions = any> = <
TReturn = any,
TArgs = any,
TContext = TBaseContext,
TRoot = any,
TExtensions = TBaseExtensions
>(
request: ExecutionRequest<TArgs, TContext, TRoot, TExtensions>
) => PromiseOrValue<ExecutionResult<TReturn>>;Helper functions for memoization, async handling, type guards, data structures, and performance monitoring.
function memoize1<TArgs, TReturn>(fn: (a1: TArgs) => TReturn): (a1: TArgs) => TReturn;
function mergeDeep<T>(sources: Partial<T>[]): T;
function isAsyncIterable(value: any): value is AsyncIterable<unknown>;
class AccumulatorMap<T> extends Map<string, T[]> {
add(key: string, item: T): void;
}
function debugTimer(label: string): () => void;