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

ast-operations.mddocs/

AST Operations

AST conversion and parsing utilities for working with GraphQL abstract syntax trees, building operations, and document processing. These tools are essential for GraphQL schema generation, query building, and document transformation.

Capabilities

Type to AST Conversion

Convert GraphQL types to their AST representation.

/**
 * Convert a GraphQL type to its AST representation
 * @param type - The GraphQL type to convert
 * @returns AST type node representation
 */
function astFromType(type: GraphQLType): TypeNode;

Usage Examples:

import { astFromType } from "@graphql-tools/utils";
import { GraphQLString, GraphQLNonNull, GraphQLList } from "graphql";

// Convert scalar type
const stringAst = astFromType(GraphQLString);
// Result: { kind: 'NamedType', name: { kind: 'Name', value: 'String' } }

// Convert non-null type
const nonNullStringAst = astFromType(new GraphQLNonNull(GraphQLString));
// Result: { kind: 'NonNullType', type: { kind: 'NamedType', ... } }

// Convert list type
const listStringAst = astFromType(new GraphQLList(GraphQLString));
// Result: { kind: 'ListType', type: { kind: 'NamedType', ... } }

Value to AST Conversion

Convert JavaScript values to GraphQL AST value nodes.

/**
 * Convert a JavaScript value to a GraphQL AST value node
 * @param value - The value to convert
 * @param type - The GraphQL input type for validation and conversion
 * @returns AST value node or undefined if conversion fails
 */
function astFromValue(value: any, type: GraphQLInputType): ValueNode | undefined;

/**
 * Convert a value to AST without type information
 * @param value - The value to convert
 * @returns AST value node representation
 */
function astFromValueUntyped(value: any): ValueNode;

Usage Examples:

import { astFromValue, astFromValueUntyped } from "@graphql-tools/utils";
import { GraphQLString, GraphQLInt, GraphQLBoolean } from "graphql";

// Convert with type validation
const stringValueAst = astFromValue("hello", GraphQLString);
// Result: { kind: 'StringValue', value: 'hello' }

const intValueAst = astFromValue(42, GraphQLInt);
// Result: { kind: 'IntValue', value: '42' }

// Convert without type information
const untypedAst = astFromValueUntyped({ name: "John", age: 30 });
// Result: { kind: 'ObjectValue', fields: [...] }

// Handle arrays
const arrayAst = astFromValueUntyped([1, 2, 3]);
// Result: { kind: 'ListValue', values: [...] }

Operation Building

Build GraphQL operation AST nodes for specific fields.

/**
 * Build a GraphQL operation AST node for a specific field
 * @param options - Options for building the operation
 * @returns Complete operation definition node
 */
function buildOperationNodeForField(options: BuildOperationNodeOptions): OperationDefinitionNode;

interface BuildOperationNodeOptions {
  schema: GraphQLSchema;
  operationType: OperationTypeNode;
  field: string;
  models?: string[];
  ignore?: string[];
  depthLimit?: number;
  circularReferenceDepth?: number;
  argNames?: string[];
  selectedFields?: SelectedFields;
}

type SelectedFields = {
  [key: string]: SelectedFields | boolean;
};

Usage Examples:

import { buildOperationNodeForField } from "@graphql-tools/utils";
import { OperationTypeNode } from "graphql";

// Build a query operation for a specific field
const queryOperation = buildOperationNodeForField({
  schema: mySchema,
  operationType: OperationTypeNode.QUERY,
  field: 'user',
  argNames: ['id'],
  depthLimit: 3,
  selectedFields: {
    id: true,
    name: true,
    posts: {
      title: true,
      content: true
    }
  }
});

// Build a mutation operation
const mutationOperation = buildOperationNodeForField({
  schema: mySchema,
  operationType: OperationTypeNode.MUTATION,
  field: 'createUser',
  argNames: ['input'],
  selectedFields: {
    id: true,
    name: true,
    email: true
  }
});

GraphQL Parsing

Parse GraphQL Schema Definition Language and JSON representations.

