The HttpLink class provides an alternative class-based interface for creating HTTP transport links. It extends the base ApolloLink class and internally uses the createHttpLink factory function.
Creates an HTTP Link instance using class instantiation syntax.
/**
* HTTP Link class for GraphQL operations over HTTP
* Extends ApolloLink with HTTP-specific functionality
*/
class HttpLink extends ApolloLink {
/**
* Creates a new HttpLink instance
* @param opts - Configuration options for HTTP transport
*/
constructor(opts?: HttpLink.Options);
/** Request handler function inherited from ApolloLink */
requester: RequestHandler;
}Constructor Parameters:
opts (optional): Configuration object with the same options as createHttpLinkProperties:
requester: The internal request handler function that processes GraphQL operationsUsage Examples:
import { HttpLink } from "apollo-link-http";
// Basic HTTP link using class syntax
const httpLink = new HttpLink();
// HTTP link with custom configuration
const customHttpLink = new HttpLink({
uri: "https://api.example.com/graphql",
headers: {
authorization: "Bearer token123",
},
credentials: "include",
useGETForQueries: true,
});
// Use in Apollo Client
import { ApolloClient, InMemoryCache } from "@apollo/client";
const client = new ApolloClient({
link: customHttpLink,
cache: new InMemoryCache(),
});Both approaches produce functionally identical results:
import { createHttpLink, HttpLink } from "apollo-link-http";
// Functional approach
const functionalLink = createHttpLink({
uri: "https://api.example.com/graphql",
});
// Class-based approach
const classLink = new HttpLink({
uri: "https://api.example.com/graphql",
});
// Both links behave identicallyHttpLink instances work seamlessly in Apollo Link chains:
import { from } from "apollo-link";
import { onError } from "apollo-link-error";
import { HttpLink } from "apollo-link-http";
const httpLink = new HttpLink({
uri: "https://api.example.com/graphql",
});
const errorLink = onError(({ graphQLErrors, networkError }) => {
// Handle errors
});
// Combine links in a chain
const link = from([errorLink, httpLink]);The HttpLink class is particularly useful when: