@apollo/client 4.0.0-rc.8 → 4.0.0-rc.9
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 +12 -0
- package/__cjs/cache/inmemory/writeToStore.cjs +1 -1
- package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
- package/__cjs/core/ObservableQuery.cjs +1 -1
- package/__cjs/core/ObservableQuery.cjs.map +1 -1
- package/__cjs/core/QueryInfo.cjs +2 -6
- package/__cjs/core/QueryInfo.cjs.map +1 -1
- package/__cjs/link/batch/batchLink.cjs +30 -2
- package/__cjs/link/batch/batchLink.cjs.map +1 -1
- package/__cjs/link/batch/batchLink.d.cts +69 -2
- package/__cjs/link/batch-http/BaseBatchHttpLink.cjs +156 -0
- package/__cjs/link/batch-http/BaseBatchHttpLink.cjs.map +1 -0
- package/__cjs/link/batch-http/BaseBatchHttpLink.d.cts +68 -0
- package/__cjs/link/batch-http/batchHttpLink.cjs +3 -128
- package/__cjs/link/batch-http/batchHttpLink.cjs.map +1 -1
- package/__cjs/link/batch-http/batchHttpLink.d.cts +4 -20
- package/__cjs/link/batch-http/index.cjs +2 -1
- package/__cjs/link/batch-http/index.cjs.map +1 -1
- package/__cjs/link/batch-http/index.d.cts +2 -1
- package/__cjs/link/client-awareness/ClientAwarenessLink.cjs +3 -3
- package/__cjs/link/client-awareness/ClientAwarenessLink.cjs.map +1 -1
- package/__cjs/link/client-awareness/ClientAwarenessLink.d.cts +20 -1
- package/__cjs/link/context/index.cjs +19 -0
- package/__cjs/link/context/index.cjs.map +1 -1
- package/__cjs/link/context/index.d.cts +64 -2
- package/__cjs/link/http/BaseHttpLink.cjs +59 -8
- package/__cjs/link/http/BaseHttpLink.cjs.map +1 -1
- package/__cjs/link/http/BaseHttpLink.d.cts +256 -2
- package/__cjs/link/http/HttpLink.cjs +1 -1
- package/__cjs/link/http/HttpLink.cjs.map +1 -1
- package/__cjs/link/http/HttpLink.d.cts +5 -208
- package/__cjs/link/http/rewriteURIForGET.cjs.map +1 -1
- package/__cjs/link/http/rewriteURIForGET.d.cts +2 -2
- package/__cjs/link/http/selectHttpOptionsAndBody.cjs.map +1 -1
- package/__cjs/link/http/selectHttpOptionsAndBody.d.cts +7 -7
- package/__cjs/link/persisted-queries/index.cjs +16 -0
- package/__cjs/link/persisted-queries/index.cjs.map +1 -1
- package/__cjs/link/persisted-queries/index.d.cts +181 -0
- package/__cjs/link/remove-typename/removeTypenameFromVariables.cjs +48 -0
- package/__cjs/link/remove-typename/removeTypenameFromVariables.cjs.map +1 -1
- package/__cjs/link/remove-typename/removeTypenameFromVariables.d.cts +115 -0
- package/__cjs/link/retry/retryLink.cjs +25 -3
- package/__cjs/link/retry/retryLink.cjs.map +1 -1
- package/__cjs/link/retry/retryLink.d.cts +82 -9
- package/__cjs/link/schema/index.cjs +20 -0
- package/__cjs/link/schema/index.cjs.map +1 -1
- package/__cjs/link/schema/index.d.cts +128 -6
- package/__cjs/link/ws/index.cjs +18 -0
- package/__cjs/link/ws/index.cjs.map +1 -1
- package/__cjs/link/ws/index.d.cts +56 -5
- package/__cjs/react/hooks/useSyncExternalStore.cjs +2 -6
- package/__cjs/react/hooks/useSyncExternalStore.cjs.map +1 -1
- package/__cjs/utilities/internal/removeDirectivesFromDocument.cjs +2 -6
- package/__cjs/utilities/internal/removeDirectivesFromDocument.cjs.map +1 -1
- package/__cjs/utilities/subscriptions/relay/index.cjs.map +1 -1
- package/__cjs/version.cjs +1 -1
- package/cache/inmemory/writeToStore.js +1 -1
- package/cache/inmemory/writeToStore.js.map +1 -1
- package/core/ObservableQuery.js +1 -1
- package/core/ObservableQuery.js.map +1 -1
- package/core/QueryInfo.js +1 -2
- package/core/QueryInfo.js.map +1 -1
- package/link/batch/batchLink.d.ts +69 -2
- package/link/batch/batchLink.js +30 -2
- package/link/batch/batchLink.js.map +1 -1
- package/link/batch-http/BaseBatchHttpLink.d.ts +68 -0
- package/link/batch-http/BaseBatchHttpLink.js +152 -0
- package/link/batch-http/BaseBatchHttpLink.js.map +1 -0
- package/link/batch-http/batchHttpLink.d.ts +5 -21
- package/link/batch-http/batchHttpLink.js +1 -125
- package/link/batch-http/batchHttpLink.js.map +1 -1
- package/link/batch-http/index.d.ts +2 -1
- package/link/batch-http/index.js +2 -1
- package/link/batch-http/index.js.map +1 -1
- package/link/client-awareness/ClientAwarenessLink.d.ts +21 -2
- package/link/client-awareness/ClientAwarenessLink.js +3 -3
- package/link/client-awareness/ClientAwarenessLink.js.map +1 -1
- package/link/context/index.d.ts +65 -3
- package/link/context/index.js +19 -0
- package/link/context/index.js.map +1 -1
- package/link/http/BaseHttpLink.d.ts +257 -3
- package/link/http/BaseHttpLink.js +59 -8
- package/link/http/BaseHttpLink.js.map +1 -1
- package/link/http/HttpLink.d.ts +6 -209
- package/link/http/HttpLink.js +1 -1
- package/link/http/HttpLink.js.map +1 -1
- package/link/http/rewriteURIForGET.d.ts +2 -2
- package/link/http/rewriteURIForGET.js.map +1 -1
- package/link/http/selectHttpOptionsAndBody.d.ts +7 -7
- package/link/http/selectHttpOptionsAndBody.js.map +1 -1
- package/link/persisted-queries/index.d.ts +182 -1
- package/link/persisted-queries/index.js +16 -0
- package/link/persisted-queries/index.js.map +1 -1
- package/link/remove-typename/removeTypenameFromVariables.d.ts +115 -0
- package/link/remove-typename/removeTypenameFromVariables.js +48 -0
- package/link/remove-typename/removeTypenameFromVariables.js.map +1 -1
- package/link/retry/retryLink.d.ts +83 -10
- package/link/retry/retryLink.js +25 -3
- package/link/retry/retryLink.js.map +1 -1
- package/link/schema/index.d.ts +129 -7
- package/link/schema/index.js +20 -0
- package/link/schema/index.js.map +1 -1
- package/link/ws/index.d.ts +56 -5
- package/link/ws/index.js +18 -0
- package/link/ws/index.js.map +1 -1
- package/package.json +1 -1
- package/react/hooks/useSyncExternalStore.js +1 -1
- package/react/hooks/useSyncExternalStore.js.map +1 -1
- package/react/hooks-compiled/useSyncExternalStore.js +1 -1
- package/react/hooks-compiled/useSyncExternalStore.js.map +1 -1
- package/utilities/internal/removeDirectivesFromDocument.js +1 -2
- package/utilities/internal/removeDirectivesFromDocument.js.map +1 -1
- package/utilities/subscriptions/relay/index.js.map +1 -1
- package/version.js +1 -1
package/link/context/index.d.ts
CHANGED
|
@@ -1,7 +1,50 @@
|
|
|
1
1
|
import { ApolloLink } from "@apollo/client/link";
|
|
2
2
|
export declare namespace SetContextLink {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
namespace SetContextLinkDocumentationTypes {
|
|
4
|
+
/**
|
|
5
|
+
* A function that returns an updated context object for an Apollo Link
|
|
6
|
+
* operation.
|
|
7
|
+
*
|
|
8
|
+
* The context setter function is called for each operation and allows you to
|
|
9
|
+
* modify the operation's context before it's passed to the next link in the
|
|
10
|
+
* chain. The returned context object is shallowly merged with the previous
|
|
11
|
+
* context object.
|
|
12
|
+
*
|
|
13
|
+
* @param prevContext - The previous context of the operation (e.g. the value
|
|
14
|
+
* of `operation.getContext()`)
|
|
15
|
+
* @param operation - The GraphQL operation being executed, without the
|
|
16
|
+
* `getContext` and `setContext` methods
|
|
17
|
+
* @returns A partial context object or a promise that resolves to a partial context object
|
|
18
|
+
*/
|
|
19
|
+
function ContextSetter(prevContext: Readonly<ApolloLink.OperationContext>, operation: SetContextLink.SetContextOperation): Promise<Partial<ApolloLink.OperationContext>> | Partial<ApolloLink.OperationContext>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A function that returns an updated context object for an Apollo Link
|
|
23
|
+
* operation.
|
|
24
|
+
*
|
|
25
|
+
* The context setter function is called for each operation and allows you to
|
|
26
|
+
* modify the operation's context before it's passed to the next link in the
|
|
27
|
+
* chain. The returned context object is shallowly merged with the previous
|
|
28
|
+
* context object.
|
|
29
|
+
*
|
|
30
|
+
* @param prevContext - The previous context of the operation (e.g. the value
|
|
31
|
+
* of `operation.getContext()`)
|
|
32
|
+
* @param operation - The GraphQL operation being executed, without the
|
|
33
|
+
* `getContext` and `setContext` methods
|
|
34
|
+
* @returns A partial context object or a promise that resolves to a partial context object
|
|
35
|
+
*/
|
|
36
|
+
type ContextSetter = (prevContext: Readonly<ApolloLink.OperationContext>, operation: SetContextLink.SetContextOperation) => Promise<Partial<ApolloLink.OperationContext>> | Partial<ApolloLink.OperationContext>;
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated
|
|
39
|
+
* Use `ContextSetter` instead. This type is used by the deprecated
|
|
40
|
+
* `setContext` function.
|
|
41
|
+
*/
|
|
42
|
+
type LegacyContextSetter = (operation: SetContextLink.SetContextOperation, prevContext: Readonly<ApolloLink.OperationContext>) => Promise<Partial<ApolloLink.OperationContext>> | Partial<ApolloLink.OperationContext>;
|
|
43
|
+
/**
|
|
44
|
+
* An `ApolloLink.Operation` object without the `getContext` and `setContext`
|
|
45
|
+
* methods. This prevents context setters from directly manipulating the
|
|
46
|
+
* context during the setter function execution.
|
|
47
|
+
*/
|
|
5
48
|
type SetContextOperation = Omit<ApolloLink.Operation, "getContext" | "setContext">;
|
|
6
49
|
}
|
|
7
50
|
/**
|
|
@@ -17,7 +60,26 @@ export declare namespace SetContextLink {
|
|
|
17
60
|
* ```
|
|
18
61
|
*/
|
|
19
62
|
export declare function setContext(setter: SetContextLink.LegacyContextSetter): SetContextLink;
|
|
63
|
+
/**
|
|
64
|
+
* `SetContextLink` is a non-terminating link that allows you to modify the
|
|
65
|
+
* context of GraphQL operations before they're passed to the next link in the
|
|
66
|
+
* chain. This is commonly used for authentication, adding headers, and other
|
|
67
|
+
* request-time configuration.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { SetContextLink } from "@apollo/client/link/context";
|
|
73
|
+
*
|
|
74
|
+
* const link = new SetContextLink((prevContext, operation) => {
|
|
75
|
+
* return {
|
|
76
|
+
* credentials: "include",
|
|
77
|
+
* // ...
|
|
78
|
+
* };
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
20
82
|
export declare class SetContextLink extends ApolloLink {
|
|
21
83
|
constructor(setter: SetContextLink.ContextSetter);
|
|
22
84
|
}
|
|
23
|
-
//# sourceMappingURL=index.d.ts.map
|
|
85
|
+
//# sourceMappingURL=index.d.ts.map
|
package/link/context/index.js
CHANGED
|
@@ -15,6 +15,25 @@ import { ApolloLink } from "@apollo/client/link";
|
|
|
15
15
|
export function setContext(setter) {
|
|
16
16
|
return new SetContextLink((prevContext, operation) => setter(operation, prevContext));
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* `SetContextLink` is a non-terminating link that allows you to modify the
|
|
20
|
+
* context of GraphQL operations before they're passed to the next link in the
|
|
21
|
+
* chain. This is commonly used for authentication, adding headers, and other
|
|
22
|
+
* request-time configuration.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { SetContextLink } from "@apollo/client/link/context";
|
|
28
|
+
*
|
|
29
|
+
* const link = new SetContextLink((prevContext, operation) => {
|
|
30
|
+
* return {
|
|
31
|
+
* credentials: "include",
|
|
32
|
+
* // ...
|
|
33
|
+
* };
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
18
37
|
export class SetContextLink extends ApolloLink {
|
|
19
38
|
constructor(setter) {
|
|
20
39
|
super((operation, forward) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/link/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/link/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AA0DjD;;;;;;;;;;;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} from \"@apollo/client/link\";\n\nexport declare namespace SetContextLink {\n namespace SetContextLinkDocumentationTypes {\n /**\n * A function that returns an updated context object for an Apollo Link\n * operation.\n *\n * The context setter function is called for each operation and allows you to\n * modify the operation's context before it's passed to the next link in the\n * chain. The returned context object is shallowly merged with the previous\n * context object.\n *\n * @param prevContext - The previous context of the operation (e.g. the value\n * of `operation.getContext()`)\n * @param operation - The GraphQL operation being executed, without the\n * `getContext` and `setContext` methods\n * @returns A partial context object or a promise that resolves to a partial context object\n */\n export function ContextSetter(\n prevContext: Readonly<ApolloLink.OperationContext>,\n operation: SetContextLink.SetContextOperation\n ):\n | Promise<Partial<ApolloLink.OperationContext>>\n | Partial<ApolloLink.OperationContext>;\n }\n\n /** {@inheritDoc @apollo/client/link/context!SetContextLink.SetContextLinkDocumentationTypes.ContextSetter:function(1)} */\n export type ContextSetter = (\n prevContext: Readonly<ApolloLink.OperationContext>,\n operation: SetContextLink.SetContextOperation\n ) =>\n | Promise<Partial<ApolloLink.OperationContext>>\n | Partial<ApolloLink.OperationContext>;\n\n /**\n * @deprecated\n * Use `ContextSetter` instead. This type is used by the deprecated\n * `setContext` function.\n */\n export type LegacyContextSetter = (\n operation: SetContextLink.SetContextOperation,\n prevContext: Readonly<ApolloLink.OperationContext>\n ) =>\n | Promise<Partial<ApolloLink.OperationContext>>\n | Partial<ApolloLink.OperationContext>;\n\n /**\n * An `ApolloLink.Operation` object without the `getContext` and `setContext`\n * methods. This prevents context setters from directly manipulating the\n * context during the setter function execution.\n */\n export type SetContextOperation = Omit<\n ApolloLink.Operation,\n \"getContext\" | \"setContext\"\n >;\n}\n\n/**\n * @deprecated\n * Use `SetContextLink` from `@apollo/client/link/context` instead. Note you\n * will need to flip the arguments when using `SetContextLink` as `prevContext`\n * is the first argument.\n *\n * ```ts\n * new SetContextLink((prevContext, operation) => {\n * // ...\n * });\n * ```\n */\nexport function setContext(setter: SetContextLink.LegacyContextSetter) {\n return new SetContextLink((prevContext, operation) =>\n setter(operation, prevContext)\n );\n}\n/**\n * `SetContextLink` is a non-terminating link that allows you to modify the\n * context of GraphQL operations before they're passed to the next link in the\n * chain. This is commonly used for authentication, adding headers, and other\n * request-time configuration.\n *\n * @example\n *\n * ```ts\n * import { SetContextLink } from \"@apollo/client/link/context\";\n *\n * const link = new SetContextLink((prevContext, operation) => {\n * return {\n * credentials: \"include\",\n * // ...\n * };\n * });\n * ```\n */\nexport class SetContextLink extends ApolloLink {\n constructor(setter: SetContextLink.ContextSetter) {\n super((operation, forward) => {\n const { ...request } = operation as SetContextLink.SetContextOperation;\n\n Object.defineProperty(request, \"client\", {\n enumerable: false,\n value: operation.client,\n });\n\n return new Observable((observer) => {\n let closed = false;\n Promise.resolve(request)\n .then((req) => setter(operation.getContext(), req))\n .then(operation.setContext)\n .then(() => {\n if (!closed) {\n forward(operation).subscribe(observer);\n }\n })\n .catch(observer.error.bind(observer));\n\n return () => {\n closed = true;\n };\n });\n });\n }\n}\n"]}
|
|
@@ -1,6 +1,260 @@
|
|
|
1
|
+
import type { ASTNode, print } from "graphql";
|
|
1
2
|
import { ApolloLink } from "@apollo/client/link";
|
|
2
|
-
|
|
3
|
+
export declare namespace BaseHttpLink {
|
|
4
|
+
/**
|
|
5
|
+
* Options passed to `BaseHttpLink` through [request context](https://apollographql.com/docs/react/api/link/introduction#managing-context). Previous
|
|
6
|
+
* non-terminating links in the link chain also can set these values to
|
|
7
|
+
* customize the behavior of `BaseHttpLink` for each operation.
|
|
8
|
+
*
|
|
9
|
+
* > [!NOTE]
|
|
10
|
+
* > Some of these values can also be provided to the `HttpLink` constructor.
|
|
11
|
+
* > If a value is provided to both, the value in `context` takes precedence.
|
|
12
|
+
*/
|
|
13
|
+
interface ContextOptions {
|
|
14
|
+
/**
|
|
15
|
+
* The URL of the GraphQL endpoint to send requests to. Can also be a
|
|
16
|
+
* function that accepts an `ApolloLink.Operation` object and returns the
|
|
17
|
+
* string URL to use for that operation.
|
|
18
|
+
*
|
|
19
|
+
* @defaultValue "/graphql"
|
|
20
|
+
*/
|
|
21
|
+
uri?: string | BaseHttpLink.UriFunction;
|
|
22
|
+
/**
|
|
23
|
+
* An object representing headers to include in every HTTP request.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```json
|
|
28
|
+
* {
|
|
29
|
+
* "Authorization": "Bearer 1234"
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
/**
|
|
35
|
+
* The credentials policy to use for each `fetch` call.
|
|
36
|
+
*/
|
|
37
|
+
credentials?: RequestCredentials;
|
|
38
|
+
/**
|
|
39
|
+
* Any overrides of the fetch options argument to pass to the fetch call.
|
|
40
|
+
*
|
|
41
|
+
* An object containing options to use for each call to `fetch`. If a
|
|
42
|
+
* particular option is not included in this object, the default value of
|
|
43
|
+
* that option is used.
|
|
44
|
+
*
|
|
45
|
+
* > [!NOTE]
|
|
46
|
+
* > If you set `fetchOptions.method` to `GET`, `HttpLink` follows [standard
|
|
47
|
+
* > GraphQL HTTP GET encoding](http://graphql.org/learn/serving-over-http/#get-request).
|
|
48
|
+
*
|
|
49
|
+
* See [available options](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
|
|
50
|
+
*/
|
|
51
|
+
fetchOptions?: RequestInit;
|
|
52
|
+
/**
|
|
53
|
+
* An object that configures advanced functionality, such as support for
|
|
54
|
+
* persisted queries.
|
|
55
|
+
*/
|
|
56
|
+
http?: BaseHttpLink.HttpOptions;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Options passed to `BaseHttpLink` through the `http` property of a request
|
|
60
|
+
* context.
|
|
61
|
+
*/
|
|
62
|
+
interface HttpOptions {
|
|
63
|
+
/**
|
|
64
|
+
* If `true`, includes the `extensions` field in operations sent to your
|
|
65
|
+
* GraphQL endpoint.
|
|
66
|
+
*
|
|
67
|
+
* @defaultValue true
|
|
68
|
+
*/
|
|
69
|
+
includeExtensions?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* If `false`, the GraphQL query string is not included in the request. Set
|
|
72
|
+
* this option if you're sending a request that uses a [persisted query](https://www.apollographql.com/docs/react/api/link/persisted-queries/).
|
|
73
|
+
*
|
|
74
|
+
* @defaultValue `true`
|
|
75
|
+
*/
|
|
76
|
+
includeQuery?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* If `true`, header names won't be automatically normalized to lowercase.
|
|
79
|
+
* This allows for non-http-spec-compliant servers that might expect
|
|
80
|
+
* capitalized header names.
|
|
81
|
+
*
|
|
82
|
+
* @defaultValue false
|
|
83
|
+
*/
|
|
84
|
+
preserveHeaderCase?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* A list of additional `accept` headers to include in the request,
|
|
87
|
+
* as defined in https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* ```json
|
|
92
|
+
* ["application/custom+json;q=1.0"]
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
accept?: string[];
|
|
96
|
+
}
|
|
97
|
+
namespace Shared {
|
|
98
|
+
/** These options are shared between `BaseHttpLink` and `BaseBatchHttpLink` */
|
|
99
|
+
interface Options {
|
|
100
|
+
/**
|
|
101
|
+
* The URL of the GraphQL endpoint to send requests to. Can also be a
|
|
102
|
+
* function that accepts an `ApolloLink.Operation` object and returns the
|
|
103
|
+
* string URL to use for that operation.
|
|
104
|
+
*
|
|
105
|
+
* @defaultValue "/graphql"
|
|
106
|
+
*/
|
|
107
|
+
uri?: string | BaseHttpLink.UriFunction;
|
|
108
|
+
/**
|
|
109
|
+
* If `true`, includes the `extensions` field in operations sent to your
|
|
110
|
+
* GraphQL endpoint.
|
|
111
|
+
*
|
|
112
|
+
* @defaultValue true
|
|
113
|
+
*/
|
|
114
|
+
includeExtensions?: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* A function to use instead of calling the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) directly
|
|
117
|
+
* when sending HTTP requests to your GraphQL endpoint. The function must
|
|
118
|
+
* conform to the signature of `fetch`.
|
|
119
|
+
*
|
|
120
|
+
* By default, the Fetch API is used unless it isn't available in your
|
|
121
|
+
* runtime environment.
|
|
122
|
+
*
|
|
123
|
+
* See [Customizing `fetch`](https://apollographql.com/docs/react/api/link/introduction#customizing-fetch).
|
|
124
|
+
*/
|
|
125
|
+
fetch?: typeof fetch;
|
|
126
|
+
/**
|
|
127
|
+
* An object representing headers to include in every HTTP request.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
*
|
|
131
|
+
* ```json
|
|
132
|
+
* {
|
|
133
|
+
* "Authorization": "Bearer 1234"
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
headers?: Record<string, string>;
|
|
138
|
+
/**
|
|
139
|
+
* If `true`, header names won't be automatically normalized to lowercase.
|
|
140
|
+
* This allows for non-http-spec-compliant servers that might expect
|
|
141
|
+
* capitalized header names.
|
|
142
|
+
*
|
|
143
|
+
* @defaultValue false
|
|
144
|
+
*/
|
|
145
|
+
preserveHeaderCase?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* The credentials policy to use for each `fetch` call.
|
|
148
|
+
*/
|
|
149
|
+
credentials?: RequestCredentials;
|
|
150
|
+
/**
|
|
151
|
+
* Any overrides of the fetch options argument to pass to the fetch call.
|
|
152
|
+
*
|
|
153
|
+
* An object containing options to use for each call to `fetch`. If a
|
|
154
|
+
* particular option is not included in this object, the default value of
|
|
155
|
+
* that option is used.
|
|
156
|
+
*
|
|
157
|
+
* > [!NOTE]
|
|
158
|
+
* > If you set `fetchOptions.method` to `GET`, `HttpLink` follows [standard
|
|
159
|
+
* > GraphQL HTTP GET encoding](http://graphql.org/learn/serving-over-http/#get-request).
|
|
160
|
+
*
|
|
161
|
+
* See [available options](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
|
|
162
|
+
*/
|
|
163
|
+
fetchOptions?: RequestInit;
|
|
164
|
+
/**
|
|
165
|
+
* If `true`, unused variables from the operation will not be stripped from
|
|
166
|
+
* the request and will instead be sent to the GraphQL endpoint.
|
|
167
|
+
*
|
|
168
|
+
* @remarks
|
|
169
|
+
* Unused variables are likely to trigger server-side validation errors,
|
|
170
|
+
* per https://spec.graphql.org/draft/#sec-All-Variables-Used.
|
|
171
|
+
* `includeUnusedVariables` can be useful if your server deviates
|
|
172
|
+
* from the GraphQL specification by not strictly enforcing that rule.
|
|
173
|
+
*
|
|
174
|
+
* @defaultValue false
|
|
175
|
+
*/
|
|
176
|
+
includeUnusedVariables?: boolean;
|
|
177
|
+
/**
|
|
178
|
+
* A function to use when transforming a GraphQL document into a string. It
|
|
179
|
+
* accepts an `ASTNode` (typically a `DocumentNode`) and the original `print`
|
|
180
|
+
* function as arguments, and is expected to return a string. This option
|
|
181
|
+
* enables you to, for example, use `stripIgnoredCharacters` to remove
|
|
182
|
+
* whitespace from queries.
|
|
183
|
+
*
|
|
184
|
+
* By default the [GraphQL `print` function](https://graphql.org/graphql-js/language/#print) is used.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
*
|
|
188
|
+
* ```ts
|
|
189
|
+
* import { stripIgnoredCharacters } from "graphql";
|
|
190
|
+
*
|
|
191
|
+
* const httpLink = new HttpLink({
|
|
192
|
+
* uri: "/graphql",
|
|
193
|
+
* print: (ast, originalPrint) => stripIgnoredCharacters(originalPrint(ast)),
|
|
194
|
+
* });
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
print?: BaseHttpLink.Printer;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Options provided to the `BaseHttpLink` constructor.
|
|
202
|
+
*
|
|
203
|
+
* > [!NOTE]
|
|
204
|
+
* > Some of these options are also available to override in [request context](https://apollographql.com/docs/react/api/link/introduction#managing-context).
|
|
205
|
+
* > Context options override the options passed to the constructor. Treat
|
|
206
|
+
* > these options as default values that are used when the request context
|
|
207
|
+
* > does not override the value.
|
|
208
|
+
*/
|
|
209
|
+
interface Options extends Shared.Options {
|
|
210
|
+
/**
|
|
211
|
+
* If `true`, the link uses an HTTP `GET` request when sending query
|
|
212
|
+
* operations to your GraphQL endpoint. Mutation operations continue to use
|
|
213
|
+
* `POST` requests. If you want all operations to use `GET` requests,
|
|
214
|
+
* set `fetchOptions.method` instead.
|
|
215
|
+
*
|
|
216
|
+
* @defaultValue false
|
|
217
|
+
*/
|
|
218
|
+
useGETForQueries?: boolean;
|
|
219
|
+
}
|
|
220
|
+
interface Body {
|
|
221
|
+
query?: string;
|
|
222
|
+
operationName?: string;
|
|
223
|
+
variables?: Record<string, any>;
|
|
224
|
+
extensions?: Record<string, any>;
|
|
225
|
+
}
|
|
226
|
+
type Printer = (node: ASTNode, originalPrint: typeof print) => string;
|
|
227
|
+
type UriFunction = (operation: ApolloLink.Operation) => string;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* `BaseHttpLink` is a terminating link that sends a GraphQL operation to a
|
|
231
|
+
* remote endpoint over HTTP. It serves as a base link to `HttpLink`.
|
|
232
|
+
*
|
|
233
|
+
* @remarks
|
|
234
|
+
*
|
|
235
|
+
* `BaseHttpLink` supports both POST and GET requests, and you can configure
|
|
236
|
+
* HTTP options on a per-operation basis. You can use these options for
|
|
237
|
+
* authentication, persisted queries, dynamic URIs, and other granular updates.
|
|
238
|
+
*
|
|
239
|
+
* > [!NOTE]
|
|
240
|
+
* > Prefer using `HttpLink` over `BaseHttpLink`. Use `BaseHttpLink` when you
|
|
241
|
+
* > need to disable client awareness features and would like to tree-shake
|
|
242
|
+
* > the implementation of `ClientAwarenessLink` out of your app bundle.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
*
|
|
246
|
+
* ```ts
|
|
247
|
+
* import { BaseHttpLink } from "@apollo/client/link/http";
|
|
248
|
+
*
|
|
249
|
+
* const link = new BaseHttpLink({
|
|
250
|
+
* uri: "http://localhost:4000/graphql",
|
|
251
|
+
* headers: {
|
|
252
|
+
* authorization: `Bearer ${token}`,
|
|
253
|
+
* },
|
|
254
|
+
* });
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
3
257
|
export declare class BaseHttpLink extends ApolloLink {
|
|
4
|
-
constructor(
|
|
258
|
+
constructor(options?: BaseHttpLink.Options);
|
|
5
259
|
}
|
|
6
|
-
//# sourceMappingURL=BaseHttpLink.d.ts.map
|
|
260
|
+
//# sourceMappingURL=BaseHttpLink.d.ts.map
|
|
@@ -11,11 +11,40 @@ import { rewriteURIForGET } from "./rewriteURIForGET.js";
|
|
|
11
11
|
import { defaultPrinter, fallbackHttpConfig, selectHttpOptionsAndBodyInternal, } from "./selectHttpOptionsAndBody.js";
|
|
12
12
|
import { selectURI } from "./selectURI.js";
|
|
13
13
|
const backupFetch = maybe(() => fetch);
|
|
14
|
+
function noop() { }
|
|
15
|
+
/**
|
|
16
|
+
* `BaseHttpLink` is a terminating link that sends a GraphQL operation to a
|
|
17
|
+
* remote endpoint over HTTP. It serves as a base link to `HttpLink`.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
*
|
|
21
|
+
* `BaseHttpLink` supports both POST and GET requests, and you can configure
|
|
22
|
+
* HTTP options on a per-operation basis. You can use these options for
|
|
23
|
+
* authentication, persisted queries, dynamic URIs, and other granular updates.
|
|
24
|
+
*
|
|
25
|
+
* > [!NOTE]
|
|
26
|
+
* > Prefer using `HttpLink` over `BaseHttpLink`. Use `BaseHttpLink` when you
|
|
27
|
+
* > need to disable client awareness features and would like to tree-shake
|
|
28
|
+
* > the implementation of `ClientAwarenessLink` out of your app bundle.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { BaseHttpLink } from "@apollo/client/link/http";
|
|
34
|
+
*
|
|
35
|
+
* const link = new BaseHttpLink({
|
|
36
|
+
* uri: "http://localhost:4000/graphql",
|
|
37
|
+
* headers: {
|
|
38
|
+
* authorization: `Bearer ${token}`,
|
|
39
|
+
* },
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
14
43
|
export class BaseHttpLink extends ApolloLink {
|
|
15
|
-
constructor(
|
|
44
|
+
constructor(options = {}) {
|
|
16
45
|
let { uri = "/graphql",
|
|
17
46
|
// use default global fetch if nothing passed in
|
|
18
|
-
fetch: preferredFetch, print = defaultPrinter, includeExtensions, preserveHeaderCase, useGETForQueries, includeUnusedVariables = false, ...requestOptions } =
|
|
47
|
+
fetch: preferredFetch, print = defaultPrinter, includeExtensions, preserveHeaderCase, useGETForQueries, includeUnusedVariables = false, ...requestOptions } = options;
|
|
19
48
|
if (__DEV__) {
|
|
20
49
|
// Make sure at least one of preferredFetch, window.fetch, or backupFetch is
|
|
21
50
|
// defined, so requests won't fail at runtime.
|
|
@@ -48,11 +77,33 @@ export class BaseHttpLink extends ApolloLink {
|
|
|
48
77
|
if (body.variables && !includeUnusedVariables) {
|
|
49
78
|
body.variables = filterOperationVariables(body.variables, operation.query);
|
|
50
79
|
}
|
|
51
|
-
let controller;
|
|
52
|
-
|
|
53
|
-
controller =
|
|
54
|
-
|
|
80
|
+
let controller = new AbortController();
|
|
81
|
+
let cleanupController = () => {
|
|
82
|
+
controller = undefined;
|
|
83
|
+
};
|
|
84
|
+
if (options.signal) {
|
|
85
|
+
const externalSignal = options.signal;
|
|
86
|
+
// in an ideal world we could use `AbortSignal.any` here, but
|
|
87
|
+
// React Native uses https://github.com/mysticatea/abort-controller as
|
|
88
|
+
// a polyfill for `AbortController`, and it does not support `AbortSignal.any`.
|
|
89
|
+
const listener = () => {
|
|
90
|
+
controller?.abort(externalSignal.reason);
|
|
91
|
+
};
|
|
92
|
+
externalSignal.addEventListener("abort", listener, { once: true });
|
|
93
|
+
cleanupController = () => {
|
|
94
|
+
controller?.signal.removeEventListener("abort", cleanupController);
|
|
95
|
+
controller = undefined;
|
|
96
|
+
// on cleanup, we need to stop listening to `options.signal` to avoid memory leaks
|
|
97
|
+
externalSignal.removeEventListener("abort", listener);
|
|
98
|
+
cleanupController = noop;
|
|
99
|
+
};
|
|
100
|
+
// react native also does not support the addEventListener `signal` option
|
|
101
|
+
// so we have to simulate that ourself
|
|
102
|
+
controller.signal.addEventListener("abort", cleanupController, {
|
|
103
|
+
once: true,
|
|
104
|
+
});
|
|
55
105
|
}
|
|
106
|
+
options.signal = controller.signal;
|
|
56
107
|
if (useGETForQueries && !isMutationOperation(operation.query)) {
|
|
57
108
|
options.method = "GET";
|
|
58
109
|
}
|
|
@@ -86,11 +137,11 @@ export class BaseHttpLink extends ApolloLink {
|
|
|
86
137
|
}
|
|
87
138
|
})
|
|
88
139
|
.then(() => {
|
|
89
|
-
|
|
140
|
+
cleanupController();
|
|
90
141
|
observer.complete();
|
|
91
142
|
})
|
|
92
143
|
.catch((err) => {
|
|
93
|
-
|
|
144
|
+
cleanupController();
|
|
94
145
|
observer.error(err);
|
|
95
146
|
});
|
|
96
147
|
return () => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseHttpLink.js","sourceRoot":"","sources":["../../../src/link/http/BaseHttpLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EACL,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,sCAAsC,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,2CAA2C,CAAC;AAElE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;A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;oBACrC,oEAAoE;oBACpE,IAAI,UAAU;wBAAE,UAAU,CAAC,KAAK,EAAE,CAAC;gBACrC,CAAC,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import { Observable } from \"rxjs\";\n\nimport { ApolloLink } from \"@apollo/client/link\";\nimport { filterOperationVariables } from \"@apollo/client/link/utils\";\nimport {\n isMutationOperation,\n isSubscriptionOperation,\n} from \"@apollo/client/utilities\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport { compact } from \"@apollo/client/utilities/internal\";\nimport { maybe } from \"@apollo/client/utilities/internal/globals\";\n\nimport { checkFetcher } from \"./checkFetcher.js\";\nimport type { HttpLink } from \"./HttpLink.js\";\nimport {\n parseAndCheckHttpResponse,\n readMultipartBody,\n} from \"./parseAndCheckHttpResponse.js\";\nimport { rewriteURIForGET } from \"./rewriteURIForGET.js\";\nimport {\n defaultPrinter,\n fallbackHttpConfig,\n selectHttpOptionsAndBodyInternal,\n} from \"./selectHttpOptionsAndBody.js\";\nimport { selectURI } from \"./selectURI.js\";\n\nconst backupFetch = maybe(() => fetch);\n\nexport class BaseHttpLink extends ApolloLink {\n constructor(linkOptions: HttpLink.Options = {}) {\n let {\n uri = \"/graphql\",\n // use default global fetch if nothing passed in\n fetch: preferredFetch,\n print = defaultPrinter,\n includeExtensions,\n preserveHeaderCase,\n useGETForQueries,\n includeUnusedVariables = false,\n ...requestOptions\n } = linkOptions;\n\n if (__DEV__) {\n // Make sure at least one of preferredFetch, window.fetch, or backupFetch is\n // 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 super((operation) => {\n let chosenURI = selectURI(operation, uri);\n\n const context = operation.getContext();\n\n const http = { ...context.http };\n if (isSubscriptionOperation(operation.query)) {\n http.accept = [\n \"multipart/mixed;boundary=graphql;subscriptionSpec=1.0\",\n ...(http.accept || []),\n ];\n }\n\n const contextConfig = {\n 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 { options, body } = selectHttpOptionsAndBodyInternal(\n operation,\n print,\n fallbackHttpConfig,\n linkConfig,\n contextConfig\n );\n\n if (body.variables && !includeUnusedVariables) {\n body.variables = filterOperationVariables(\n body.variables,\n operation.query\n );\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 if (useGETForQueries && !isMutationOperation(operation.query)) {\n options.method = \"GET\";\n }\n\n return new Observable((observer) => {\n if (options.method === \"GET\") {\n const { newURI, parseError } = rewriteURIForGET(chosenURI, body);\n if (parseError) {\n throw parseError;\n }\n chosenURI = newURI;\n } else {\n options.body = JSON.stringify(body);\n }\n // Prefer linkOptions.fetch (preferredFetch) if provided, and otherwise\n // fall back to the *current* global window.fetch function (see issue\n // #7832), or (if all else fails) the backupFetch function we saved when\n // this module was first evaluated. This last option protects against the\n // removal of window.fetch, which is unlikely but not impossible.\n const currentFetch =\n preferredFetch || maybe(() => fetch) || backupFetch;\n\n const observerNext = observer.next.bind(observer);\n currentFetch!(chosenURI, options)\n .then((response) => {\n operation.setContext({ response });\n const ctype = response.headers?.get(\"content-type\");\n\n if (ctype !== null && /^multipart\\/mixed/i.test(ctype)) {\n return readMultipartBody(response, observerNext);\n } else {\n return parseAndCheckHttpResponse(operation)(response).then(\n observerNext\n );\n }\n })\n .then(() => {\n controller = undefined;\n observer.complete();\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}\n"]}
|
|
1
|
+
{"version":3,"file":"BaseHttpLink.js","sourceRoot":"","sources":["../../../src/link/http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ACtB,CAAC,CAAC,CAAC;gBAEL,OAAO,GAAG,EAAE;oBACV,qCAAqC;oBACrC,oEAAoE;oBACpE,IAAI,UAAU;wBAAE,UAAU,CAAC,KAAK,EAAE,CAAC;gBACrC,CAAC,CAAC;YACJ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type { ASTNode, print } from \"graphql\";\nimport { Observable } from \"rxjs\";\n\nimport { ApolloLink } from \"@apollo/client/link\";\nimport { filterOperationVariables } from \"@apollo/client/link/utils\";\nimport {\n isMutationOperation,\n isSubscriptionOperation,\n} from \"@apollo/client/utilities\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport { compact } from \"@apollo/client/utilities/internal\";\nimport { maybe } from \"@apollo/client/utilities/internal/globals\";\n\nimport { checkFetcher } from \"./checkFetcher.js\";\nimport {\n parseAndCheckHttpResponse,\n readMultipartBody,\n} from \"./parseAndCheckHttpResponse.js\";\nimport { rewriteURIForGET } from \"./rewriteURIForGET.js\";\nimport {\n defaultPrinter,\n fallbackHttpConfig,\n selectHttpOptionsAndBodyInternal,\n} from \"./selectHttpOptionsAndBody.js\";\nimport { selectURI } from \"./selectURI.js\";\n\nconst backupFetch = maybe(() => fetch);\nfunction noop() {}\n\nexport declare namespace BaseHttpLink {\n /**\n * Options passed to `BaseHttpLink` 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 `BaseHttpLink` for each operation.\n *\n * > [!NOTE]\n * > Some of these values can also be provided to the `HttpLink` constructor.\n * > If a value is provided to both, the value in `context` takes precedence.\n */\n interface ContextOptions {\n /** {@inheritDoc @apollo/client/link/http!BaseHttpLink.Shared.Options#uri:member} */\n uri?: string | BaseHttpLink.UriFunction;\n\n /** {@inheritDoc @apollo/client/link/http!BaseHttpLink.Shared.Options#headers:member} */\n headers?: Record<string, string>;\n\n /** {@inheritDoc @apollo/client/link/http!BaseHttpLink.Shared.Options#credentials:member} */\n credentials?: RequestCredentials;\n\n /** {@inheritDoc @apollo/client/link/http!BaseHttpLink.Shared.Options#fetchOptions:member} */\n fetchOptions?: RequestInit;\n\n /**\n * An object that configures advanced functionality, such as support for\n * persisted queries.\n */\n http?: BaseHttpLink.HttpOptions;\n }\n\n /**\n * Options passed to `BaseHttpLink` through the `http` property of a request\n * context.\n */\n export interface HttpOptions {\n /** {@inheritDoc @apollo/client/link/http!BaseHttpLink.Shared.Options#includeExtensions:member} */\n includeExtensions?: boolean;\n\n /**\n * If `false`, the GraphQL query string is not included in the request. Set\n * this option if you're sending a request that uses a [persisted query](https://www.apollographql.com/docs/react/api/link/persisted-queries/).\n *\n * @defaultValue `true`\n */\n includeQuery?: boolean;\n\n /** {@inheritDoc @apollo/client/link/http!BaseHttpLink.Shared.Options#preserveHeaderCase:member} */\n preserveHeaderCase?: boolean;\n\n /**\n * A list of additional `accept` headers to include in the request,\n * as defined in https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2\n *\n * @example\n *\n * ```json\n * [\"application/custom+json;q=1.0\"]\n * ```\n */\n accept?: string[];\n }\n\n export namespace Shared {\n /** These options are shared between `BaseHttpLink` and `BaseBatchHttpLink` */\n export interface Options {\n /**\n * The URL of the GraphQL endpoint to send requests to. Can also be a\n * function that accepts an `ApolloLink.Operation` object and returns the\n * string URL to use for that operation.\n *\n * @defaultValue \"/graphql\"\n */\n uri?: string | BaseHttpLink.UriFunction;\n\n /**\n * If `true`, includes the `extensions` field in operations sent to your\n * GraphQL endpoint.\n *\n * @defaultValue true\n */\n includeExtensions?: boolean;\n\n /**\n * A function to use instead of calling the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) directly\n * when sending HTTP requests to your GraphQL endpoint. The function must\n * conform to the signature of `fetch`.\n *\n * By default, the Fetch API is used unless it isn't available in your\n * runtime environment.\n *\n * See [Customizing `fetch`](https://apollographql.com/docs/react/api/link/introduction#customizing-fetch).\n */\n fetch?: typeof fetch;\n\n /**\n * An object representing headers to include in every HTTP request.\n *\n * @example\n *\n * ```json\n * {\n * \"Authorization\": \"Bearer 1234\"\n * }\n * ```\n */\n headers?: Record<string, string>;\n\n /**\n * If `true`, header names won't be automatically normalized to lowercase.\n * This allows for non-http-spec-compliant servers that might expect\n * capitalized header names.\n *\n * @defaultValue false\n */\n preserveHeaderCase?: boolean;\n\n /**\n * The credentials policy to use for each `fetch` call.\n */\n credentials?: RequestCredentials;\n\n /**\n * Any overrides of the fetch options argument to pass to the fetch call.\n *\n * An object containing options to use for each call to `fetch`. If a\n * particular option is not included in this object, the default value of\n * that option is used.\n *\n * > [!NOTE]\n * > If you set `fetchOptions.method` to `GET`, `HttpLink` follows [standard\n * > GraphQL HTTP GET encoding](http://graphql.org/learn/serving-over-http/#get-request).\n *\n * See [available options](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)\n */\n fetchOptions?: RequestInit;\n\n /**\n * If `true`, unused variables from the operation will not be stripped from\n * the request and will instead be sent to the GraphQL endpoint.\n *\n * @remarks\n * Unused variables are likely to trigger server-side validation errors,\n * per https://spec.graphql.org/draft/#sec-All-Variables-Used.\n * `includeUnusedVariables` can be useful if your server deviates\n * from the GraphQL specification by not strictly enforcing that rule.\n *\n * @defaultValue false\n */\n includeUnusedVariables?: boolean;\n /**\n * A function to use when transforming a GraphQL document into a string. It\n * accepts an `ASTNode` (typically a `DocumentNode`) and the original `print`\n * function as arguments, and is expected to return a string. This option\n * enables you to, for example, use `stripIgnoredCharacters` to remove\n * whitespace from queries.\n *\n * By default the [GraphQL `print` function](https://graphql.org/graphql-js/language/#print) is used.\n *\n * @example\n *\n * ```ts\n * import { stripIgnoredCharacters } from \"graphql\";\n *\n * const httpLink = new HttpLink({\n * uri: \"/graphql\",\n * print: (ast, originalPrint) => stripIgnoredCharacters(originalPrint(ast)),\n * });\n * ```\n */\n print?: BaseHttpLink.Printer;\n }\n }\n\n /**\n * Options provided to the `BaseHttpLink` constructor.\n *\n * > [!NOTE]\n * > Some of these options are also available to override in [request context](https://apollographql.com/docs/react/api/link/introduction#managing-context).\n * > Context options override the options passed to the constructor. Treat\n * > these options as default values that are used when the request context\n * > does not override the value.\n */\n interface Options extends Shared.Options {\n /**\n * If `true`, the link uses an HTTP `GET` request when sending query\n * operations to your GraphQL endpoint. Mutation operations continue to use\n * `POST` requests. If you want all operations to use `GET` requests,\n * set `fetchOptions.method` instead.\n *\n * @defaultValue false\n */\n useGETForQueries?: boolean;\n }\n\n interface Body {\n query?: string;\n operationName?: string;\n variables?: Record<string, any>;\n extensions?: Record<string, any>;\n }\n\n type Printer = (node: ASTNode, originalPrint: typeof print) => string;\n type UriFunction = (operation: ApolloLink.Operation) => string;\n}\n\n/**\n * `BaseHttpLink` is a terminating link that sends a GraphQL operation to a\n * remote endpoint over HTTP. It serves as a base link to `HttpLink`.\n *\n * @remarks\n *\n * `BaseHttpLink` supports both POST and GET requests, and you can configure\n * HTTP options on a per-operation basis. You can use these options for\n * authentication, persisted queries, dynamic URIs, and other granular updates.\n *\n * > [!NOTE]\n * > Prefer using `HttpLink` over `BaseHttpLink`. Use `BaseHttpLink` when you\n * > need to disable client awareness features and would like to tree-shake\n * > the implementation of `ClientAwarenessLink` out of your app bundle.\n *\n * @example\n *\n * ```ts\n * import { BaseHttpLink } from \"@apollo/client/link/http\";\n *\n * const link = new BaseHttpLink({\n * uri: \"http://localhost:4000/graphql\",\n * headers: {\n * authorization: `Bearer ${token}`,\n * },\n * });\n * ```\n */\nexport class BaseHttpLink extends ApolloLink {\n constructor(options: BaseHttpLink.Options = {}) {\n let {\n uri = \"/graphql\",\n // use default global fetch if nothing passed in\n fetch: preferredFetch,\n print = defaultPrinter,\n includeExtensions,\n preserveHeaderCase,\n useGETForQueries,\n includeUnusedVariables = false,\n ...requestOptions\n } = options;\n\n if (__DEV__) {\n // Make sure at least one of preferredFetch, window.fetch, or backupFetch is\n // 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 super((operation) => {\n let chosenURI = selectURI(operation, uri);\n\n const context = operation.getContext();\n\n const http = { ...context.http };\n if (isSubscriptionOperation(operation.query)) {\n http.accept = [\n \"multipart/mixed;boundary=graphql;subscriptionSpec=1.0\",\n ...(http.accept || []),\n ];\n }\n\n const contextConfig = {\n 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 { options, body } = selectHttpOptionsAndBodyInternal(\n operation,\n print,\n fallbackHttpConfig,\n linkConfig,\n contextConfig\n );\n\n if (body.variables && !includeUnusedVariables) {\n body.variables = filterOperationVariables(\n body.variables,\n operation.query\n );\n }\n\n let controller: AbortController | undefined = new AbortController();\n let cleanupController = () => {\n controller = undefined;\n };\n if (options.signal) {\n const externalSignal: AbortSignal = options.signal;\n // in an ideal world we could use `AbortSignal.any` here, but\n // React Native uses https://github.com/mysticatea/abort-controller as\n // a polyfill for `AbortController`, and it does not support `AbortSignal.any`.\n\n const listener = () => {\n controller?.abort(externalSignal.reason);\n };\n externalSignal.addEventListener(\"abort\", listener, { once: true });\n cleanupController = () => {\n controller?.signal.removeEventListener(\"abort\", cleanupController);\n controller = undefined;\n // on cleanup, we need to stop listening to `options.signal` to avoid memory leaks\n externalSignal.removeEventListener(\"abort\", listener);\n cleanupController = noop;\n };\n // react native also does not support the addEventListener `signal` option\n // so we have to simulate that ourself\n controller.signal.addEventListener(\"abort\", cleanupController, {\n once: true,\n });\n }\n options.signal = controller.signal;\n\n if (useGETForQueries && !isMutationOperation(operation.query)) {\n options.method = \"GET\";\n }\n\n return new Observable((observer) => {\n if (options.method === \"GET\") {\n const { newURI, parseError } = rewriteURIForGET(chosenURI, body);\n if (parseError) {\n throw parseError;\n }\n chosenURI = newURI;\n } else {\n options.body = JSON.stringify(body);\n }\n // Prefer linkOptions.fetch (preferredFetch) if provided, and otherwise\n // fall back to the *current* global window.fetch function (see issue\n // #7832), or (if all else fails) the backupFetch function we saved when\n // this module was first evaluated. This last option protects against the\n // removal of window.fetch, which is unlikely but not impossible.\n const currentFetch =\n preferredFetch || maybe(() => fetch) || backupFetch;\n\n const observerNext = observer.next.bind(observer);\n currentFetch!(chosenURI, options)\n .then((response) => {\n operation.setContext({ response });\n const ctype = response.headers?.get(\"content-type\");\n\n if (ctype !== null && /^multipart\\/mixed/i.test(ctype)) {\n return readMultipartBody(response, observerNext);\n } else {\n return parseAndCheckHttpResponse(operation)(response).then(\n observerNext\n );\n }\n })\n .then(() => {\n cleanupController();\n observer.complete();\n })\n .catch((err) => {\n cleanupController();\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}\n"]}
|