or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-extraction.mddirectives.mddocument-transformation.mdfragments.mdindex.mdstore-utilities.mdutility-functions.md
tile.json

directives.mddocs/

Directive Handling

Functions for working with GraphQL directives including conditional inclusion (@skip/@include), client-side directives (@client), and directive extraction.

Capabilities

Should Include

Determines whether a selection should be included based on @skip and @include directives and their variable values.

/**
 * Check if a selection should be included based on @skip/@include directives
 * @param selection - GraphQL selection node to check
 * @param variables - Variable values for directive evaluation
 * @returns true if selection should be included, false otherwise
 */
function shouldInclude(
  selection: SelectionNode,
  variables: { [name: string]: any } = {}
): boolean;

Usage Example:

import { shouldInclude } from "apollo-utilities";
import { parse } from "graphql";

const query = parse(`
  query GetUser($showEmail: Boolean!) {
    user {
      name
      email @include(if: $showEmail)
    }
  }
`);

const operation = getOperationDefinition(query);
const userField = operation.selectionSet.selections[0];
const emailField = userField.selectionSet.selections[1];

// Check if email field should be included
const variables = { showEmail: true };
const includeEmail = shouldInclude(emailField, variables);
console.log(includeEmail); // true

const variables2 = { showEmail: false };
const includeEmail2 = shouldInclude(emailField, variables2);
console.log(includeEmail2); // false

Get Directive Names

Extracts all directive names used in a GraphQL document.

/**
 * Get all directive names from a GraphQL document
 * @param doc - GraphQL document to analyze
 * @returns Array of directive names found in the document
 */
function getDirectiveNames(doc: DocumentNode): string[];

Usage Example:

import { getDirectiveNames } from "apollo-utilities";
import { parse } from "graphql";

const query = parse(`
  query GetUser {
    user @client {
      name @deprecated(reason: "Use fullName")
      email @include(if: true)
    }
  }
`);

const directives = getDirectiveNames(query);
console.log(directives); // ["client", "deprecated", "include"]

Has Directives

Checks if a document contains any of the specified directive names.

/**
 * Check if document contains specific directives
 * @param names - Array of directive names to check for
 * @param doc - GraphQL document to analyze
 * @returns true if any of the specified directives are found
 */
function hasDirectives(names: string[], doc: DocumentNode): boolean;

Usage Example:

import { hasDirectives } from "apollo-utilities";
import { parse } from "graphql";

const query = parse(`
  query GetUser {
    user @client {
      name
    }
  }
`);

const hasClient = hasDirectives(["client"], query);
console.log(hasClient); // true

const hasConnection = hasDirectives(["connection"], query);
console.log(hasConnection); // false

const hasAny = hasDirectives(["client", "connection"], query);
console.log(hasAny); // true (because it has "client")

Has Client Exports

Checks if a document has both @client and @export directives, indicating client-side state that should be exported.

/**
 * Check if document has both @client and @export directives
 * @param document - GraphQL document to analyze
 * @returns true if document has both @client and @export directives
 */
function hasClientExports(document: DocumentNode): boolean;

Get Directive Info From Field

Extracts directive information from a field node, converting directive arguments to an object representation.

/**
 * Extract directive information from a field
 * @param field - GraphQL field node to analyze
 * @param variables - Variable values for argument resolution
 * @returns Object mapping directive names to their arguments, or null if no directives
 */
function getDirectiveInfoFromField(
  field: FieldNode,
  variables: Object
): DirectiveInfo | null;

Usage Example:

import { getDirectiveInfoFromField } from "apollo-utilities";

// Assuming we have a field with directives like:
// user @connection(key: "users", filter: ["status"]) @client

const directiveInfo = getDirectiveInfoFromField(userField, {});
console.log(directiveInfo);
// {
//   connection: { key: "users", filter: ["status"] },
//   client: {}
// }

Get Inclusion Directives

Extracts @skip and @include directives from a list of directives, validating their structure.

/**
 * Get @skip and @include directives with their if arguments
 * @param directives - Array of directive nodes to filter
 * @returns Array of inclusion directives with their if arguments
 */
function getInclusionDirectives(
  directives: ReadonlyArray<DirectiveNode>
): InclusionDirectives;

Types

interface DirectiveInfo {
  [fieldName: string]: { [argName: string]: any };
}

type InclusionDirectives = Array<{
  directive: DirectiveNode;
  ifArgument: ArgumentNode;
}>;