or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mderror-handling.mdindex.mdlink-class.mdlink-creation.md
tile.json

link-class.mddocs/

HTTP Link Class

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.

Capabilities

HttpLink Constructor

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 createHttpLink

Properties:

  • requester: The internal request handler function that processes GraphQL operations

Usage 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(),
});

Comparison with createHttpLink

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 identically

Integration with Link Chains

HttpLink 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:

  • You prefer class-based APIs
  • You need to extend or customize the link behavior
  • You're working in environments that favor class instantiation patterns
  • You want to maintain consistency with other class-based Apollo Link implementations