HTTP transport layer for GraphQL operations within the Apollo Link ecosystem
npx @tessl/cli install tessl/npm-apollo-link-http@1.5.0Apollo Link HTTP provides an HTTP transport layer for GraphQL operations within the Apollo Link ecosystem. It enables GraphQL queries and mutations to be sent over HTTP connections, supporting both POST and GET requests with extensive configuration options for authentication, custom headers, credentials, and fetch behavior.
npm install apollo-link-httpimport { createHttpLink, HttpLink } from "apollo-link-http";For CommonJS:
const { createHttpLink, HttpLink } = require("apollo-link-http");import { createHttpLink } from "apollo-link-http";
import { ApolloClient, InMemoryCache } from "@apollo/client";
// Create HTTP link with default settings
const httpLink = createHttpLink({
uri: "https://api.example.com/graphql",
});
// Create HTTP link with custom options
const customHttpLink = createHttpLink({
uri: "https://api.example.com/graphql",
headers: {
authorization: "Bearer token123",
},
credentials: "include",
useGETForQueries: true,
});
// Use with Apollo Client
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
});Apollo Link HTTP is built around several key components:
createHttpLink() function that creates configured Apollo Link instancesHttpLink class providing an alternative class-based interfaceFactory function for creating HTTP transport links with comprehensive configuration options.
function createHttpLink(linkOptions?: HttpLink.Options): ApolloLink;Class-based interface for creating HTTP transport links, extending Apollo Link base functionality.
class HttpLink extends ApolloLink {
constructor(opts?: HttpLink.Options);
requester: RequestHandler;
}Comprehensive configuration system for customizing HTTP transport behavior.
namespace HttpLink {
interface Options extends HttpOptions {
useGETForQueries?: boolean;
}
interface UriFunction {
(operation: Operation): string;
}
}
interface HttpOptions {
uri?: string | UriFunction;
includeExtensions?: boolean;
fetch?: WindowOrWorkerGlobalScope['fetch'];
headers?: any;
credentials?: string;
fetchOptions?: any;
}Structured error types for different failure scenarios in HTTP transport.
type ServerError = Error & {
response: Response;
result: Record<string, any>;
statusCode: number;
};
type ServerParseError = Error & {
response: Response;
statusCode: number;
bodyText: string;
};
type ClientParseError = InvariantError & {
parseError: Error;
};interface Body {
query?: string;
operationName?: string;
variables?: Record<string, any>;
extensions?: Record<string, any>;
}
interface HttpConfig {
http?: HttpQueryOptions;
options?: any;
headers?: any;
credentials?: any;
}
interface HttpQueryOptions {
includeQuery?: boolean;
includeExtensions?: boolean;
}type FetchOptions = HttpLink.Options;
type UriFunction = HttpLink.UriFunction;