or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-operations.mddirectives.mdexecution-resolution.mdfield-type-operations.mdindex.mdschema-transformation.mdutilities.md
tile.json

tessl/npm-graphql-tools--utils

Common package containing utils and types for GraphQL tools

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@graphql-tools/utils@10.9.x

To install, run

npx @tessl/cli install tessl/npm-graphql-tools--utils@10.9.0

index.mddocs/

GraphQL Tools Utils

GraphQL 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.

Package Information

  • Package Name: @graphql-tools/utils
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @graphql-tools/utils
  • GraphQL Compatibility: GraphQL 14-17

Core Imports

import { 
  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");

Basic Usage

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
);

Architecture

GraphQL Tools Utils is built around several key components:

  • Schema Transformation: Core utilities for modifying, filtering, and healing schemas
  • AST Operations: Functions for converting between GraphQL types, values, and AST representations
  • Field & Type Operations: Tools for field collection, iteration, and type relationship analysis
  • Directives: Comprehensive directive extraction and extension handling
  • Execution & Resolution: Execution request/result types and resolver management interfaces
  • Utilities: Helper functions for memoization, async handling, validation, and data structures

Capabilities

Schema Transformation

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;

Schema Transformation

AST Operations

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;

AST Operations

Field & Type Operations

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[];

Field & Type Operations

Directives

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[]>>;

Directives

Execution & Resolution

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>>;

Execution & Resolution

Utilities

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;

Utilities