The foundational ApolloLink class and composition functions for building and connecting GraphQL transport chains.
Main class for creating custom GraphQL transport links that can be composed into chains.
/**
* Main class for creating custom GraphQL transport links
*/
class ApolloLink {
/** Creates an empty link that returns empty observable */
static empty(): ApolloLink;
/** Combines multiple links into a single link chain */
static from(links: ApolloLink[]): ApolloLink;
/** Creates conditional routing between two links based on test function */
static split(test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler): ApolloLink;
/** Executes a GraphQL operation through a link */
static execute(link: ApolloLink, operation: GraphQLRequest): Observable<FetchResult>;
/**
* Creates a new ApolloLink instance
* @param request - Optional request handler function
*/
constructor(request?: RequestHandler);
/**
* Processes a GraphQL operation (must be implemented by subclasses)
* @param operation - The GraphQL operation to process
* @param forward - Function to forward operation to next link (for non-terminating links)
* @returns Observable of fetch results or null
*/
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
/**
* Creates a conditional split, forwarding to different links based on test
* @param test - Function to determine which link to use
* @param left - Link to use when test returns true
* @param right - Link to use when test returns false (defaults to passthrough)
* @returns New ApolloLink with conditional routing
*/
split(test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | RequestHandler): ApolloLink;
/**
* Chains this link with another link
* @param next - Link to chain after this one
* @returns New ApolloLink representing the chain
*/
concat(next: ApolloLink | RequestHandler): ApolloLink;
}Usage Examples:
import { ApolloLink, Observable } from "apollo-link";
// Create a custom terminating link
class HttpLink extends ApolloLink {
constructor(private uri: string) {
super();
}
request(operation: Operation) {
return new Observable(observer => {
fetch(this.uri, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: operation.query,
variables: operation.variables,
operationName: operation.operationName
})
})
.then(response => response.json())
.then(result => {
observer.next(result);
observer.complete();
})
.catch(observer.error);
});
}
}
// Create a non-terminating link that adds authentication
class AuthLink extends ApolloLink {
constructor(private getToken: () => string) {
super();
}
request(operation: Operation, forward: NextLink) {
operation.setContext({
headers: {
Authorization: `Bearer ${this.getToken()}`
}
});
return forward(operation);
}
}Executes a GraphQL operation through a link chain.
/**
* Executes a GraphQL operation through a link chain
* @param link - The link chain to execute through
* @param operation - The GraphQL request to execute
* @returns Observable of fetch results
*/
function execute(link: ApolloLink, operation: GraphQLRequest): Observable<FetchResult>;Usage Example:
import { execute, ApolloLink } from "apollo-link";
import gql from "graphql-tag";
const link = new ApolloLink(/* implementation */);
const query = gql`query { users { id name } }`;
execute(link, { query }).subscribe({
next: result => console.log(result.data),
error: err => console.error(err)
});Combines multiple links into a single link chain by reducing them sequentially.
/**
* Combines multiple links into a single link chain
* @param links - Array of links to combine
* @returns Single ApolloLink representing the chain
*/
function from(links: ApolloLink[]): ApolloLink;Usage Example:
import { from } from "apollo-link";
const authLink = new AuthLink();
const httpLink = new HttpLink("/graphql");
const errorLink = new ErrorLink();
// Chain links: errorLink -> authLink -> httpLink
const link = from([errorLink, authLink, httpLink]);Creates conditional routing between two links based on a test function.
/**
* Creates conditional routing between two links based on test function
* @param test - Function that determines which link to use
* @param left - Link to use when test returns true
* @param right - Link to use when test returns false (optional)
* @returns ApolloLink with conditional routing
*/
function split(
test: (op: Operation) => boolean,
left: ApolloLink | RequestHandler,
right?: ApolloLink | RequestHandler
): ApolloLink;Usage Example:
import { split } from "apollo-link";
const httpLink = new HttpLink("/graphql");
const wsLink = new WebSocketLink({ uri: "ws://localhost:4000/graphql" });
// Route subscriptions to WebSocket, queries/mutations to HTTP
const link = split(
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === "OperationDefinition" && definition.operation === "subscription";
},
wsLink,
httpLink
);Joins two links together sequentially.
/**
* Joins two links together sequentially
* @param first - The first link in the chain
* @param second - The second link in the chain
* @returns ApolloLink representing the concatenated chain
*/
function concat(
first: ApolloLink | RequestHandler,
second: ApolloLink | RequestHandler
): ApolloLink;Usage Example:
import { concat } from "apollo-link";
const authLink = new AuthLink();
const httpLink = new HttpLink("/graphql");
// Chain auth link before HTTP link
const link = concat(authLink, httpLink);Creates an empty link that returns an empty observable.
/**
* Creates an empty link that returns an empty observable
* @returns ApolloLink that emits no results
*/
function empty(): ApolloLink;Usage Example:
import { empty } from "apollo-link";
// Useful as default for optional links
const fallbackLink = empty();