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.
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
1constructor(
2 request?: ApolloLink.RequestHandler
3): ApolloLinkStatic methods
Composes multiple links into a single composed link that executes each provided link in serial order.
Example
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
1from(
2 links: ApolloLink[]
3): ApolloLinkParameters
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
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
1split(
2 test: (op: ApolloLink.Operation) => boolean,
3 left: ApolloLink,
4 right?: ApolloLink
5): ApolloLinkParameters
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.
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
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
1execute(
2 link: ApolloLink,
3 request: ApolloLink.Request,
4 context: ApolloLink.ExecuteContext
5): Observable<ApolloLink.Result>Parameters
The ApolloLink instance to execute the request.
The GraphQL request details, such as the query and
variables.
Show/hide child attributes
DefaultContextContext 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.
DocumentNodeThe parsed GraphQL document that will be sent with the GraphQL request to the server.
OperationVariablesThe variables provided for the query.
The execution context for the request, such as the
client making the request.
Show/hide child attributes
ApolloClientThe Apollo Client instance that executed the GraphQL request.
Creates a link that completes immediately and does not emit a result.
Example
1 const link = ApolloLink.empty();Signature
1empty(): ApolloLink⚠️ Deprecated
Use
ApolloLink.frominstead.ApolloLink.concatwill be removed in a future major version.
Combines multiple links into a single composed link.
Example
1 const link = ApolloLink.concat(firstLink, secondLink, thirdLink);Signature
1concat(
2 links: ApolloLink[]
3): ApolloLinkParameters
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
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
1concat(
2 links: ApolloLink[]
3): ApolloLinkParameters
Concatenates a link that conditionally routes a request to different links.
Example
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
1split(
2 test: (op: ApolloLink.Operation) => boolean,
3 left: ApolloLink,
4 right?: ApolloLink
5): ApolloLinkParameters
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.
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.
ApolloClientThe Apollo Client instance that executed the GraphQL request.
A function that when called will execute the next link in the link chain.
Example
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
1ForwardFunction(
2 operation: ApolloLink.Operation
3): Observable<ApolloLink.Result>Parameters
The current ApolloLink.Operation object for the
request.
Show/hide child attributes
ApolloClientThe 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 | undefinedThe string name of the GraphQL operation. If it is anonymous,
operationName will be undefined.
OperationTypeNodeThe type of the GraphQL operation, such as query or mutation.
DocumentNodeA 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.
OperationVariablesA 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.
DefaultContextContext 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.
DocumentNodeThe parsed GraphQL document that will be sent with the GraphQL request to the server.
OperationVariablesThe 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
1RequestHandler(
2 operation: ApolloLink.Operation,
3 forward: ApolloLink.ForwardFunction
4): Observable<ApolloLink.Result>Parameters
The Operation object that provides information about the
currently executed GraphQL request.
Show/hide child attributes
ApolloClientThe 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 | undefinedThe string name of the GraphQL operation. If it is anonymous,
operationName will be undefined.
OperationTypeNodeThe type of the GraphQL operation, such as query or mutation.
DocumentNodeA 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.
OperationVariablesA map of GraphQL variables being sent with the operation.
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.
ApolloClientThe 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 | undefinedThe string name of the GraphQL operation. If it is anonymous,
operationName will be undefined.
OperationTypeNodeThe type of the GraphQL operation, such as query or mutation.
DocumentNodeA 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.
OperationVariablesA 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.
(Warning: some properties might be missing from the table due to complex inheritance!)
ClientAwarenessLink.ClientAwarenessOptionsbooleanIndicates whether queryDeduplication was enabled for the request.