/**
 * Parse GraphQL Schema Definition Language
 * @param location - Source location identifier
 * @param rawSDL - Raw SDL string to parse
 * @param options - Parsing options
 * @returns GraphQL Source object
 */
function parseGraphQLSDL(location: string, rawSDL: string, options?: ParseOptions): Source;

/**
 * Parse GraphQL schema from JSON representation
 * @param location - Source location identifier  
 * @param jsonSchema - JSON schema object
 * @param options - Parsing options
 * @returns GraphQL Source object
 */
function parseGraphQLJSON(location: string, jsonSchema: any, options?: ParseOptions): Source;

interface ParseOptions {
  noLocation?: boolean;
  allowLegacySDLEmptyFields?: boolean;
  allowLegacySDLImplementsInterfaces?: boolean;
  experimentalFragmentVariables?: boolean;
}

Usage Examples:

import { parseGraphQLSDL, parseGraphQLJSON } from "@graphql-tools/utils";

// Parse SDL string
const sdlSource = parseGraphQLSDL('schema.graphql', `
  type Query {
    hello: String
  }
`);

// Parse from JSON introspection result
const jsonSource = parseGraphQLJSON('schema.json', {
  __schema: {
    types: [
      // ... introspection result
    ]
  }
});

// Parse with options
const sourceWithOptions = parseGraphQLSDL('schema.graphql', sdl, {
  noLocation: true,
  allowLegacySDLEmptyFields: true
});

Schema AST Repair

Fix common issues in GraphQL schema AST definitions.

/**
 * Fix common issues in GraphQL schema AST definitions
 * @param schema - The schema to fix
 * @param options - Repair options
 * @returns Schema with fixed AST issues
 */
function fixSchemaAst(schema: GraphQLSchema, options?: FixSchemaAstOptions): GraphQLSchema;

interface FixSchemaAstOptions {
  assumeValid?: boolean;
  assumeValidSDL?: boolean;
}

Operation Extraction

Extract operation AST from execution requests.

/**
 * Extract operation AST from a GraphQL execution request
 * @param request - The execution request
 * @returns Operation definition node and operation name
 */
function getOperationASTFromRequest(request: ExecutionRequest): {
  operation: OperationDefinitionNode;
  operationName: string | undefined;
};

Usage Examples:

import { getOperationASTFromRequest } from "@graphql-tools/utils";

// Extract operation from request
const request = {
  document: parse(`
    query GetUser($id: ID!) {
      user(id: $id) {
        name
        email
      }
    }
  `),
  variables: { id: "123" },
  operationName: "GetUser"
};

const { operation, operationName } = getOperationASTFromRequest(request);
// operation: OperationDefinitionNode for the GetUser query
// operationName: "GetUser"

Document Validation

Validate GraphQL documents against a schema.

/**
 * Validate GraphQL documents against a schema
 * @param schema - The schema to validate against
 * @param documents - Array of document sources to validate
 * @param options - Validation options
 * @returns Array of validation errors
 */
function validateGraphQlDocuments(
  schema: GraphQLSchema,
  documents: Source[],
  options?: ValidationOptions
): readonly GraphQLError[];

interface ValidationOptions {
  maxDepth?: number;
  costAnalysis?: {
    maximumCost: number;
    defaultCost?: number;
    scalarCost?: number;
    objectCost?: number;
    listFactor?: number;
    introspectionCost?: number;
  };
  rules?: ValidationRule[];
}

interface LoadDocumentError {
  readonly message: string;
  readonly location: string;
}

Usage Examples:

import { validateGraphQlDocuments, parseGraphQLSDL } from "@graphql-tools/utils";

// Validate documents
const documents = [
  parseGraphQLSDL('query1.graphql', 'query { user { name } }'),
  parseGraphQLSDL('query2.graphql', 'query { invalidField }')
];

const errors = validateGraphQlDocuments(schema, documents, {
  maxDepth: 10,
  costAnalysis: {
    maximumCost: 1000,
    defaultCost: 1
  }
});

// Check for validation errors
if (errors.length > 0) {
  console.log('Validation errors found:', errors);
}