@apollo/client 4.0.0-rc.7 → 4.0.0-rc.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/__cjs/core/index.cjs +1 -2
- package/__cjs/core/index.cjs.map +1 -1
- package/__cjs/core/index.d.cts +1 -2
- package/__cjs/invariantErrorCodes.cjs +9 -9
- package/__cjs/link/batch/batchLink.cjs +1 -3
- package/__cjs/link/batch/batchLink.cjs.map +1 -1
- package/__cjs/link/batch/batchLink.d.cts +35 -26
- package/__cjs/link/batch/batching.cjs.map +1 -1
- package/__cjs/link/batch/batching.d.cts +2 -2
- package/__cjs/link/batch/index.cjs +1 -2
- package/__cjs/link/batch/index.cjs.map +1 -1
- package/__cjs/link/batch/index.d.cts +1 -2
- package/__cjs/link/batch-http/batchHttpLink.cjs +23 -3
- package/__cjs/link/batch-http/batchHttpLink.cjs.map +1 -1
- package/__cjs/link/batch-http/batchHttpLink.d.cts +44 -4
- package/__cjs/link/client-awareness/ClientAwarenessLink.cjs +33 -0
- package/__cjs/link/client-awareness/ClientAwarenessLink.cjs.map +1 -1
- package/__cjs/link/client-awareness/ClientAwarenessLink.d.cts +35 -2
- package/__cjs/link/error/index.cjs +44 -0
- package/__cjs/link/error/index.cjs.map +1 -1
- package/__cjs/link/error/index.d.cts +64 -4
- package/__cjs/link/http/BaseHttpLink.cjs +1 -2
- package/__cjs/link/http/BaseHttpLink.cjs.map +1 -1
- package/__cjs/link/http/HttpLink.cjs +22 -0
- package/__cjs/link/http/HttpLink.cjs.map +1 -1
- package/__cjs/link/http/HttpLink.d.cts +190 -87
- package/__cjs/link/http/checkFetcher.cjs +1 -1
- package/__cjs/link/http/index.cjs +1 -3
- package/__cjs/link/http/index.cjs.map +1 -1
- package/__cjs/link/http/index.d.cts +0 -2
- package/__cjs/link/http/parseAndCheckHttpResponse.cjs +1 -1
- package/__cjs/link/http/rewriteURIForGET.cjs +2 -3
- package/__cjs/link/http/rewriteURIForGET.cjs.map +1 -1
- package/__cjs/link/persisted-queries/index.cjs +2 -2
- package/__cjs/link/persisted-queries/index.cjs.map +1 -1
- package/__cjs/link/persisted-queries/index.d.cts +23 -24
- package/__cjs/link/remove-typename/index.cjs.map +1 -1
- package/__cjs/link/remove-typename/index.d.cts +0 -1
- package/__cjs/link/remove-typename/removeTypenameFromVariables.cjs.map +1 -1
- package/__cjs/link/remove-typename/removeTypenameFromVariables.d.cts +9 -8
- package/__cjs/link/retry/delayFunction.cjs.map +1 -1
- package/__cjs/link/retry/delayFunction.d.cts +2 -39
- package/__cjs/link/retry/retryFunction.cjs.map +1 -1
- package/__cjs/link/retry/retryFunction.d.cts +2 -29
- package/__cjs/link/retry/retryLink.cjs +10 -7
- package/__cjs/link/retry/retryLink.cjs.map +1 -1
- package/__cjs/link/retry/retryLink.d.cts +56 -4
- package/__cjs/link/schema/index.cjs.map +1 -1
- package/__cjs/link/schema/index.d.cts +1 -1
- package/__cjs/link/subscriptions/index.cjs +23 -0
- package/__cjs/link/subscriptions/index.cjs.map +1 -1
- package/__cjs/link/subscriptions/index.d.cts +23 -0
- package/__cjs/link/ws/index.cjs +17 -1
- package/__cjs/link/ws/index.cjs.map +1 -1
- package/__cjs/link/ws/index.d.cts +7 -0
- package/__cjs/utilities/subscriptions/relay/index.cjs +1 -2
- package/__cjs/utilities/subscriptions/relay/index.cjs.map +1 -1
- package/__cjs/version.cjs +1 -1
- package/core/index.d.ts +1 -2
- package/core/index.js +1 -1
- package/core/index.js.map +1 -1
- package/invariantErrorCodes.js +9 -9
- package/link/batch/batchLink.d.ts +36 -27
- package/link/batch/batchLink.js +0 -1
- package/link/batch/batchLink.js.map +1 -1
- package/link/batch/batching.d.ts +2 -2
- package/link/batch/batching.js.map +1 -1
- package/link/batch/index.d.ts +1 -2
- package/link/batch/index.js +1 -1
- package/link/batch/index.js.map +1 -1
- package/link/batch-http/batchHttpLink.d.ts +45 -5
- package/link/batch-http/batchHttpLink.js +24 -4
- package/link/batch-http/batchHttpLink.js.map +1 -1
- package/link/client-awareness/ClientAwarenessLink.d.ts +35 -2
- package/link/client-awareness/ClientAwarenessLink.js +33 -0
- package/link/client-awareness/ClientAwarenessLink.js.map +1 -1
- package/link/error/index.d.ts +65 -5
- package/link/error/index.js +44 -0
- package/link/error/index.js.map +1 -1
- package/link/http/BaseHttpLink.js +1 -2
- package/link/http/BaseHttpLink.js.map +1 -1
- package/link/http/HttpLink.d.ts +191 -88
- package/link/http/HttpLink.js +22 -0
- package/link/http/HttpLink.js.map +1 -1
- package/link/http/checkFetcher.js +1 -1
- package/link/http/index.d.ts +0 -2
- package/link/http/index.js +0 -1
- package/link/http/index.js.map +1 -1
- package/link/http/parseAndCheckHttpResponse.js +1 -1
- package/link/http/rewriteURIForGET.js +2 -3
- package/link/http/rewriteURIForGET.js.map +1 -1
- package/link/persisted-queries/index.d.ts +23 -24
- package/link/persisted-queries/index.js +2 -2
- package/link/persisted-queries/index.js.map +1 -1
- package/link/remove-typename/index.d.ts +0 -1
- package/link/remove-typename/index.js.map +1 -1
- package/link/remove-typename/removeTypenameFromVariables.d.ts +9 -8
- package/link/remove-typename/removeTypenameFromVariables.js.map +1 -1
- package/link/retry/delayFunction.d.ts +2 -39
- package/link/retry/delayFunction.js.map +1 -1
- package/link/retry/retryFunction.d.ts +2 -29
- package/link/retry/retryFunction.js.map +1 -1
- package/link/retry/retryLink.d.ts +56 -4
- package/link/retry/retryLink.js +11 -8
- package/link/retry/retryLink.js.map +1 -1
- package/link/schema/index.d.ts +1 -1
- package/link/schema/index.js.map +1 -1
- package/link/subscriptions/index.d.ts +23 -0
- package/link/subscriptions/index.js +23 -0
- package/link/subscriptions/index.js.map +1 -1
- package/link/ws/index.d.ts +7 -0
- package/link/ws/index.js +13 -1
- package/link/ws/index.js.map +1 -1
- package/package.json +1 -1
- package/utilities/subscriptions/relay/index.js +1 -2
- package/utilities/subscriptions/relay/index.js.map +1 -1
- package/version.js +1 -1
- package/__cjs/link/http/serializeFetchParameter.cjs +0 -18
- package/__cjs/link/http/serializeFetchParameter.cjs.map +0 -1
- package/__cjs/link/http/serializeFetchParameter.d.cts +0 -6
- package/link/http/serializeFetchParameter.d.ts +0 -6
- package/link/http/serializeFetchParameter.js +0 -14
- package/link/http/serializeFetchParameter.js.map +0 -1
package/core/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,UAAU;AAgBV,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAYvD,OAAO,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAyB5E,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,SAAS,EACT,eAAe,EACf,WAAW,EACX,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,uBAAuB,CAAC;AAiC/B,OAAO,EACL,WAAW,EACX,uBAAuB,EACvB,aAAa,EACb,OAAO,EACP,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAE9B,UAAU;AAEV,OAAO,EACL,UAAU,EACV,MAAM,EACN,KAAK,EACL,OAAO,EACP,IAAI,EACJ,KAAK,GACN,MAAM,qBAAqB,CAAC;AAU7B,OAAO,EACL,YAAY,EACZ,cAAc,EACd,uBAAuB,EACvB,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,yBAAyB,EACzB,gBAAgB,EAChB,wBAAwB;AACxB,uEAAuE;AACvE,gCAAgC,EAChC,SAAS,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,UAAU;AAgBV,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAYvD,OAAO,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAyB5E,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,SAAS,EACT,eAAe,EACf,WAAW,EACX,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,uBAAuB,CAAC;AAiC/B,OAAO,EACL,WAAW,EACX,uBAAuB,EACvB,aAAa,EACb,OAAO,EACP,iBAAiB,GAClB,MAAM,sBAAsB,CAAC;AAE9B,UAAU;AAEV,OAAO,EACL,UAAU,EACV,MAAM,EACN,KAAK,EACL,OAAO,EACP,IAAI,EACJ,KAAK,GACN,MAAM,qBAAqB,CAAC;AAU7B,OAAO,EACL,YAAY,EACZ,cAAc,EACd,uBAAuB,EACvB,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,yBAAyB,EACzB,gBAAgB,EAChB,wBAAwB;AACxB,uEAAuE;AACvE,gCAAgC,EAChC,SAAS,GACV,MAAM,0BAA0B,CAAC;AAmBlC,OAAO,EACL,iBAAiB,EACjB,WAAW,EACX,UAAU,GACX,MAAM,0BAA0B,CAAC;AAElC,gBAAgB;AAEhB,yEAAyE;AACzE,yFAAyF;AACzF,6DAA6D;AAC7D,OAAO,EAAE,YAAY,IAAI,eAAe,EAAE,MAAM,oCAAoC,CAAC;AAErF,0DAA0D;AAC1D,qEAAqE;AACrE,sEAAsE;AACtE,yEAAyE;AACzE,yCAAyC;AACzC,wEAAwE;AACxE,6EAA6E;AAC7E,8EAA8E;AAC9E,sEAAsE;AACtE,qEAAqE;AACrE,4EAA4E;AAC5E,6BAA6B;AAC7B,OAAO,EACL,oCAAoC,EACpC,uBAAuB,EACvB,mCAAmC,EACnC,GAAG,EACH,WAAW,GACZ,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC","sourcesContent":["/* Core */\n\nexport type {\n ApolloClientOptions,\n ApolloQueryResult,\n DefaultOptions,\n DevtoolsOptions,\n MutateResult,\n MutationOptions,\n QueryOptions,\n RefetchQueriesOptions,\n RefetchQueriesResult,\n SubscribeToMoreOptions,\n SubscriptionOptions,\n WatchQueryOptions,\n} from \"./deprecated.js\";\nexport { ApolloClient } from \"./ApolloClient.js\";\nexport { ObservableQuery } from \"./ObservableQuery.js\";\nexport type {\n ErrorPolicy,\n FetchPolicy,\n MutationFetchPolicy,\n RefetchWritePolicy,\n SubscribeToMoreFunction,\n SubscribeToMoreUpdateQueryFn,\n UpdateQueryMapFn,\n UpdateQueryOptions,\n WatchQueryFetchPolicy,\n} from \"./watchQueryOptions.js\";\nexport { isNetworkRequestSettled, NetworkStatus } from \"./networkStatus.js\";\nexport type {\n DataState,\n DataValue,\n DefaultContext,\n ErrorLike,\n GetDataState,\n InternalRefetchQueriesInclude,\n InternalRefetchQueriesMap,\n InternalRefetchQueriesOptions,\n InternalRefetchQueriesResult,\n InternalRefetchQueryDescriptor,\n MutationQueryReducer,\n MutationQueryReducersMap,\n MutationUpdaterFunction,\n NormalizedExecutionResult,\n OnQueryUpdated,\n OperationVariables,\n RefetchQueriesInclude,\n RefetchQueriesPromiseResults,\n RefetchQueryDescriptor,\n SubscriptionObservable,\n TypedDocumentNode,\n TypeOverrides,\n} from \"./types.js\";\nexport {\n CombinedGraphQLErrors,\n CombinedProtocolErrors,\n LinkError,\n LocalStateError,\n ServerError,\n ServerParseError,\n UnconventionalError,\n} from \"@apollo/client/errors\";\n/* Cache */\n\nexport type {\n ApolloReducerConfig,\n Cache,\n DiffQueryAgainstStoreOptions,\n FieldFunctionOptions,\n FieldMergeFunction,\n FieldPolicy,\n FieldReadFunction,\n IdGetter,\n IdGetterObj,\n InMemoryCacheConfig,\n MergeInfo,\n MergeTree,\n NormalizedCache,\n NormalizedCacheObject,\n OptimisticStoreItem,\n PossibleTypesMap,\n ReactiveVar,\n ReadMergeModifyContext,\n ReadQueryOptions,\n StoreValue,\n // All the exports (types) from ../cache, minus cacheSlot,\n // which we want to keep semi-private.\n Transaction,\n TypePolicies,\n TypePolicy,\n WatchFragmentOptions,\n WatchFragmentResult,\n} from \"@apollo/client/cache\";\n\nexport {\n ApolloCache,\n defaultDataIdFromObject,\n InMemoryCache,\n makeVar,\n MissingFieldError,\n} from \"@apollo/client/cache\";\n\n/* Link */\n\nexport {\n ApolloLink,\n concat,\n empty,\n execute,\n from,\n split,\n} from \"@apollo/client/link\";\nexport type {\n ApolloPayloadResult,\n DocumentNode,\n FetchResult,\n GraphQLRequest,\n Operation,\n RequestHandler,\n} from \"@apollo/client/link\";\n\nexport {\n checkFetcher,\n createHttpLink,\n createSignalIfSupported,\n defaultPrinter,\n fallbackHttpConfig,\n HttpLink,\n parseAndCheckHttpResponse,\n rewriteURIForGET,\n selectHttpOptionsAndBody,\n // TODO remove: needed by @apollo/client/link/batch-http but not public\n selectHttpOptionsAndBodyInternal,\n selectURI,\n} from \"@apollo/client/link/http\";\n\n/* Masking */\nexport type {\n DataMasking,\n FragmentType,\n Masked,\n MaskedDocumentNode,\n MaybeMasked,\n Unmasked,\n} from \"@apollo/client/masking\";\n\n/* Utilities */\n\nexport type {\n DocumentTransformCacheKey,\n Reference,\n StoreObject,\n} from \"@apollo/client/utilities\";\nexport {\n DocumentTransform,\n isReference,\n Observable,\n} from \"@apollo/client/utilities\";\n\n/* Supporting */\n\n// The verbosity of invariant.{log,warn,error} can be controlled globally\n// by passing \"log\", \"warn\", \"error\", or \"silent\" to setVerbosity (\"log\" is the default).\n// Note that all invariant.* logging is hidden in production.\nexport { setVerbosity as setLogVerbosity } from \"@apollo/client/utilities/invariant\";\n\n// Note that importing `gql` by itself, then destructuring\n// additional properties separately before exporting, is intentional.\n// Due to the way the `graphql-tag` library is setup, certain bundlers\n// can't find the properties added to the exported `gql` function without\n// additional guidance (e.g. Rollup - see\n// https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module).\n// Instead of having people that are using bundlers with `@apollo/client` add\n// extra bundler config to help `graphql-tag` exports be found (which would be\n// awkward since they aren't importing `graphql-tag` themselves), this\n// workaround of pulling the extra properties off the `gql` function,\n// then re-exporting them separately, helps keeps bundlers happy without any\n// additional config changes.\nexport {\n disableExperimentalFragmentVariables,\n disableFragmentWarnings,\n enableExperimentalFragmentVariables,\n gql,\n resetCaches,\n} from \"graphql-tag\";\nexport { build, version } from \"../version.js\";\n\n// internal types\nimport type { QueryManager } from \"./QueryManager.js\";\nimport type { NextFetchPolicyContext } from \"./watchQueryOptions.js\";\nexport declare namespace InternalTypes {\n export type { NextFetchPolicyContext, QueryManager };\n}\n"]}
|
package/invariantErrorCodes.js
CHANGED
|
@@ -289,7 +289,7 @@ you have an infinite render loop in your application.`
|
|
|
289
289
|
message: "The configured cache does not support fragment matching which will lead to incorrect results when executing local resolvers. Please use a cache that implements `fragmetMatches`."
|
|
290
290
|
},
|
|
291
291
|
|
|
292
|
-
|
|
292
|
+
57: {
|
|
293
293
|
file: "@apollo/client/link/persisted-queries/index.js",
|
|
294
294
|
condition: "options &&\n (typeof options.sha256 === \"function\" ||\n typeof options.generateHash === \"function\")",
|
|
295
295
|
|
|
@@ -298,13 +298,13 @@ you have an infinite render loop in your application.`
|
|
|
298
298
|
"parameter."
|
|
299
299
|
},
|
|
300
300
|
|
|
301
|
-
|
|
301
|
+
58: {
|
|
302
302
|
file: "@apollo/client/link/persisted-queries/index.js",
|
|
303
303
|
condition: "forward",
|
|
304
304
|
message: "PersistedQueryLink cannot be the last link in the chain."
|
|
305
305
|
},
|
|
306
306
|
|
|
307
|
-
|
|
307
|
+
59: {
|
|
308
308
|
file: "@apollo/client/link/http/checkFetcher.js",
|
|
309
309
|
condition: "fetcher || typeof fetch !== \"undefined\"",
|
|
310
310
|
|
|
@@ -322,17 +322,12 @@ const client = new ApolloClient({
|
|
|
322
322
|
`
|
|
323
323
|
},
|
|
324
324
|
|
|
325
|
-
|
|
325
|
+
60: {
|
|
326
326
|
file: "@apollo/client/link/http/parseAndCheckHttpResponse.js",
|
|
327
327
|
condition: "response.body && typeof response.body.getReader === \"function\"",
|
|
328
328
|
message: "Unknown type for `response.body`. Please use a `fetch` implementation that is WhatWG-compliant and that uses WhatWG ReadableStreams for `body`."
|
|
329
329
|
},
|
|
330
330
|
|
|
331
|
-
60: {
|
|
332
|
-
file: "@apollo/client/link/http/serializeFetchParameter.js",
|
|
333
|
-
message: `Network request failed. %s is not serializable: %s`
|
|
334
|
-
},
|
|
335
|
-
|
|
336
331
|
63: {
|
|
337
332
|
file: "@apollo/client/link/core/ApolloLink.js",
|
|
338
333
|
message: "request is not implemented"
|
|
@@ -589,6 +584,11 @@ export const devWarn = {
|
|
|
589
584
|
message: "The '%s' field on object %o returned `undefined` instead of a value. The parent resolver did not include the property in the returned value and there was no resolver defined for the field."
|
|
590
585
|
},
|
|
591
586
|
|
|
587
|
+
56: {
|
|
588
|
+
file: "@apollo/client/link/ws/index.js",
|
|
589
|
+
message: "`WebSocketLink` uses the deprecated and unmaintained `subscriptions-transport-ws` library. This link is no longer maintained and will be removed in a future major version of Apollo Client. We recommend switching to `GraphQLWsLink` which uses the `graphql-ws` library to send GraphQL operations through WebSocket connections (https://the-guild.dev/graphql/ws)."
|
|
590
|
+
},
|
|
591
|
+
|
|
592
592
|
61: {
|
|
593
593
|
file: "@apollo/client/link/core/ApolloLink.js",
|
|
594
594
|
message: "[ApolloLink.split]: The test function returned a non-boolean value which could result in subtle bugs (e.g. such as using an `async` function which always returns a truthy value). Got `%o`."
|
|
@@ -1,36 +1,45 @@
|
|
|
1
1
|
import type { Observable } from "rxjs";
|
|
2
2
|
import { ApolloLink } from "@apollo/client/link";
|
|
3
|
-
import type { BatchHandler } from "./batching.js";
|
|
4
|
-
export type { BatchableRequest, BatchHandler } from "./batching.js";
|
|
5
|
-
export { OperationBatcher } from "./batching.js";
|
|
6
3
|
export declare namespace BatchLink {
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
4
|
+
namespace Shared {
|
|
5
|
+
/** These options are shared between `BatchLink` and `BatchHttpLink` */
|
|
6
|
+
interface Options {
|
|
7
|
+
/**
|
|
8
|
+
* The interval at which to batch, in milliseconds.
|
|
9
|
+
*
|
|
10
|
+
* @defaultValue 10
|
|
11
|
+
*/
|
|
12
|
+
batchInterval?: number;
|
|
13
|
+
/**
|
|
14
|
+
* "batchInterval" is a throttling behavior by default, if you instead wish
|
|
15
|
+
* to debounce outbound requests, set "batchDebounce" to true. More useful
|
|
16
|
+
* for mutations than queries.
|
|
17
|
+
*/
|
|
18
|
+
batchDebounce?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The maximum number of operations to include in a single batch.
|
|
21
|
+
*
|
|
22
|
+
* @defaultValue \{\{defaultValue\}\}
|
|
23
|
+
*/
|
|
24
|
+
batchMax?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Creates the key for a batch
|
|
27
|
+
*/
|
|
28
|
+
batchKey?: (operation: ApolloLink.Operation) => string;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
type BatchHandler = (operations: ApolloLink.Operation[], forward: ApolloLink.ForwardFunction[]) => Observable<ApolloLink.Result[]>;
|
|
32
|
+
interface Options extends Shared.Options {
|
|
26
33
|
/**
|
|
27
34
|
* The handler that should execute a batch of operations.
|
|
28
35
|
*/
|
|
29
|
-
batchHandler?: BatchHandler;
|
|
36
|
+
batchHandler?: BatchLink.BatchHandler;
|
|
30
37
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
* The maximum number of operations to include in a single batch.
|
|
39
|
+
*
|
|
40
|
+
* @defaultValue 0
|
|
41
|
+
*/
|
|
42
|
+
batchMax?: number;
|
|
34
43
|
}
|
|
35
44
|
}
|
|
36
45
|
export declare class BatchLink extends ApolloLink {
|
|
@@ -38,4 +47,4 @@ export declare class BatchLink extends ApolloLink {
|
|
|
38
47
|
constructor(fetchParams?: BatchLink.Options);
|
|
39
48
|
request(operation: ApolloLink.Operation, forward: ApolloLink.ForwardFunction): Observable<ApolloLink.Result>;
|
|
40
49
|
}
|
|
41
|
-
//# sourceMappingURL=batchLink.d.ts.map
|
|
50
|
+
//# sourceMappingURL=batchLink.d.ts.map
|
package/link/batch/batchLink.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { EMPTY } from "rxjs";
|
|
2
2
|
import { ApolloLink } from "@apollo/client/link";
|
|
3
3
|
import { OperationBatcher } from "./batching.js";
|
|
4
|
-
export { OperationBatcher } from "./batching.js";
|
|
5
4
|
export class BatchLink extends ApolloLink {
|
|
6
5
|
batcher;
|
|
7
6
|
constructor(fetchParams) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batchLink.js","sourceRoot":"","sources":["../../../src/link/batch/batchLink.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAE7B,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"batchLink.js","sourceRoot":"","sources":["../../../src/link/batch/batchLink.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAE7B,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAkDjD,MAAM,OAAO,SAAU,SAAQ,UAAU;IAC/B,OAAO,CAAmB;IAElC,YAAY,WAA+B;QACzC,KAAK,EAAE,CAAC;QAER,MAAM,EACJ,aAAa,EACb,aAAa,GAAG,EAAE,EAClB,QAAQ,GAAG,CAAC,EACZ,YAAY,GAAG,GAAG,EAAE,CAAC,KAAK,EAC1B,QAAQ,GAAG,GAAG,EAAE,CAAC,EAAE,GACpB,GAAG,WAAW,IAAI,EAAE,CAAC;QAEtB,IAAI,CAAC,OAAO,GAAG,IAAI,gBAAgB,CAAC;YAClC,aAAa;YACb,aAAa;YACb,QAAQ;YACR,YAAY;YACZ,QAAQ;SACT,CAAC,CAAC;IACL,CAAC;IAEM,OAAO,CACZ,SAA+B,EAC/B,OAAmC;QAEnC,OAAO,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,CAAC;IAC7D,CAAC;CACF","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 export type BatchHandler = (\n operations: ApolloLink.Operation[],\n forward: ApolloLink.ForwardFunction[]\n ) => Observable<ApolloLink.Result[]>;\n\n export interface Options extends Shared.Options {\n /**\n * The handler that should execute a batch of operations.\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\nexport class BatchLink extends ApolloLink {\n private batcher: OperationBatcher;\n\n constructor(fetchParams?: BatchLink.Options) {\n super();\n\n const {\n batchDebounce,\n batchInterval = 10,\n batchMax = 0,\n batchHandler = () => EMPTY,\n batchKey = () => \"\",\n } = fetchParams || {};\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"]}
|
package/link/batch/batching.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Observable } from "rxjs";
|
|
2
2
|
import type { ApolloLink } from "@apollo/client/link";
|
|
3
|
-
|
|
3
|
+
import type { BatchLink } from "./batchLink.js";
|
|
4
4
|
export interface BatchableRequest {
|
|
5
5
|
operation: ApolloLink.Operation;
|
|
6
6
|
forward: ApolloLink.ForwardFunction;
|
|
@@ -17,7 +17,7 @@ export declare class OperationBatcher {
|
|
|
17
17
|
batchDebounce?: boolean;
|
|
18
18
|
batchInterval?: number;
|
|
19
19
|
batchMax?: number;
|
|
20
|
-
batchHandler: BatchHandler;
|
|
20
|
+
batchHandler: BatchLink.BatchHandler;
|
|
21
21
|
batchKey?: (operation: ApolloLink.Operation) => string;
|
|
22
22
|
});
|
|
23
23
|
enqueueRequest(request: BatchableRequest): Observable<ApolloLink.Result>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batching.js","sourceRoot":"","sources":["../../../src/link/batch/batching.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AA4BlC,qFAAqF;AACrF,2FAA2F;AAC3F,kBAAkB;AAClB,MAAM,OAAO,gBAAgB;IAC3B,oEAAoE;IAC5D,YAAY,GAAG,IAAI,GAAG,EAAwB,CAAC;IAE/C,wBAAwB,GAAG,IAAI,GAAG,EAGvC,CAAC;IACI,aAAa,CAAW;IACxB,aAAa,CAAU;IACvB,QAAQ,CAAS;IAEzB,oEAAoE;IAC5D,YAAY,CAAe;IAC3B,QAAQ,CAA8C;IAE9D,YAAY,EACV,aAAa,EACb,aAAa,EACb,QAAQ,EACR,YAAY,EACZ,QAAQ,GAOT;QAC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Q,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;oBAC/B,CAAC;gBACH,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACrB,CAAC;IAEO,wBAAwB,CAAC,GAAW;QAC1C,YAAY,CAAC,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QACrD,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAC/B,GAAG,EACH,UAAU,CAAC,GAAG,EAAE;YACd,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;YACvB,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC5C,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,CACvB,CAAC;IACJ,CAAC;CACF","sourcesContent":["import type { Subscription } from \"rxjs\";\nimport { Observable } from \"rxjs\";\n\nimport type { ApolloLink } from \"@apollo/client/link\";\n\nexport type BatchHandler = (\n operations: ApolloLink.Operation[],\n forward: ApolloLink.ForwardFunction[]\n) => Observable<ApolloLink.Result[]>;\n\nexport interface BatchableRequest {\n operation: ApolloLink.Operation;\n forward: ApolloLink.ForwardFunction;\n}\n\ntype QueuedRequest = BatchableRequest & {\n observable?: Observable<ApolloLink.Result>;\n next: Array<(result: ApolloLink.Result) => void>;\n error: Array<(error: Error) => void>;\n complete: Array<() => void>;\n subscribers: Set<object>;\n};\n\n// Batches are primarily a Set<QueuedRequest>, but may have other optional\n// properties, such as batch.subscription.\ntype RequestBatch = Set<QueuedRequest> & {\n subscription?: Subscription;\n};\n\n// QueryBatcher doesn't fire requests immediately. Requests that were enqueued within\n// a certain amount of time (configurable through `batchInterval`) will be batched together\n// into one query.\nexport class OperationBatcher {\n // Queue on which the QueryBatcher will operate on a per-tick basis.\n private batchesByKey = new Map<string, RequestBatch>();\n\n private scheduledBatchTimerByKey = new Map<\n string,\n ReturnType<typeof setTimeout>\n >();\n private batchDebounce?: boolean;\n private batchInterval?: number;\n private batchMax: number;\n\n //This function is called to the queries in the queue to the server.\n private batchHandler: BatchHandler;\n private batchKey: (operation: ApolloLink.Operation) => string;\n\n constructor({\n batchDebounce,\n batchInterval,\n batchMax,\n batchHandler,\n batchKey,\n }: {\n batchDebounce?: boolean;\n batchInterval?: number;\n batchMax?: number;\n batchHandler: BatchHandler;\n batchKey?: (operation: ApolloLink.Operation) => string;\n }) {\n this.batchDebounce = batchDebounce;\n this.batchInterval = batchInterval;\n this.batchMax = batchMax || 0;\n this.batchHandler = batchHandler;\n this.batchKey = batchKey || (() => \"\");\n }\n\n public enqueueRequest(\n request: BatchableRequest\n ): Observable<ApolloLink.Result> {\n const requestCopy: QueuedRequest = {\n ...request,\n next: [],\n error: [],\n complete: [],\n subscribers: new Set(),\n };\n\n const key = this.batchKey(request.operation);\n\n if (!requestCopy.observable) {\n requestCopy.observable = new Observable<ApolloLink.Result>((observer) => {\n let batch = this.batchesByKey.get(key)!;\n if (!batch) this.batchesByKey.set(key, (batch = new Set()));\n\n // These booleans seem to me (@benjamn) like they might always be the\n // same (and thus we could do with only one of them), but I'm not 100%\n // sure about that.\n const isFirstEnqueuedRequest = batch.size === 0;\n const isFirstSubscriber = requestCopy.subscribers.size === 0;\n requestCopy.subscribers.add(observer);\n if (isFirstSubscriber) {\n batch.add(requestCopy);\n }\n\n // called for each subscriber, so need to save all listeners (next, error, complete)\n if (observer.next) {\n requestCopy.next.push(observer.next.bind(observer));\n }\n\n if (observer.error) {\n requestCopy.error.push(observer.error.bind(observer));\n }\n\n if (observer.complete) {\n requestCopy.complete.push(observer.complete.bind(observer));\n }\n\n // The first enqueued request triggers the queue consumption after `batchInterval` milliseconds.\n if (isFirstEnqueuedRequest || this.batchDebounce) {\n this.scheduleQueueConsumption(key);\n }\n\n // When amount of requests reaches `batchMax`, trigger the queue consumption without waiting on the `batchInterval`.\n if (batch.size === this.batchMax) {\n this.consumeQueue(key);\n }\n\n return () => {\n // If this is last subscriber for this request, remove request from queue\n if (\n requestCopy.subscribers.delete(observer) &&\n requestCopy.subscribers.size < 1\n ) {\n // If this is last request from queue, remove queue entirely\n if (batch.delete(requestCopy) && batch.size < 1) {\n this.consumeQueue(key);\n // If queue was in flight, cancel it\n batch.subscription?.unsubscribe();\n }\n }\n };\n });\n }\n\n return requestCopy.observable;\n }\n\n // Consumes the queue.\n // Returns a list of promises (one for each query).\n public consumeQueue(\n key: string = \"\"\n ): (Observable<ApolloLink.Result> | undefined)[] | undefined {\n const batch = this.batchesByKey.get(key);\n // Delete this batch and process it below.\n this.batchesByKey.delete(key);\n if (!batch || !batch.size) {\n // No requests to be processed.\n return;\n }\n\n const operations: QueuedRequest[\"operation\"][] = [];\n const forwards: QueuedRequest[\"forward\"][] = [];\n const observables: QueuedRequest[\"observable\"][] = [];\n const nexts: QueuedRequest[\"next\"][] = [];\n const errors: QueuedRequest[\"error\"][] = [];\n const completes: QueuedRequest[\"complete\"][] = [];\n\n // Even though batch is a Set, it preserves the order of first insertion\n // when iterating (per ECMAScript specification), so these requests will be\n // handled in the order they were enqueued (minus any deleted ones).\n batch.forEach((request) => {\n operations.push(request.operation);\n forwards.push(request.forward);\n observables.push(request.observable);\n nexts.push(request.next);\n errors.push(request.error);\n completes.push(request.complete);\n });\n\n const batchedObservable = this.batchHandler(operations, forwards);\n\n const onError = (error: Error) => {\n //each callback list in batch\n errors.forEach((rejecters) => {\n if (rejecters) {\n //each subscriber to request\n rejecters.forEach((e) => e(error));\n }\n });\n };\n\n batch.subscription = batchedObservable.subscribe({\n next: (results) => {\n if (!Array.isArray(results)) {\n results = [results];\n }\n\n if (nexts.length !== results.length) {\n const error = new Error(\n `server returned results with length ${results.length}, expected length of ${nexts.length}`\n );\n (error as any).result = results;\n\n return onError(error);\n }\n\n results.forEach((result, index) => {\n if (nexts[index]) {\n nexts[index].forEach((next) => next(result));\n }\n });\n },\n error: onError,\n complete: () => {\n completes.forEach((complete) => {\n if (complete) {\n //each subscriber to request\n complete.forEach((c) => c());\n }\n });\n },\n });\n\n return observables;\n }\n\n private scheduleQueueConsumption(key: string): void {\n clearTimeout(this.scheduledBatchTimerByKey.get(key));\n this.scheduledBatchTimerByKey.set(\n key,\n setTimeout(() => {\n this.consumeQueue(key);\n this.scheduledBatchTimerByKey.delete(key);\n }, this.batchInterval)\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"batching.js","sourceRoot":"","sources":["../../../src/link/batch/batching.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAyBlC,qFAAqF;AACrF,2FAA2F;AAC3F,kBAAkB;AAClB,MAAM,OAAO,gBAAgB;IAC3B,oEAAoE;IAC5D,YAAY,GAAG,IAAI,GAAG,EAAwB,CAAC;IAE/C,wBAAwB,GAAG,IAAI,GAAG,EAGvC,CAAC;IACI,aAAa,CAAW;IACxB,aAAa,CAAU;IACvB,QAAQ,CAAS;IAEzB,oEAAoE;IAC5D,YAAY,CAAyB;IACrC,QAAQ,CAA8C;IAE9D,YAAY,EACV,aAAa,EACb,aAAa,EACb,QAAQ,EACR,YAAY,EACZ,QAAQ,GAOT;QACC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,CAAC,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;IACzC,CAAC;IAEM,cAAc,CACnB,OAAyB;QAEzB,MAAM,WAAW,GAAkB;YACjC,GAAG,OAAO;YACV,IAAI,EAAE,EAAE;YACR,KAAK,EAAE,EAAE;YACT,QAAQ,EAAE,EAAE;YACZ,WAAW,EAAE,IAAI,GAAG,EAAE;SACvB,CAAC;QAEF,MAAM,GAAG,GAAG,IAAI,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AAI,CAAC,aAAa,CAAC,CACvB,CAAC;IACJ,CAAC;CACF","sourcesContent":["import type { Subscription } from \"rxjs\";\nimport { Observable } from \"rxjs\";\n\nimport type { ApolloLink } from \"@apollo/client/link\";\n\nimport type { BatchLink } from \"./batchLink.js\";\n\nexport interface BatchableRequest {\n operation: ApolloLink.Operation;\n forward: ApolloLink.ForwardFunction;\n}\n\ntype QueuedRequest = BatchableRequest & {\n observable?: Observable<ApolloLink.Result>;\n next: Array<(result: ApolloLink.Result) => void>;\n error: Array<(error: Error) => void>;\n complete: Array<() => void>;\n subscribers: Set<object>;\n};\n\n// Batches are primarily a Set<QueuedRequest>, but may have other optional\n// properties, such as batch.subscription.\ntype RequestBatch = Set<QueuedRequest> & {\n subscription?: Subscription;\n};\n\n// QueryBatcher doesn't fire requests immediately. Requests that were enqueued within\n// a certain amount of time (configurable through `batchInterval`) will be batched together\n// into one query.\nexport class OperationBatcher {\n // Queue on which the QueryBatcher will operate on a per-tick basis.\n private batchesByKey = new Map<string, RequestBatch>();\n\n private scheduledBatchTimerByKey = new Map<\n string,\n ReturnType<typeof setTimeout>\n >();\n private batchDebounce?: boolean;\n private batchInterval?: number;\n private batchMax: number;\n\n //This function is called to the queries in the queue to the server.\n private batchHandler: BatchLink.BatchHandler;\n private batchKey: (operation: ApolloLink.Operation) => string;\n\n constructor({\n batchDebounce,\n batchInterval,\n batchMax,\n batchHandler,\n batchKey,\n }: {\n batchDebounce?: boolean;\n batchInterval?: number;\n batchMax?: number;\n batchHandler: BatchLink.BatchHandler;\n batchKey?: (operation: ApolloLink.Operation) => string;\n }) {\n this.batchDebounce = batchDebounce;\n this.batchInterval = batchInterval;\n this.batchMax = batchMax || 0;\n this.batchHandler = batchHandler;\n this.batchKey = batchKey || (() => \"\");\n }\n\n public enqueueRequest(\n request: BatchableRequest\n ): Observable<ApolloLink.Result> {\n const requestCopy: QueuedRequest = {\n ...request,\n next: [],\n error: [],\n complete: [],\n subscribers: new Set(),\n };\n\n const key = this.batchKey(request.operation);\n\n if (!requestCopy.observable) {\n requestCopy.observable = new Observable<ApolloLink.Result>((observer) => {\n let batch = this.batchesByKey.get(key)!;\n if (!batch) this.batchesByKey.set(key, (batch = new Set()));\n\n // These booleans seem to me (@benjamn) like they might always be the\n // same (and thus we could do with only one of them), but I'm not 100%\n // sure about that.\n const isFirstEnqueuedRequest = batch.size === 0;\n const isFirstSubscriber = requestCopy.subscribers.size === 0;\n requestCopy.subscribers.add(observer);\n if (isFirstSubscriber) {\n batch.add(requestCopy);\n }\n\n // called for each subscriber, so need to save all listeners (next, error, complete)\n if (observer.next) {\n requestCopy.next.push(observer.next.bind(observer));\n }\n\n if (observer.error) {\n requestCopy.error.push(observer.error.bind(observer));\n }\n\n if (observer.complete) {\n requestCopy.complete.push(observer.complete.bind(observer));\n }\n\n // The first enqueued request triggers the queue consumption after `batchInterval` milliseconds.\n if (isFirstEnqueuedRequest || this.batchDebounce) {\n this.scheduleQueueConsumption(key);\n }\n\n // When amount of requests reaches `batchMax`, trigger the queue consumption without waiting on the `batchInterval`.\n if (batch.size === this.batchMax) {\n this.consumeQueue(key);\n }\n\n return () => {\n // If this is last subscriber for this request, remove request from queue\n if (\n requestCopy.subscribers.delete(observer) &&\n requestCopy.subscribers.size < 1\n ) {\n // If this is last request from queue, remove queue entirely\n if (batch.delete(requestCopy) && batch.size < 1) {\n this.consumeQueue(key);\n // If queue was in flight, cancel it\n batch.subscription?.unsubscribe();\n }\n }\n };\n });\n }\n\n return requestCopy.observable;\n }\n\n // Consumes the queue.\n // Returns a list of promises (one for each query).\n public consumeQueue(\n key: string = \"\"\n ): (Observable<ApolloLink.Result> | undefined)[] | undefined {\n const batch = this.batchesByKey.get(key);\n // Delete this batch and process it below.\n this.batchesByKey.delete(key);\n if (!batch || !batch.size) {\n // No requests to be processed.\n return;\n }\n\n const operations: QueuedRequest[\"operation\"][] = [];\n const forwards: QueuedRequest[\"forward\"][] = [];\n const observables: QueuedRequest[\"observable\"][] = [];\n const nexts: QueuedRequest[\"next\"][] = [];\n const errors: QueuedRequest[\"error\"][] = [];\n const completes: QueuedRequest[\"complete\"][] = [];\n\n // Even though batch is a Set, it preserves the order of first insertion\n // when iterating (per ECMAScript specification), so these requests will be\n // handled in the order they were enqueued (minus any deleted ones).\n batch.forEach((request) => {\n operations.push(request.operation);\n forwards.push(request.forward);\n observables.push(request.observable);\n nexts.push(request.next);\n errors.push(request.error);\n completes.push(request.complete);\n });\n\n const batchedObservable = this.batchHandler(operations, forwards);\n\n const onError = (error: Error) => {\n //each callback list in batch\n errors.forEach((rejecters) => {\n if (rejecters) {\n //each subscriber to request\n rejecters.forEach((e) => e(error));\n }\n });\n };\n\n batch.subscription = batchedObservable.subscribe({\n next: (results) => {\n if (!Array.isArray(results)) {\n results = [results];\n }\n\n if (nexts.length !== results.length) {\n const error = new Error(\n `server returned results with length ${results.length}, expected length of ${nexts.length}`\n );\n (error as any).result = results;\n\n return onError(error);\n }\n\n results.forEach((result, index) => {\n if (nexts[index]) {\n nexts[index].forEach((next) => next(result));\n }\n });\n },\n error: onError,\n complete: () => {\n completes.forEach((complete) => {\n if (complete) {\n //each subscriber to request\n complete.forEach((c) => c());\n }\n });\n },\n });\n\n return observables;\n }\n\n private scheduleQueueConsumption(key: string): void {\n clearTimeout(this.scheduledBatchTimerByKey.get(key));\n this.scheduledBatchTimerByKey.set(\n key,\n setTimeout(() => {\n this.consumeQueue(key);\n this.scheduledBatchTimerByKey.delete(key);\n }, this.batchInterval)\n );\n }\n}\n"]}
|
package/link/batch/index.d.ts
CHANGED
package/link/batch/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { BatchLink
|
|
1
|
+
export { BatchLink } from "./batchLink.js";
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/link/batch/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/link/batch/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/link/batch/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["export { BatchLink } from \"./batchLink.js\";\n"]}
|
|
@@ -4,12 +4,52 @@ import { BatchLink } from "@apollo/client/link/batch";
|
|
|
4
4
|
import { ClientAwarenessLink } from "@apollo/client/link/client-awareness";
|
|
5
5
|
import type { HttpLink } from "@apollo/client/link/http";
|
|
6
6
|
export declare namespace BatchHttpLink {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Options provided to the `BatchHttpLink` constructor.
|
|
9
|
+
*/
|
|
10
|
+
interface Options extends BatchLink.Shared.Options, HttpLink.Shared.Options {
|
|
11
|
+
/**
|
|
12
|
+
* The maximum number of operations to include in a single batch.
|
|
13
|
+
*
|
|
14
|
+
* @defaultValue 10
|
|
15
|
+
*/
|
|
16
|
+
batchMax?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Options passed to `BatchHttpLink` through [request context](https://apollographql.com/docs/react/api/link/introduction#managing-context). Previous
|
|
20
|
+
* non-terminating links in the link chain also can set these values to
|
|
21
|
+
* customize the behavior of `BatchHttpLink` for each operation.
|
|
22
|
+
*
|
|
23
|
+
* > [!NOTE]
|
|
24
|
+
* > Some of these values can also be provided to the `BatchHttpLink` constructor.
|
|
25
|
+
* > If a value is provided to both, the value in `context` takes precedence.
|
|
26
|
+
*/
|
|
27
|
+
interface ContextOptions extends HttpLink.ContextOptions {
|
|
28
|
+
}
|
|
9
29
|
}
|
|
10
30
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
31
|
+
* `BatchHttpLink` is a terminating link that batches array of individual
|
|
32
|
+
* GraphQL operations into a single HTTP request that's sent to a single GraphQL
|
|
33
|
+
* endpoint. It combines the functionality of `BaseBatchHttpLink` and
|
|
34
|
+
* `ClientAwarenessLink` into a single link.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
*
|
|
38
|
+
* If you use `BatchHttpLink` instead of `HttpLink` as your terminating link,
|
|
39
|
+
* Apollo Client automatically batches executed GraphQL operations and transmits
|
|
40
|
+
* them to your server according to the batching options you provide.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { BatchHttpLink } from "@apollo/client/link/batch-http";
|
|
46
|
+
*
|
|
47
|
+
* const link = new BatchHttpLink({
|
|
48
|
+
* uri: "http://localhost:4000/graphql",
|
|
49
|
+
* batchMax: 5, // No more than 5 operations per batch
|
|
50
|
+
* batchInterval: 20, // Wait no more than 20ms after first batched operation
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
13
53
|
*/
|
|
14
54
|
export declare class BatchHttpLink extends ApolloLink {
|
|
15
55
|
constructor(options?: BatchHttpLink.Options & ClientAwarenessLink.Options);
|
|
@@ -22,4 +62,4 @@ export declare class BaseBatchHttpLink extends ApolloLink {
|
|
|
22
62
|
constructor(fetchParams?: BatchHttpLink.Options);
|
|
23
63
|
request(operation: ApolloLink.Operation, forward: ApolloLink.ForwardFunction): Observable<ApolloLink.Result>;
|
|
24
64
|
}
|
|
25
|
-
//# sourceMappingURL=batchHttpLink.d.ts.map
|
|
65
|
+
//# sourceMappingURL=batchHttpLink.d.ts.map
|
|
@@ -2,15 +2,35 @@ import { Observable, throwError } from "rxjs";
|
|
|
2
2
|
import { ApolloLink } from "@apollo/client/link";
|
|
3
3
|
import { BatchLink } from "@apollo/client/link/batch";
|
|
4
4
|
import { ClientAwarenessLink } from "@apollo/client/link/client-awareness";
|
|
5
|
-
import { checkFetcher, defaultPrinter, fallbackHttpConfig, parseAndCheckHttpResponse, selectHttpOptionsAndBodyInternal, selectURI,
|
|
5
|
+
import { checkFetcher, defaultPrinter, fallbackHttpConfig, parseAndCheckHttpResponse, selectHttpOptionsAndBodyInternal, selectURI, } from "@apollo/client/link/http";
|
|
6
6
|
import { filterOperationVariables } from "@apollo/client/link/utils";
|
|
7
7
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
8
8
|
import { compact } from "@apollo/client/utilities/internal";
|
|
9
9
|
import { maybe } from "@apollo/client/utilities/internal/globals";
|
|
10
10
|
const backupFetch = maybe(() => fetch);
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* `BatchHttpLink` is a terminating link that batches array of individual
|
|
13
|
+
* GraphQL operations into a single HTTP request that's sent to a single GraphQL
|
|
14
|
+
* endpoint. It combines the functionality of `BaseBatchHttpLink` and
|
|
15
|
+
* `ClientAwarenessLink` into a single link.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
*
|
|
19
|
+
* If you use `BatchHttpLink` instead of `HttpLink` as your terminating link,
|
|
20
|
+
* Apollo Client automatically batches executed GraphQL operations and transmits
|
|
21
|
+
* them to your server according to the batching options you provide.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { BatchHttpLink } from "@apollo/client/link/batch-http";
|
|
27
|
+
*
|
|
28
|
+
* const link = new BatchHttpLink({
|
|
29
|
+
* uri: "http://localhost:4000/graphql",
|
|
30
|
+
* batchMax: 5, // No more than 5 operations per batch
|
|
31
|
+
* batchInterval: 20, // Wait no more than 20ms after first batched operation
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
14
34
|
*/
|
|
15
35
|
export class BatchHttpLink extends ApolloLink {
|
|
16
36
|
constructor(options = {}) {
|
|
@@ -70,7 +90,7 @@ export class BaseBatchHttpLink extends ApolloLink {
|
|
|
70
90
|
return throwError(() => new Error("apollo-link-batch-http does not support GET requests"));
|
|
71
91
|
}
|
|
72
92
|
try {
|
|
73
|
-
options.body =
|
|
93
|
+
options.body = JSON.stringify(loadedBody);
|
|
74
94
|
}
|
|
75
95
|
catch (parseError) {
|
|
76
96
|
return throwError(() => parseError);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
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AAE,OAAO,CAAC,YAAY;wBAC7B,WAAW,EAAE,OAAO,CAAC,WAAW;wBAChC,OAAO,EAAE,OAAO,CAAC,OAAO;qBACzB,CAAC;oBAEF,4CAA4C;oBAC5C,OAAO,SAAS,CAAC,SAAS,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;gBACnE,CAAC,CAAC,CAAC;QAEL,IAAI,CAAC,OAAO,GAAG,IAAI,SAAS,CAAC;YAC3B,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,QAAQ;YACR,YAAY;SACb,CAAC,CAAC;IACL,CAAC;IAEM,OAAO,CACZ,SAA+B,EAC/B,OAAmC;QAEnC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;CACF","sourcesContent":["import { Observable, throwError } from \"rxjs\";\n\nimport { ApolloLink } from \"@apollo/client/link\";\nimport type { BatchHandler } from \"@apollo/client/link/batch\";\nimport { BatchLink } from \"@apollo/client/link/batch\";\nimport { ClientAwarenessLink } from \"@apollo/client/link/client-awareness\";\nimport type { HttpLink } from \"@apollo/client/link/http\";\nimport {\n checkFetcher,\n defaultPrinter,\n fallbackHttpConfig,\n parseAndCheckHttpResponse,\n selectHttpOptionsAndBodyInternal,\n selectURI,\n serializeFetchParameter,\n} from \"@apollo/client/link/http\";\nimport { filterOperationVariables } from \"@apollo/client/link/utils\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport { compact } from \"@apollo/client/utilities/internal\";\nimport { maybe } from \"@apollo/client/utilities/internal/globals\";\n\nexport declare namespace BatchHttpLink {\n export type Options = Pick<\n BatchLink.Options,\n \"batchMax\" | \"batchDebounce\" | \"batchInterval\" | \"batchKey\"\n > &\n Omit<HttpLink.Options, \"useGETForQueries\">;\n\n export type ContextOptions = HttpLink.ContextOptions;\n}\n\nconst backupFetch = maybe(() => fetch);\n\n/**\n * Transforms Operation for into HTTP results.\n * context can include the headers property, which will be passed to the fetch function\n */\nexport class BatchHttpLink extends ApolloLink {\n constructor(\n options: BatchHttpLink.Options & ClientAwarenessLink.Options = {}\n ) {\n const { left, right, request } = ApolloLink.from([\n new ClientAwarenessLink(options),\n new BaseBatchHttpLink(options),\n ]);\n super(request);\n Object.assign(this, { left, right });\n }\n}\nexport class BaseBatchHttpLink extends ApolloLink {\n private batchDebounce?: boolean;\n private batchInterval: number;\n private batchMax: number;\n private batcher: ApolloLink;\n\n constructor(fetchParams?: BatchHttpLink.Options) {\n super();\n\n let {\n uri = \"/graphql\",\n // use default global fetch if nothing is passed in\n fetch: preferredFetch,\n print = defaultPrinter,\n includeExtensions,\n preserveHeaderCase,\n batchInterval,\n batchDebounce,\n batchMax,\n batchKey,\n includeUnusedVariables = false,\n ...requestOptions\n } = fetchParams || ({} as BatchHttpLink.Options);\n\n if (__DEV__) {\n // Make sure at least one of preferredFetch, window.fetch, or backupFetch\n // is defined, so requests won't fail at runtime.\n checkFetcher(preferredFetch || backupFetch);\n }\n\n const linkConfig = {\n http: compact({ includeExtensions, preserveHeaderCase }),\n options: requestOptions.fetchOptions,\n credentials: requestOptions.credentials,\n headers: requestOptions.headers,\n };\n\n this.batchDebounce = batchDebounce;\n this.batchInterval = batchInterval || 10;\n this.batchMax = batchMax || 10;\n\n const batchHandler: BatchHandler = (operations) => {\n const chosenURI = selectURI(operations[0], uri);\n\n const context = operations[0].getContext();\n\n const contextConfig = {\n http: context.http,\n options: context.fetchOptions,\n credentials: context.credentials,\n headers: context.headers,\n };\n\n //uses fallback, link, and then context to build options\n const optsAndBody = operations.map((operation) => {\n const result = selectHttpOptionsAndBodyInternal(\n operation,\n print,\n fallbackHttpConfig,\n linkConfig,\n contextConfig\n );\n\n if (result.body.variables && !includeUnusedVariables) {\n result.body.variables = filterOperationVariables(\n result.body.variables,\n operation.query\n );\n }\n\n return result;\n });\n\n const loadedBody = optsAndBody.map(({ body }) => body);\n const options = optsAndBody[0].options;\n\n // There's no spec for using GET with batches.\n if (options.method === \"GET\") {\n return throwError(\n () =>\n new Error(\"apollo-link-batch-http does not support GET requests\")\n );\n }\n\n try {\n (options as any).body = serializeFetchParameter(loadedBody, \"Payload\");\n } catch (parseError) {\n return throwError(() => parseError);\n }\n\n let controller: AbortController | undefined;\n if (!options.signal && typeof AbortController !== \"undefined\") {\n controller = new AbortController();\n options.signal = controller.signal;\n }\n\n return new Observable((observer) => {\n // Prefer BatchHttpLink.Options.fetch (preferredFetch) if provided, and\n // otherwise fall back to the *current* global window.fetch function\n // (see issue #7832), or (if all else fails) the backupFetch function we\n // saved when this module was first evaluated. This last option protects\n // against the removal of window.fetch, which is unlikely but not\n // impossible.\n const currentFetch =\n preferredFetch || maybe(() => fetch) || backupFetch;\n\n currentFetch!(chosenURI, options)\n .then((response) => {\n // Make the raw response available in the context.\n operations.forEach((operation) =>\n operation.setContext({ response })\n );\n return response;\n })\n .then(parseAndCheckHttpResponse(operations))\n .then((result) => {\n controller = undefined;\n // we have data and can send it to back up the link chain\n observer.next(result);\n observer.complete();\n return result;\n })\n .catch((err) => {\n controller = undefined;\n observer.error(err);\n });\n\n return () => {\n // XXX support canceling this request\n // https://developers.google.com/web/updates/2017/09/abortable-fetch\n if (controller) controller.abort();\n };\n });\n };\n\n batchKey =\n batchKey ||\n ((operation: ApolloLink.Operation) => {\n const context = operation.getContext();\n\n const contextConfig = {\n http: context.http,\n options: context.fetchOptions,\n credentials: context.credentials,\n headers: context.headers,\n };\n\n //may throw error if config not serializable\n return selectURI(operation, uri) + JSON.stringify(contextConfig);\n });\n\n this.batcher = new BatchLink({\n batchDebounce: this.batchDebounce,\n batchInterval: this.batchInterval,\n batchMax: this.batchMax,\n batchKey,\n batchHandler,\n });\n }\n\n public request(\n operation: ApolloLink.Operation,\n forward: ApolloLink.ForwardFunction\n ): Observable<ApolloLink.Result> {\n return this.batcher.request(oper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AI,EAAE,OAAO,CAAC,IAAI;wBAClB,OAAO,EAAE,OAAO,CAAC,YAAY;wBAC7B,WAAW,EAAE,OAAO,CAAC,WAAW;wBAChC,OAAO,EAAE,OAAO,CAAC,OAAO;qBACzB,CAAC;oBAEF,4CAA4C;oBAC5C,OAAO,SAAS,CAAC,SAAS,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;gBACnE,CAAC,CAAC,CAAC;QAEL,IAAI,CAAC,OAAO,GAAG,IAAI,SAAS,CAAC;YAC3B,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,aAAa,EAAE,IAAI,CAAC,aAAa;YACjC,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,QAAQ;YACR,YAAY;SACb,CAAC,CAAC;IACL,CAAC;IAEM,OAAO,CACZ,SAA+B,EAC/B,OAAmC;QAEnC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;CACF","sourcesContent":["import { Observable, throwError } from \"rxjs\";\n\nimport { ApolloLink } from \"@apollo/client/link\";\nimport { BatchLink } from \"@apollo/client/link/batch\";\nimport { ClientAwarenessLink } from \"@apollo/client/link/client-awareness\";\nimport type { HttpLink } from \"@apollo/client/link/http\";\nimport {\n checkFetcher,\n defaultPrinter,\n fallbackHttpConfig,\n parseAndCheckHttpResponse,\n selectHttpOptionsAndBodyInternal,\n selectURI,\n} from \"@apollo/client/link/http\";\nimport { filterOperationVariables } from \"@apollo/client/link/utils\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport { compact } from \"@apollo/client/utilities/internal\";\nimport { maybe } from \"@apollo/client/utilities/internal/globals\";\n\nexport declare namespace BatchHttpLink {\n /**\n * Options provided to the `BatchHttpLink` constructor.\n */\n export interface Options\n extends BatchLink.Shared.Options,\n HttpLink.Shared.Options {\n /** {@inheritDoc @apollo/client/link/batch!BatchLink.Shared.Options#batchMax:member {\"defaultValue\": 10}} */\n batchMax?: number;\n }\n\n /**\n * Options passed to `BatchHttpLink` through [request context](https://apollographql.com/docs/react/api/link/introduction#managing-context). Previous\n * non-terminating links in the link chain also can set these values to\n * customize the behavior of `BatchHttpLink` for each operation.\n *\n * > [!NOTE]\n * > Some of these values can also be provided to the `BatchHttpLink` constructor.\n * > If a value is provided to both, the value in `context` takes precedence.\n */\n export interface ContextOptions extends HttpLink.ContextOptions {}\n}\n\nconst backupFetch = maybe(() => fetch);\n\n/**\n * `BatchHttpLink` is a terminating link that batches array of individual\n * GraphQL operations into a single HTTP request that's sent to a single GraphQL\n * endpoint. It combines the functionality of `BaseBatchHttpLink` and\n * `ClientAwarenessLink` into a single link.\n *\n * @remarks\n *\n * If you use `BatchHttpLink` instead of `HttpLink` as your terminating link,\n * Apollo Client automatically batches executed GraphQL operations and transmits\n * them to your server according to the batching options you provide.\n *\n * @example\n *\n * ```ts\n * import { BatchHttpLink } from \"@apollo/client/link/batch-http\";\n *\n * const link = new BatchHttpLink({\n * uri: \"http://localhost:4000/graphql\",\n * batchMax: 5, // No more than 5 operations per batch\n * batchInterval: 20, // Wait no more than 20ms after first batched operation\n * });\n * ```\n */\nexport class BatchHttpLink extends ApolloLink {\n constructor(\n options: BatchHttpLink.Options & ClientAwarenessLink.Options = {}\n ) {\n const { left, right, request } = ApolloLink.from([\n new ClientAwarenessLink(options),\n new BaseBatchHttpLink(options),\n ]);\n super(request);\n Object.assign(this, { left, right });\n }\n}\nexport class BaseBatchHttpLink extends ApolloLink {\n private batchDebounce?: boolean;\n private batchInterval: number;\n private batchMax: number;\n private batcher: ApolloLink;\n\n constructor(fetchParams?: BatchHttpLink.Options) {\n super();\n\n let {\n uri = \"/graphql\",\n // use default global fetch if nothing is passed in\n fetch: preferredFetch,\n print = defaultPrinter,\n includeExtensions,\n preserveHeaderCase,\n batchInterval,\n batchDebounce,\n batchMax,\n batchKey,\n includeUnusedVariables = false,\n ...requestOptions\n } = fetchParams || ({} as BatchHttpLink.Options);\n\n if (__DEV__) {\n // Make sure at least one of preferredFetch, window.fetch, or backupFetch\n // is defined, so requests won't fail at runtime.\n checkFetcher(preferredFetch || backupFetch);\n }\n\n const linkConfig = {\n http: compact({ includeExtensions, preserveHeaderCase }),\n options: requestOptions.fetchOptions,\n credentials: requestOptions.credentials,\n headers: requestOptions.headers,\n };\n\n this.batchDebounce = batchDebounce;\n this.batchInterval = batchInterval || 10;\n this.batchMax = batchMax || 10;\n\n const batchHandler: BatchLink.BatchHandler = (operations) => {\n const chosenURI = selectURI(operations[0], uri);\n\n const context = operations[0].getContext();\n\n const contextConfig = {\n http: context.http,\n options: context.fetchOptions,\n credentials: context.credentials,\n headers: context.headers,\n };\n\n //uses fallback, link, and then context to build options\n const optsAndBody = operations.map((operation) => {\n const result = selectHttpOptionsAndBodyInternal(\n operation,\n print,\n fallbackHttpConfig,\n linkConfig,\n contextConfig\n );\n\n if (result.body.variables && !includeUnusedVariables) {\n result.body.variables = filterOperationVariables(\n result.body.variables,\n operation.query\n );\n }\n\n return result;\n });\n\n const loadedBody = optsAndBody.map(({ body }) => body);\n const options = optsAndBody[0].options;\n\n // There's no spec for using GET with batches.\n if (options.method === \"GET\") {\n return throwError(\n () =>\n new Error(\"apollo-link-batch-http does not support GET requests\")\n );\n }\n\n try {\n (options as any).body = JSON.stringify(loadedBody);\n } catch (parseError) {\n return throwError(() => parseError);\n }\n\n let controller: AbortController | undefined;\n if (!options.signal && typeof AbortController !== \"undefined\") {\n controller = new AbortController();\n options.signal = controller.signal;\n }\n\n return new Observable((observer) => {\n // Prefer BatchHttpLink.Options.fetch (preferredFetch) if provided, and\n // otherwise fall back to the *current* global window.fetch function\n // (see issue #7832), or (if all else fails) the backupFetch function we\n // saved when this module was first evaluated. This last option protects\n // against the removal of window.fetch, which is unlikely but not\n // impossible.\n const currentFetch =\n preferredFetch || maybe(() => fetch) || backupFetch;\n\n currentFetch!(chosenURI, options)\n .then((response) => {\n // Make the raw response available in the context.\n operations.forEach((operation) =>\n operation.setContext({ response })\n );\n return response;\n })\n .then(parseAndCheckHttpResponse(operations))\n .then((result) => {\n controller = undefined;\n // we have data and can send it to back up the link chain\n observer.next(result);\n observer.complete();\n return result;\n })\n .catch((err) => {\n controller = undefined;\n observer.error(err);\n });\n\n return () => {\n // XXX support canceling this request\n // https://developers.google.com/web/updates/2017/09/abortable-fetch\n if (controller) controller.abort();\n };\n });\n };\n\n batchKey =\n batchKey ||\n ((operation: ApolloLink.Operation) => {\n const context = operation.getContext();\n\n const contextConfig = {\n http: context.http,\n options: context.fetchOptions,\n credentials: context.credentials,\n headers: context.headers,\n };\n\n //may throw error if config not serializable\n return selectURI(operation, uri) + JSON.stringify(contextConfig);\n });\n\n this.batcher = new BatchLink({\n batchDebounce: this.batchDebounce,\n batchInterval: this.batchInterval,\n batchMax: this.batchMax,\n batchKey,\n batchHandler,\n });\n }\n\n public request(\n operation: ApolloLink.Operation,\n forward: ApolloLink.ForwardFunction\n ): Observable<ApolloLink.Result> {\n return this.batcher.request(operation, forward);\n }\n}\n"]}
|
|
@@ -46,16 +46,49 @@ export declare namespace ClientAwarenessLink {
|
|
|
46
46
|
* and Apollo Server logs (and other monitoring or analytics tools) by adding
|
|
47
47
|
* information about the your application to outgoing requests.
|
|
48
48
|
*/
|
|
49
|
-
clientAwareness?: ClientAwarenessOptions;
|
|
49
|
+
clientAwareness?: ClientAwarenessLink.ClientAwarenessOptions;
|
|
50
50
|
/**
|
|
51
51
|
* Configures the "enhanced client awareness" feature.
|
|
52
52
|
* This feature allows you to identify the version of the Apollo Client library
|
|
53
53
|
* used in your application in Apollo Studio (and other monitoring or analytics tools)
|
|
54
54
|
* by adding information about the Apollo Client library to outgoing requests.
|
|
55
55
|
*/
|
|
56
|
-
enhancedClientAwareness?: EnhancedClientAwarenessOptions;
|
|
56
|
+
enhancedClientAwareness?: ClientAwarenessLink.EnhancedClientAwarenessOptions;
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* `ClientAwarenessLink` provides support for providing client awareness
|
|
61
|
+
* features.
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
*
|
|
65
|
+
* Client awareness adds identifying information about the client to HTTP
|
|
66
|
+
* requests for use with metrics reporting tools, such as [Apollo GraphOS](https://apollographql.com/docs/graphos/platform).
|
|
67
|
+
* It is included in the functionality of [`HttpLink`](https://apollographql.com/docs/react/api/link/apollo-link-http) by default.
|
|
68
|
+
*
|
|
69
|
+
* Client awareness distinguishes between user-provided client awareness
|
|
70
|
+
* (provided by the `clientAwareness` option) and enhanced client awareness
|
|
71
|
+
* (provided by the `enhancedClientAwareness` option). User-provided client
|
|
72
|
+
* awareness enables you to set a customized client name and version for
|
|
73
|
+
* identification in metrics reporting tools. Enhanced client awareness enables
|
|
74
|
+
* the identification of the Apollo Client package name and version.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { ClientAwarenessLink } from "@apollo/client/link/client-awareness";
|
|
80
|
+
*
|
|
81
|
+
* const link = new ClientAwarenessLink({
|
|
82
|
+
* clientAwareness: {
|
|
83
|
+
* name: "My Client",
|
|
84
|
+
* version: "1",
|
|
85
|
+
* },
|
|
86
|
+
* enhancedClientAwareness: {
|
|
87
|
+
* transport: "extensions",
|
|
88
|
+
* },
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
59
92
|
export declare class ClientAwarenessLink extends ApolloLink {
|
|
60
93
|
constructor(constructorOptions?: ClientAwarenessLink.Options);
|
|
61
94
|
}
|
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
import { ApolloLink } from "@apollo/client/link";
|
|
2
2
|
import { compact } from "@apollo/client/utilities/internal";
|
|
3
|
+
/**
|
|
4
|
+
* `ClientAwarenessLink` provides support for providing client awareness
|
|
5
|
+
* features.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
*
|
|
9
|
+
* Client awareness adds identifying information about the client to HTTP
|
|
10
|
+
* requests for use with metrics reporting tools, such as [Apollo GraphOS](https://apollographql.com/docs/graphos/platform).
|
|
11
|
+
* It is included in the functionality of [`HttpLink`](https://apollographql.com/docs/react/api/link/apollo-link-http) by default.
|
|
12
|
+
*
|
|
13
|
+
* Client awareness distinguishes between user-provided client awareness
|
|
14
|
+
* (provided by the `clientAwareness` option) and enhanced client awareness
|
|
15
|
+
* (provided by the `enhancedClientAwareness` option). User-provided client
|
|
16
|
+
* awareness enables you to set a customized client name and version for
|
|
17
|
+
* identification in metrics reporting tools. Enhanced client awareness enables
|
|
18
|
+
* the identification of the Apollo Client package name and version.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { ClientAwarenessLink } from "@apollo/client/link/client-awareness";
|
|
24
|
+
*
|
|
25
|
+
* const link = new ClientAwarenessLink({
|
|
26
|
+
* clientAwareness: {
|
|
27
|
+
* name: "My Client",
|
|
28
|
+
* version: "1",
|
|
29
|
+
* },
|
|
30
|
+
* enhancedClientAwareness: {
|
|
31
|
+
* transport: "extensions",
|
|
32
|
+
* },
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
3
36
|
export class ClientAwarenessLink extends ApolloLink {
|
|
4
37
|
constructor(constructorOptions = {}) {
|
|
5
38
|
super((operation, forward) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientAwarenessLink.js","sourceRoot":"","sources":["../../../src/link/client-awareness/ClientAwarenessLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AA6D5D,MAAM,OAAO,mBAAoB,SAAQ,UAAU;IACjD,YAAY,qBAAkD,EAAE;QAC9D,KAAK,CAAC,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE;YAC3B,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC;YAEhC,MAAM,aAAa,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC,aAAa,CAAC;YAC3D,MAAM,OAAO,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC;YACvC,CAAC;gBACC,MAAM,EACJ,IAAI,EACJ,OAAO,EACP,SAAS,GAAG,SAAS,GACtB,GAAG,OAAO,CACT,EAAE,EACF,aAAa,CAAC,eAAe,EAC7B,kBAAkB,CAAC,eAAe,EAClC,OAAO,CAAC,eAAe,CACxB,CAAC;gBAEF,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;oBAC5B,SAAS,CAAC,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE;wBAC/C,OAAO;4BACL,OAAO,EAAE,OAAO;4BACd,8EAA8E;4BAC9E;gCACE,2BAA2B,EAAE,IAAI;gCACjC,8BAA8B,EAAE,OAAO;6BACxC,EACD,OAAO,CACR;yBACF,CAAC;oBACJ,CAAC,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;YACD,CAAC;gBACC,MAAM,EAAE,SAAS,GAAG,YAAY,EAAE,GAAG,OAAO,CAC1C,EAAE,EACF,aAAa,CAAC,uBAAuB,EACrC,kBAAkB,CAAC,uBAAuB,CAC3C,CAAC;gBACF,IAAI,SAAS,KAAK,YAAY,EAAE,CAAC;oBAC/B,SAAS,CAAC,UAAU,GAAG,OAAO;oBAC5B,+EAA+E;oBAC/E;wBACE,aAAa,EAAE;4BACb,IAAI,EAAE,gBAAgB;4BACtB,OAAO,EAAE,MAAM,CAAC,OAAO;yBACxB;qBACF,EACD,SAAS,CAAC,UAAU,CACrB,CAAC;gBACJ,CAAC;YACH,CAAC;YAED,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { ApolloLink } from \"@apollo/client/link\";\nimport { compact } from \"@apollo/client/utilities/internal\";\n\nexport declare namespace ClientAwarenessLink {\n export interface ClientAwarenessOptions {\n /**\n * A custom name (e.g., `iOS`) that identifies this particular client among your set of clients. Apollo Server and Apollo Studio use this property as part of the [client awareness](https://www.apollographql.com/docs/apollo-server/monitoring/metrics#identifying-distinct-clients) feature.\n *\n * This option can either be set as part of the Apollo Client constructor call or when manually constructing a `HttpLink`, `BatchHttpLink` or `ClientAwarenessLink`.\n */\n name?: string;\n /**\n * A custom version that identifies the current version of this particular client (e.g., `1.2`). Apollo Server and Apollo Studio use this property as part of the [client awareness](https://www.apollographql.com/docs/apollo-server/monitoring/metrics#identifying-distinct-clients) feature.\n *\n * This is **not** the version of Apollo Client that you are using, but rather any version string that helps you differentiate between versions of your client.\n *\n * This option can either be set as part of the Apollo Client constructor call or when manually constructing a `HttpLink`, `BatchHttpLink` or `ClientAwarenessLink`.\n */\n version?: string;\n /**\n * Determines how `name` and `version` are sent in outgoing requests.\n *\n * If `name` and `version` are not provided, this option will be ignored.\n * (These options can either be set as part of the Apollo Client constructor call or when manually constructing a `HttpLink`, `BatchHttpLink` or `ClientAwarenessLink`.)\n *\n * - If set to `\"headers\"`, `name` and `version` will be sent in the request headers as `apollographql-client-name` and `apollographql-client-version`, respectively.\n * - If set to `false`, `name` and `version` will not be included in outgoing requests.\n *\n * @defaultValue \"headers\"\n */\n transport?: \"headers\" | false;\n }\n export interface EnhancedClientAwarenessOptions {\n /**\n * Determines how the the version information of Apollo Client is sent in outgoing requests.\n *\n * - If set to `\"extensions\"`, library `name` and `version` will be sent in an object in the request extensions as `clientLibrary`.\n * - If set to `false`, library name and version will not be included in outgoing requests.\n *\n * @defaultValue \"extensions\"\n */\n transport?: \"extensions\" | false;\n }\n\n export interface Options {\n /**\n * Configures the \"client awareness\" feature.\n * This feature allows you to identify distinct applications in Apollo Studio\n * and Apollo Server logs (and other monitoring or analytics tools) by adding\n * information about the your application to outgoing requests.\n */\n clientAwareness?: ClientAwarenessOptions;\n /**\n * Configures the \"enhanced client awareness\" feature.\n * This feature allows you to identify the version of the Apollo Client library\n * used in your application in Apollo Studio (and other monitoring or analytics tools)\n * by adding information about the Apollo Client library to outgoing requests.\n */\n enhancedClientAwareness?: EnhancedClientAwarenessOptions;\n }\n}\n\nexport class ClientAwarenessLink extends ApolloLink {\n constructor(constructorOptions: ClientAwarenessLink.Options = {}) {\n super((operation, forward) => {\n const client = operation.client;\n\n const clientOptions = client[\"queryManager\"].clientOptions;\n const context = operation.getContext();\n {\n const {\n name,\n version,\n transport = \"headers\",\n } = compact(\n {},\n clientOptions.clientAwareness,\n constructorOptions.clientAwareness,\n context.clientAwareness\n );\n\n if (transport === \"headers\") {\n operation.setContext(({ headers, extensions }) => {\n return {\n headers: compact(\n // setting these first so that they can be overridden by user-provided headers\n {\n \"apollographql-client-name\": name,\n \"apollographql-client-version\": version,\n },\n headers\n ),\n };\n });\n }\n }\n {\n const { transport = \"extensions\" } = compact(\n {},\n clientOptions.enhancedClientAwareness,\n constructorOptions.enhancedClientAwareness\n );\n if (transport === \"extensions\") {\n operation.extensions = compact(\n // setting these first so that it can be overridden by user-provided extensions\n {\n clientLibrary: {\n name: \"@apollo/client\",\n version: client.version,\n },\n },\n operation.extensions\n );\n }\n }\n\n return forward(operation);\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ClientAwarenessLink.js","sourceRoot":"","sources":["../../../src/link/client-awareness/ClientAwarenessLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AA6D5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,OAAO,mBAAoB,SAAQ,UAAU;IACjD,YAAY,qBAAkD,EAAE;QAC9D,KAAK,CAAC,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE;YAC3B,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC;YAEhC,MAAM,aAAa,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC,aAAa,CAAC;YAC3D,MAAM,OAAO,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC;YACvC,CAAC;gBACC,MAAM,EACJ,IAAI,EACJ,OAAO,EACP,SAAS,GAAG,SAAS,GACtB,GAAG,OAAO,CACT,EAAE,EACF,aAAa,CAAC,eAAe,EAC7B,kBAAkB,CAAC,eAAe,EAClC,OAAO,CAAC,eAAe,CACxB,CAAC;gBAEF,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;oBAC5B,SAAS,CAAC,UAAU,CAAC,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE;wBAC/C,OAAO;4BACL,OAAO,EAAE,OAAO;4BACd,8EAA8E;4BAC9E;gCACE,2BAA2B,EAAE,IAAI;gCACjC,8BAA8B,EAAE,OAAO;6BACxC,EACD,OAAO,CACR;yBACF,CAAC;oBACJ,CAAC,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;YACD,CAAC;gBACC,MAAM,EAAE,SAAS,GAAG,YAAY,EAAE,GAAG,OAAO,CAC1C,EAAE,EACF,aAAa,CAAC,uBAAuB,EACrC,kBAAkB,CAAC,uBAAuB,CAC3C,CAAC;gBACF,IAAI,SAAS,KAAK,YAAY,EAAE,CAAC;oBAC/B,SAAS,CAAC,UAAU,GAAG,OAAO;oBAC5B,+EAA+E;oBAC/E;wBACE,aAAa,EAAE;4BACb,IAAI,EAAE,gBAAgB;4BACtB,OAAO,EAAE,MAAM,CAAC,OAAO;yBACxB;qBACF,EACD,SAAS,CAAC,UAAU,CACrB,CAAC;gBACJ,CAAC;YACH,CAAC;YAED,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { ApolloLink } from \"@apollo/client/link\";\nimport { compact } from \"@apollo/client/utilities/internal\";\n\nexport declare namespace ClientAwarenessLink {\n export interface ClientAwarenessOptions {\n /**\n * A custom name (e.g., `iOS`) that identifies this particular client among your set of clients. Apollo Server and Apollo Studio use this property as part of the [client awareness](https://www.apollographql.com/docs/apollo-server/monitoring/metrics#identifying-distinct-clients) feature.\n *\n * This option can either be set as part of the Apollo Client constructor call or when manually constructing a `HttpLink`, `BatchHttpLink` or `ClientAwarenessLink`.\n */\n name?: string;\n /**\n * A custom version that identifies the current version of this particular client (e.g., `1.2`). Apollo Server and Apollo Studio use this property as part of the [client awareness](https://www.apollographql.com/docs/apollo-server/monitoring/metrics#identifying-distinct-clients) feature.\n *\n * This is **not** the version of Apollo Client that you are using, but rather any version string that helps you differentiate between versions of your client.\n *\n * This option can either be set as part of the Apollo Client constructor call or when manually constructing a `HttpLink`, `BatchHttpLink` or `ClientAwarenessLink`.\n */\n version?: string;\n /**\n * Determines how `name` and `version` are sent in outgoing requests.\n *\n * If `name` and `version` are not provided, this option will be ignored.\n * (These options can either be set as part of the Apollo Client constructor call or when manually constructing a `HttpLink`, `BatchHttpLink` or `ClientAwarenessLink`.)\n *\n * - If set to `\"headers\"`, `name` and `version` will be sent in the request headers as `apollographql-client-name` and `apollographql-client-version`, respectively.\n * - If set to `false`, `name` and `version` will not be included in outgoing requests.\n *\n * @defaultValue \"headers\"\n */\n transport?: \"headers\" | false;\n }\n export interface EnhancedClientAwarenessOptions {\n /**\n * Determines how the the version information of Apollo Client is sent in outgoing requests.\n *\n * - If set to `\"extensions\"`, library `name` and `version` will be sent in an object in the request extensions as `clientLibrary`.\n * - If set to `false`, library name and version will not be included in outgoing requests.\n *\n * @defaultValue \"extensions\"\n */\n transport?: \"extensions\" | false;\n }\n\n export interface Options {\n /**\n * Configures the \"client awareness\" feature.\n * This feature allows you to identify distinct applications in Apollo Studio\n * and Apollo Server logs (and other monitoring or analytics tools) by adding\n * information about the your application to outgoing requests.\n */\n clientAwareness?: ClientAwarenessLink.ClientAwarenessOptions;\n /**\n * Configures the \"enhanced client awareness\" feature.\n * This feature allows you to identify the version of the Apollo Client library\n * used in your application in Apollo Studio (and other monitoring or analytics tools)\n * by adding information about the Apollo Client library to outgoing requests.\n */\n enhancedClientAwareness?: ClientAwarenessLink.EnhancedClientAwarenessOptions;\n }\n}\n\n/**\n * `ClientAwarenessLink` provides support for providing client awareness\n * features.\n *\n * @remarks\n *\n * Client awareness adds identifying information about the client to HTTP\n * requests for use with metrics reporting tools, such as [Apollo GraphOS](https://apollographql.com/docs/graphos/platform).\n * It is included in the functionality of [`HttpLink`](https://apollographql.com/docs/react/api/link/apollo-link-http) by default.\n *\n * Client awareness distinguishes between user-provided client awareness\n * (provided by the `clientAwareness` option) and enhanced client awareness\n * (provided by the `enhancedClientAwareness` option). User-provided client\n * awareness enables you to set a customized client name and version for\n * identification in metrics reporting tools. Enhanced client awareness enables\n * the identification of the Apollo Client package name and version.\n *\n * @example\n *\n * ```ts\n * import { ClientAwarenessLink } from \"@apollo/client/link/client-awareness\";\n *\n * const link = new ClientAwarenessLink({\n * clientAwareness: {\n * name: \"My Client\",\n * version: \"1\",\n * },\n * enhancedClientAwareness: {\n * transport: \"extensions\",\n * },\n * });\n * ```\n */\nexport class ClientAwarenessLink extends ApolloLink {\n constructor(constructorOptions: ClientAwarenessLink.Options = {}) {\n super((operation, forward) => {\n const client = operation.client;\n\n const clientOptions = client[\"queryManager\"].clientOptions;\n const context = operation.getContext();\n {\n const {\n name,\n version,\n transport = \"headers\",\n } = compact(\n {},\n clientOptions.clientAwareness,\n constructorOptions.clientAwareness,\n context.clientAwareness\n );\n\n if (transport === \"headers\") {\n operation.setContext(({ headers, extensions }) => {\n return {\n headers: compact(\n // setting these first so that they can be overridden by user-provided headers\n {\n \"apollographql-client-name\": name,\n \"apollographql-client-version\": version,\n },\n headers\n ),\n };\n });\n }\n }\n {\n const { transport = \"extensions\" } = compact(\n {},\n clientOptions.enhancedClientAwareness,\n constructorOptions.enhancedClientAwareness\n );\n if (transport === \"extensions\") {\n operation.extensions = compact(\n // setting these first so that it can be overridden by user-provided extensions\n {\n clientLibrary: {\n name: \"@apollo/client\",\n version: client.version,\n },\n },\n operation.extensions\n );\n }\n }\n\n return forward(operation);\n });\n }\n}\n"]}
|