ApolloLink

API reference


The base class for all links in Apollo Client. A link represents either a self-contained modification to a GraphQL operation or a side effect (such as logging).

Links enable you to customize Apollo Client's request flow by composing together different pieces of functionality into a chain of links. Each link represents a specific capability, such as adding authentication headers, retrying failed requests, batching operations, or sending requests to a GraphQL server.

Every link must define a request handler via its constructor or by extending this class and implementing the request method.

TypeScript
1 import { ApolloLink } from "@apollo/client";
2
3 const link = new ApolloLink((operation, forward) => {
4   console.log("Operation:", operation.operationName);
5   return forward(operation);
6 });

Constructor signature

TypeScript
1constructor(
2  request?: ApolloLink.RequestHandler
3): ApolloLink

Static methods

Composes multiple links into a single composed link that executes each provided link in serial order.

Example

TypeScript
1 import { from, HttpLink, ApolloLink } from "@apollo/client";
2 import { RetryLink } from "@apollo/client/link/retry";
3 import MyAuthLink from "../auth";
4
5 const link = ApolloLink.from([
6   new RetryLink(),
7   new MyAuthLink(),
8   new HttpLink({ uri: "http://localhost:4000/graphql" }),
9 ]);

Signature

TypeScript
1from(
2  links: ApolloLink[]
3): ApolloLink

Parameters

Name / Type
Description
links
ApolloLink[]

An array of ApolloLink instances or request handlers that are executed in serial order.

Creates a link that conditionally routes a request to different links.

Example

TypeScript
1 import { ApolloLink, HttpLink } from "@apollo/client";
2
3 const link = ApolloLink.split(
4   (operation) => operation.getContext().version === 1,
5   new HttpLink({ uri: "http://localhost:4000/v1/graphql" }),
6   new HttpLink({ uri: "http://localhost:4000/v2/graphql" })
7 );

Signature

TypeScript
1split(
2  test: (op: ApolloLink.Operation) => boolean,
3  left: ApolloLink,
4  right?: ApolloLink
5): ApolloLink

Parameters

Name / Type
Description
test
(op: ApolloLink.Operation) => boolean

A predicate function that receives the current operation and returns a boolean indicating which link to execute. Returning true executes the left link. Returning false executes the right link.

left
ApolloLink

The link that executes when the test function returns true.

The link that executes when the test function returns false. If the right link is not provided, the request is forwarded to the next link in the chain.

Executes a GraphQL request against a link. The execute function begins the request by calling the request handler of the link.

Example

TypeScript
1 const observable = ApolloLink.execute(link, { query, variables }, { client });
2
3 observable.subscribe({
4   next(value) {
5     console.log("Received", value);
6   },
7   error(error) {
8     console.error("Oops got error", error);
9   },
10   complete() {
11     console.log("Request complete");
12   },
13 });

Signature

TypeScript
1execute(
2  link: ApolloLink,
3  request: ApolloLink.Request,
4  context: ApolloLink.ExecuteContext
5): Observable<ApolloLink.Result>

Parameters

Name / Type
Description
link
ApolloLink

The ApolloLink instance to execute the request.

request
ApolloLink.Request

The GraphQL request details, such as the query and variables.

Show/hide child attributes
DefaultContext

Context provided to the link chain. Context is not sent to the server and is used to communicate additional metadata from a request to individual links in the link chain.

Record<string, any>

A map of extensions that will be sent with the GraphQL request to the server.

DocumentNode

The parsed GraphQL document that will be sent with the GraphQL request to the server.

OperationVariables

The variables provided for the query.

context
ApolloLink.ExecuteContext

The execution context for the request, such as the client making the request.

Show/hide child attributes
ApolloClient

The Apollo Client instance that executed the GraphQL request.

Creates a link that completes immediately and does not emit a result.

Example

TypeScript
1 const link = ApolloLink.empty();

Signature

TypeScript
1empty(): ApolloLink

⚠️ Deprecated

Use ApolloLink.from instead. ApolloLink.concat will be removed in a future major version.

Combines multiple links into a single composed link.

Example

TypeScript
1 const link = ApolloLink.concat(firstLink, secondLink, thirdLink);

Signature

TypeScript
1concat(
2  links: ApolloLink[]
3): ApolloLink

Parameters

Name / Type
Description
links
ApolloLink[]

The links to concatenate into a single link. Each link will execute in serial order.

Instance methods

Combines the link with other links into a single composed link.

Example

TypeScript
1 import { ApolloLink, HttpLink } from "@apollo/client";
2
3 const previousLink = new ApolloLink((operation, forward) => {
4   // Handle the request
5
6   return forward(operation);
7 });
8
9 const link = previousLink.concat(
10   link1,
11   link2,
12   new HttpLink({ uri: "http://localhost:4000/graphql" })
13 );

Signature

TypeScript
1concat(
2  links: ApolloLink[]
3): ApolloLink

Parameters

Name / Type
Description
links
ApolloLink[]

Concatenates a link that conditionally routes a request to different links.

Example

TypeScript
1 import { ApolloLink, HttpLink } from "@apollo/client";
2
3 const previousLink = new ApolloLink((operation, forward) => {
4   // Handle the request
5
6   return forward(operation);
7 });
8
9 const link = previousLink.split(
10   (operation) => operation.getContext().version === 1,
11   new HttpLink({ uri: "http://localhost:4000/v1/graphql" }),
12   new HttpLink({ uri: "http://localhost:4000/v2/graphql" })
13 );

