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.
npm install apollo-linkimport { 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");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)
});Apollo Link is built around several key architectural concepts:
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;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;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;
}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[];
}