@apollo/client
Version:
A fully-featured caching GraphQL client.
1 lines • 7.55 kB
Source Map (JSON)
{"version":3,"file":"batchLink.cjs","sources":["../../../../src/link/batch/batchLink.ts"],"sourcesContent":["import type { Observable } from \"rxjs\";\nimport { EMPTY } from \"rxjs\";\n\nimport { ApolloLink } from \"@apollo/client/link\";\n\nimport { OperationBatcher } from \"./batching.js\";\n\nexport declare namespace BatchLink {\n export namespace Shared {\n /** These options are shared between `BatchLink` and `BatchHttpLink` */\n interface Options {\n /**\n * The interval at which to batch, in milliseconds.\n *\n * @defaultValue 10\n */\n batchInterval?: number;\n\n /**\n * \"batchInterval\" is a throttling behavior by default, if you instead wish\n * to debounce outbound requests, set \"batchDebounce\" to true. More useful\n * for mutations than queries.\n */\n batchDebounce?: boolean;\n\n /**\n * The maximum number of operations to include in a single batch.\n *\n * @defaultValue \\{\\{defaultValue\\}\\}\n */\n batchMax?: number;\n\n /**\n * Creates the key for a batch\n */\n batchKey?: (operation: ApolloLink.Operation) => string;\n }\n }\n\n /**\n * Function type for handling a batch of GraphQL operations.\n *\n * @remarks\n *\n * The batch handler is responsible for processing multiple operations together\n * and returning their results. Each operation has a corresponding forward function\n * that can be used to continue processing down the link chain.\n *\n * Results must be returned in the same order as the input operations to ensure\n * proper correlation with the original requests.\n *\n * @param operations - Array of GraphQL operations to process\n * @param forward - Array of forward functions, one per operation\n * @returns Observable that emits an array of results in the same order as operations\n */\n export type BatchHandler = (\n operations: ApolloLink.Operation[],\n forward: ApolloLink.ForwardFunction[]\n ) => Observable<ApolloLink.Result[]>;\n\n /**\n * Configuration options for creating a `BatchLink` instance.\n *\n * @remarks\n *\n * `BatchLink` options control how operations are grouped into batches\n * and when those batches are processed. The `batchHandler` function\n * is responsible for actually processing the batched operations.\n *\n * Most batching behavior is configured through timing options:\n *\n * - `batchInterval`: How long to wait before processing a batch\n * - `batchDebounce`: Whether to reset the timer on new operations\n * - `batchMax`: Maximum operations per batch (0 = unlimited)\n *\n * Custom grouping logic can be implemented via `batchKey` function.\n */\n export interface Options extends Shared.Options {\n /**\n * The handler that executes a batch of operations.\n *\n * @remarks\n *\n * This function receives an array of operations and their corresponding\n * forward functions, and should return an Observable that emits the results\n * for all operations in the batch.\n */\n batchHandler?: BatchLink.BatchHandler;\n\n /** {@inheritDoc @apollo/client/link/batch!BatchLink.Shared.Options#batchMax:member {\"defaultValue\": 0}} */\n batchMax?: number;\n }\n}\n\n/**\n * `BatchLink` is a non-terminating link that provides the core batching\n * functionality for grouping multiple GraphQL operations into batches based\n * on configurable timing and key-based grouping strategies. It serves as a base\n * link to `BatchHttpLink`.\n *\n * @remarks\n *\n * > [!NOTE]\n * > You will not generally use `BatchLink` on your own unless you need to\n * > provide batching capabilities to third-party terminating links. Prefer\n * > using `BatchHttpLink` to batch GraphQL operations over HTTP.\n *\n * @example\n *\n * ```ts\n * import { BatchLink } from \"@apollo/client/link/batch\";\n *\n * const link = new BatchLink({\n * batchInterval: 20,\n * batchMax: 5,\n * batchHandler: (operations, forwards) => {\n * // Custom logic to process batch of operations\n * return handleBatch(operations, forwards);\n * },\n * });\n * ```\n */\nexport class BatchLink extends ApolloLink {\n private batcher: OperationBatcher;\n\n constructor(options?: BatchLink.Options) {\n super();\n\n const {\n batchDebounce,\n batchInterval = 10,\n batchMax = 0,\n batchHandler = () => EMPTY,\n batchKey = () => \"\",\n } = options || {};\n\n this.batcher = new OperationBatcher({\n batchDebounce,\n batchInterval,\n batchMax,\n batchHandler,\n batchKey,\n });\n }\n\n public request(\n operation: ApolloLink.Operation,\n forward: ApolloLink.ForwardFunction\n ): Observable<ApolloLink.Result> {\n return this.batcher.enqueueRequest({ operation, forward });\n }\n}\n"],"names":[],"mappings":";;;AACA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;AAEA,CAAA,CAAA,CAA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