Signature

TypeScript
1split(
2  test: (op: ApolloLink.Operation) => boolean,
3  left: ApolloLink,
4  right?: ApolloLink
5): ApolloLink

Parameters

Name / Type
Description
test
(op: ApolloLink.Operation) => boolean

A predicate function that receives the current operation and returns a boolean indicating which link to execute. Returning true executes the left link. Returning false executes the right link.

left
ApolloLink

The link that executes when the test function returns true.

The link that executes when the test function returns false. If the right link is not provided, the request is forwarded to the next link in the chain.

Types

Context provided for link execution, such as the client executing the request. It is separate from the request operation context.

Properties
Name / Type
Description
ApolloClient

The Apollo Client instance that executed the GraphQL request.

A function that when called will execute the next link in the link chain.

Example

TypeScript
1 const link = new ApolloLink((operation, forward) => {
2   // process the request
3
4   // Call `forward` to execute the next link in the chain
5   return forward(operation);
6 });

Signature

TypeScript
1ForwardFunction(
2  operation: ApolloLink.Operation
3): Observable<ApolloLink.Result>

Parameters

Name / Type
Description
operation
ApolloLink.Operation

The current ApolloLink.Operation object for the request.

Show/hide child attributes
ApolloClient

The Apollo Client instance executing the request.

Record<string, any>

A map that stores extensions data to be sent to the server.

() => Readonly<ApolloLink.OperationContext>

A function that gets the current context of the request. This can be used by links to determine which actions to perform. See managing context

string | undefined

The string name of the GraphQL operation. If it is anonymous, operationName will be undefined.

OperationTypeNode

The type of the GraphQL operation, such as query or mutation.

DocumentNode

A DocumentNode that describes the operation taking place.

{ (context: Partial<ApolloLink.OperationContext>): void; (updateContext: (previousContext: Readonly<ApolloLink.OperationContext>) => Partial<ApolloLink.OperationContext>): void; }

A function that takes either a new context object, or a function which takes in the previous context and returns a new one. See managing context.

OperationVariables

A map of GraphQL variables being sent with the operation.

The input object provided to ApolloLink.execute to send a GraphQL request through the link chain.

Properties
Name / Type
Description
DefaultContext

Context provided to the link chain. Context is not sent to the server and is used to communicate additional metadata from a request to individual links in the link chain.

Record<string, any>

A map of extensions that will be sent with the GraphQL request to the server.

DocumentNode

The parsed GraphQL document that will be sent with the GraphQL request to the server.

OperationVariables

The variables provided for the query.

A request handler is responsible for performing some logic and executing the request, either by forwarding the operation to the next link in the chain, or sending the operation to the destination that executes it, such as a GraphQL server.

Signature

TypeScript
1RequestHandler(
2  operation: ApolloLink.Operation,
3  forward: ApolloLink.ForwardFunction
4): Observable<ApolloLink.Result>

Parameters

Name / Type
Description
operation
ApolloLink.Operation

The Operation object that provides information about the currently executed GraphQL request.

Show/hide child attributes
ApolloClient

The Apollo Client instance executing the request.

Record<string, any>

A map that stores extensions data to be sent to the server.

() => Readonly<ApolloLink.OperationContext>

A function that gets the current context of the request. This can be used by links to determine which actions to perform. See managing context

string | undefined

The string name of the GraphQL operation. If it is anonymous, operationName will be undefined.

OperationTypeNode

The type of the GraphQL operation, such as query or mutation.

DocumentNode

A DocumentNode that describes the operation taking place.

{ (context: Partial<ApolloLink.OperationContext>): void; (updateContext: (previousContext: Readonly<ApolloLink.OperationContext>) => Partial<ApolloLink.OperationContext>): void; }

A function that takes either a new context object, or a function which takes in the previous context and returns a new one. See managing context.

OperationVariables

A map of GraphQL variables being sent with the operation.

forward
ApolloLink.ForwardFunction

A function that is called to execute the next link in the chain.

The currently executed operation object provided to an ApolloLink.RequestHandler for each link in the link chain.

Properties
Name / Type
Description
ApolloClient

The Apollo Client instance executing the request.

Record<string, any>

A map that stores extensions data to be sent to the server.

() => Readonly<ApolloLink.OperationContext>

A function that gets the current context of the request. This can be used by links to determine which actions to perform. See managing context

string | undefined

The string name of the GraphQL operation. If it is anonymous, operationName will be undefined.

OperationTypeNode

The type of the GraphQL operation, such as query or mutation.

DocumentNode

A DocumentNode that describes the operation taking place.

{ (context: Partial<ApolloLink.OperationContext>): void; (updateContext: (previousContext: Readonly<ApolloLink.OperationContext>) => Partial<ApolloLink.OperationContext>): void; }

A function that takes either a new context object, or a function which takes in the previous context and returns a new one. See managing context.

OperationVariables

A map of GraphQL variables being sent with the operation.

The context object that can be read and modified by links using the operation.getContext() and operation.setContext() methods.

Properties

(Warning: some properties might be missing from the table due to complex inheritance!)
Name / Type
Description
ClientAwarenessLink.ClientAwarenessOptions

Indicates whether queryDeduplication was enabled for the request.

Feedback

Edit on GitHub

Ask Community