or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdlink-operations.mdtesting.mdutilities.md
tile.json

index.mddocs/

Apollo Link

Apollo Link is a flexible, lightweight transport layer for GraphQL that provides a standard interface for modifying control flow of GraphQL requests and fetching GraphQL results. It enables composable links that can be chained together to handle different aspects of GraphQL operations such as fetching, caching, error handling, and request transformation.

Package Information

  • Package Name: apollo-link
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install apollo-link

Core Imports

import { ApolloLink, execute, Observable } from "apollo-link";

For utilities and composition:

import { 
  ApolloLink, 
  from, 
  split, 
  concat,
  empty,
  execute,
  toPromise,
  fromPromise,
  fromError,
  createOperation,
  getOperationName
} from "apollo-link";

CommonJS:

const { ApolloLink, execute, Observable } = require("apollo-link");

Basic Usage

import { ApolloLink, execute, Observable } from "apollo-link";
import gql from "graphql-tag";

// Create a simple terminating link
const httpLink = new ApolloLink(operation => {
  return new Observable(observer => {
    fetch("/graphql", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        query: operation.query,
        variables: operation.variables,
      })
    })
    .then(response => response.json())
    .then(result => {
      observer.next(result);
      observer.complete();
    })
    .catch(observer.error);
  });
});

// Execute a GraphQL operation
const query = gql`
  query GetUser($id: ID!) {
    user(id: $id) {
      name
      email
    }
  }
`;

execute(httpLink, { query, variables: { id: "123" } })
  .subscribe({
    next: result => console.log(result),
    error: err => console.error(err)
  });

Architecture

Apollo Link is built around several key architectural concepts:

  • Link Chain: Links can be composed into chains where each link processes the operation and optionally forwards it to the next link
  • Observable Pattern: All responses are handled as Observables, supporting both single responses and streaming data (subscriptions)
  • Terminating vs Non-terminating Links: Terminating links produce results, while non-terminating links modify operations and forward them
  • Operation Context: Each operation carries context that can be modified by links in the chain
  • Request/Forward Pattern: Non-terminating links receive both the operation and a forward function to pass the operation to the next link

Capabilities

Core Link Operations

The foundational ApolloLink class and composition functions for building and connecting GraphQL transport chains.

class ApolloLink {
  constructor(request?: RequestHandler);
  request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
  split(test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler): ApolloLink;
  concat(next: ApolloLink | RequestHandler): ApolloLink;
}

function execute(link: ApolloLink, operation: GraphQLRequest): Observable<FetchResult>;
function from(links: ApolloLink[]): ApolloLink;
function split(test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler): ApolloLink;

Core Link Operations

Utilities

Helper functions for operation processing, promise/observable conversion, and link validation.

function toPromise<R>(observable: Observable<R>): Promise<R>;
function fromPromise<T>(promise: Promise<T>): Observable<T>;
function fromError<T>(errorValue: any): Observable<T>;
function createOperation(starting: any, operation: GraphQLRequest): Operation;
function getOperationName(query: DocumentNode): string | null;

Utilities

Testing

Test utilities including mock implementations and testing helpers for validating link behavior.

class MockLink extends ApolloLink {
  constructor(handleRequest?: RequestHandler);
}

function testLinkResults(params: TestResultType): void;

interface TestResultType {
  link: ApolloLink;
  results?: any[];
  query?: string;
  done?: () => void;
  context?: any;
  variables?: any;
}

Testing

Types

interface GraphQLRequest {
  query: DocumentNode;
  variables?: Record<string, any>;
  operationName?: string;
  context?: Record<string, any>;
  extensions?: Record<string, any>;
}

interface Operation {
  query: DocumentNode;
  variables: Record<string, any>;
  operationName: string;
  extensions: Record<string, any>;
  setContext: (context: Record<string, any>) => Record<string, any>;
  getContext: () => Record<string, any>;
  toKey: () => string;
}

type FetchResult<TData = { [key: string]: any }, C = Record<string, any>, E = Record<string, any>> = ExecutionResult<TData> & {
  extensions?: E;
  context?: C;
};

type NextLink = (operation: Operation) => Observable<FetchResult>;
type RequestHandler = (operation: Operation, forward: NextLink) => Observable<FetchResult> | null;

interface ExecutionResult<TData = { [key: string]: any }> {
  data?: TData | null;
  errors?: any[];
}