Apollo Link HTTP provides extensive configuration options through the HttpLink.Options interface, allowing fine-grained control over HTTP transport behavior, authentication, headers, and request processing.
Primary configuration interface extending base HTTP options with additional GraphQL-specific features.
namespace HttpLink {
/**
* Configuration options for HttpLink
* Extends HttpOptions with GraphQL-specific HTTP options
*/
interface Options extends HttpOptions {
/**
* If set to true, use the HTTP GET method for query operations
* Mutations will still use the method specified in fetchOptions.method (defaults to POST)
* @default false
*/
useGETForQueries?: boolean;
}
/**
* Function type for dynamic URI resolution
* Receives the GraphQL operation and returns the appropriate URI
*/
interface UriFunction {
(operation: Operation): string;
}
}Core HTTP configuration options inherited from apollo-link-http-common.
/**
* Base HTTP configuration options for GraphQL transport
*/
interface HttpOptions {
/**
* The URI to use when fetching operations
* Can be a static string or a function that resolves URI based on operation
* @default '/graphql'
*/
uri?: string | UriFunction;
/**
* Passes the extensions field to your GraphQL server
* @default false
*/
includeExtensions?: boolean;
/**
* A fetch-compatible API to use when making requests
* If not provided, uses global fetch or throws error if unavailable
*/
fetch?: WindowOrWorkerGlobalScope['fetch'];
/**
* An object representing values to be sent as headers on the request
* Headers can be overridden per-operation via context
*/
headers?: any;
/**
* The credentials policy you want to use for the fetch call
* Values: 'omit', 'same-origin', 'include'
*/
credentials?: string;
/**
* Any overrides of the fetch options argument to pass to the fetch call
* Merged with other options and can be overridden per-operation
*/
fetchOptions?: any;
}Internal configuration structure used for request processing.
/**
* Internal HTTP configuration structure
* Used for merging link options, context options, and fallback options
*/
interface HttpConfig {
/** HTTP query-specific options */
http?: HttpQueryOptions;
/** Fetch options for the request */
options?: any;
/** Headers to include in the request */
headers?: any;
/** Credentials policy for the request */
credentials?: any;
}
/**
* Query-specific HTTP configuration options
*/
interface HttpQueryOptions {
/** Whether to include the query string in the request body */
includeQuery?: boolean;
/** Whether to include GraphQL extensions in the request */
includeExtensions?: boolean;
}Basic Configuration:
import { createHttpLink } from "apollo-link-http";
const link = createHttpLink({
uri: "https://api.example.com/graphql",
});Authentication Configuration:
const authLink = createHttpLink({
uri: "https://api.example.com/graphql",
headers: {
authorization: "Bearer your-jwt-token",
"X-API-Key": "your-api-key",
},
credentials: "include", // Send cookies with requests
});Custom Fetch Configuration:
import fetch from "node-fetch";
const nodeLink = createHttpLink({
uri: "https://api.example.com/graphql",
fetch: fetch as any, // Custom fetch for Node.js
fetchOptions: {
timeout: 30000,
compress: true,
},
});Dynamic URI Configuration:
const dynamicLink = createHttpLink({
uri: (operation) => {
const context = operation.getContext();
// Route based on operation type
if (operation.query.definitions.some(d =>
d.kind === 'OperationDefinition' && d.operation === 'subscription'
)) {
return "wss://subscriptions.example.com/graphql";
}
// Route based on operation name
if (operation.operationName?.startsWith('Admin')) {
return "https://admin-api.example.com/graphql";
}
return "https://api.example.com/graphql";
},
});GET Method Configuration:
const getLink = createHttpLink({
uri: "https://api.example.com/graphql",
useGETForQueries: true, // Use GET for queries, POST for mutations
fetchOptions: {
method: "POST", // Default method for mutations
},
});Extensions and Advanced Options:
const advancedLink = createHttpLink({
uri: "https://api.example.com/graphql",
includeExtensions: true, // Include GraphQL extensions
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},
fetchOptions: {
mode: "cors",
cache: "no-cache",
redirect: "follow",
},
});Configuration can be overridden per-operation using Apollo Link context:
// Execute query with custom context
const result = await client.query({
query: MY_QUERY,
context: {
// Override URI for this operation
uri: "https://special-endpoint.example.com/graphql",
// Add or override headers
headers: {
"X-Request-ID": generateRequestId(),
"X-User-Role": "admin",
},
// Override credentials policy
credentials: "omit",
// Override fetch options
fetchOptions: {
timeout: 60000, // Longer timeout for this operation
},
// Client awareness for automatic headers
clientAwareness: {
name: "admin-dashboard",
version: "2.1.0",
},
},
});For backward compatibility, the following type aliases are provided:
/**
* Legacy type alias for HttpLink.Options
* @deprecated Use HttpLink.Options instead
*/
type FetchOptions = HttpLink.Options;
/**
* Legacy type alias for HttpLink.UriFunction
* @deprecated Use HttpLink.UriFunction instead
*/
type UriFunction = HttpLink.UriFunction;