@hyper-fetch/graphql 9.9.0 → 10.1.0

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.
Files changed (57) hide show
  1. package/README.md +152 -84
  2. package/__tests__/e2e/e2e.conformance.spec.ts +206 -0
  3. package/__tests__/e2e/e2e.graphql-ws-server.ts +187 -0
  4. package/__tests__/e2e/e2e.graphql-ws.spec.ts +306 -0
  5. package/__tests__/e2e/e2e.react-subscriptions.spec.tsx +208 -0
  6. package/__tests__/features/adapter/adapter.browser.spec.ts +23 -5
  7. package/__tests__/features/adapter/adapter.document.spec.ts +17 -13
  8. package/__tests__/features/adapter/adapter.headers.spec.ts +65 -0
  9. package/__tests__/features/adapter/adapter.keys.spec.ts +235 -0
  10. package/__tests__/features/adapter/adapter.persisted-queries.spec.ts +207 -0
  11. package/__tests__/features/adapter/adapter.progress.spec.ts +39 -0
  12. package/__tests__/features/adapter/adapter.responses.spec.ts +162 -1
  13. package/__tests__/features/adapter/adapter.server.spec.ts +6 -6
  14. package/__tests__/features/adapter/adapter.types.spec.ts +121 -2
  15. package/__tests__/features/adapter/adapter.utils.spec.ts +4 -2
  16. package/__tests__/features/adapter/adapter.variables.spec.ts +3 -3
  17. package/__tests__/features/socket/socket.connection.spec.ts +892 -0
  18. package/__tests__/features/socket/socket.fuzz.spec.ts +329 -0
  19. package/__tests__/features/socket/socket.subscriptions.spec.ts +515 -0
  20. package/__tests__/features/socket/socket.types.spec.ts +160 -0
  21. package/__tests__/utils/controlled-websocket.ts +238 -0
  22. package/dist/adapter/adapter.constants.d.ts +5 -0
  23. package/dist/adapter/adapter.constants.d.ts.map +1 -1
  24. package/dist/adapter/adapter.types.d.ts +55 -2
  25. package/dist/adapter/adapter.types.d.ts.map +1 -1
  26. package/dist/adapter/adapter.utils.d.ts +51 -4
  27. package/dist/adapter/adapter.utils.d.ts.map +1 -1
  28. package/dist/adapter/http-adapter.fetch.d.ts.map +1 -1
  29. package/dist/index.d.ts +1 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.mjs +958 -128
  32. package/dist/index.mjs.map +1 -1
  33. package/dist/socket/index.d.ts +5 -0
  34. package/dist/socket/index.d.ts.map +1 -0
  35. package/dist/socket/socket.constants.d.ts +44 -0
  36. package/dist/socket/socket.constants.d.ts.map +1 -0
  37. package/dist/socket/socket.subscriptions.d.ts +74 -0
  38. package/dist/socket/socket.subscriptions.d.ts.map +1 -0
  39. package/dist/socket/socket.types.d.ts +102 -0
  40. package/dist/socket/socket.types.d.ts.map +1 -0
  41. package/dist/socket/socket.utils.d.ts +80 -0
  42. package/dist/socket/socket.utils.d.ts.map +1 -0
  43. package/dist/socket/socket.websocket.d.ts +30 -0
  44. package/dist/socket/socket.websocket.d.ts.map +1 -0
  45. package/package.json +14 -9
  46. package/src/adapter/adapter.constants.ts +6 -0
  47. package/src/adapter/adapter.types.ts +69 -3
  48. package/src/adapter/adapter.utils.ts +196 -25
  49. package/src/adapter/http-adapter.fetch.ts +93 -43
  50. package/src/index.ts +1 -0
  51. package/src/socket/index.ts +4 -0
  52. package/src/socket/socket.constants.ts +59 -0
  53. package/src/socket/socket.subscriptions.ts +160 -0
  54. package/src/socket/socket.types.ts +118 -0
  55. package/src/socket/socket.utils.ts +150 -0
  56. package/src/socket/socket.websocket.ts +555 -0
  57. package/vite.config.ts +4 -0
@@ -0,0 +1,238 @@
1
+ /**
2
+ * A WebSocket stand-in that does nothing on its own - the test plays the `graphql-transport-ws` server and decides
3
+ * when the connection opens, which messages arrive and when (and how) it closes. There are no timers and no network
4
+ * inside, so every scenario is deterministic and can be combined with fake timers freely.
5
+ *
6
+ * It follows the browser behaviour that matters for the adapter:
7
+ * - `close()` on a connection that is still CONNECTING fails it with `error` + `close` (code 1006)
8
+ * - `close()` on an OPEN connection ends with a clean `close` carrying the requested code
9
+ * - `send()` throws while the connection is not OPEN
10
+ */
11
+ export class ControlledWebSocket extends EventTarget {
12
+ static readonly CONNECTING = 0;
13
+ static readonly OPEN = 1;
14
+ static readonly CLOSING = 2;
15
+ static readonly CLOSED = 3;
16
+
17
+ /** Every instance created since the last `reset()`, in creation order */
18
+ static instances: ControlledWebSocket[] = [];
19
+ /** When set, the next constructor call throws it - like an invalid url does in the browser */
20
+ static constructorError: Error | null = null;
21
+
22
+ static reset() {
23
+ ControlledWebSocket.instances = [];
24
+ ControlledWebSocket.constructorError = null;
25
+ }
26
+
27
+ static get last(): ControlledWebSocket {
28
+ const instance = ControlledWebSocket.instances.at(-1);
29
+ if (!instance) {
30
+ throw new Error("[ControlledWebSocket] No WebSocket was created");
31
+ }
32
+ return instance;
33
+ }
34
+
35
+ /** Instances that are connecting or open - "how many live connections does the client hold" */
36
+ static get active(): ControlledWebSocket[] {
37
+ return ControlledWebSocket.instances.filter(
38
+ (instance) =>
39
+ instance.readyState === ControlledWebSocket.CONNECTING || instance.readyState === ControlledWebSocket.OPEN,
40
+ );
41
+ }
42
+
43
+ readonly CONNECTING = 0;
44
+ readonly OPEN = 1;
45
+ readonly CLOSING = 2;
46
+ readonly CLOSED = 3;
47
+
48
+ readyState: number = ControlledWebSocket.CONNECTING;
49
+ /** Frames passed to `send`, in order */
50
+ sent: string[] = [];
51
+ /** Arguments of client-side `close()` calls */
52
+ closeCalls: { code?: number; reason?: string }[] = [];
53
+
54
+ constructor(
55
+ public url: string,
56
+ public protocols?: string | string[],
57
+ ) {
58
+ super();
59
+ if (ControlledWebSocket.constructorError) {
60
+ const error = ControlledWebSocket.constructorError;
61
+ ControlledWebSocket.constructorError = null;
62
+ throw error;
63
+ }
64
+ ControlledWebSocket.instances.push(this);
65
+ }
66
+
67
+ send(frame: string) {
68
+ if (this.readyState !== ControlledWebSocket.OPEN) {
69
+ throw new Error("[ControlledWebSocket] Cannot send - the connection is not open");
70
+ }
71
+ this.sent.push(frame);
72
+ }
73
+
74
+ close(code?: number, reason?: string) {
75
+ this.closeCalls.push({ code, reason });
76
+ if (this.readyState === ControlledWebSocket.CLOSING || this.readyState === ControlledWebSocket.CLOSED) {
77
+ return;
78
+ }
79
+ const wasConnecting = this.readyState === ControlledWebSocket.CONNECTING;
80
+ this.readyState = ControlledWebSocket.CLOSED;
81
+ // Closing before the handshake finished fails the connection (1006), otherwise the close is clean
82
+ if (wasConnecting) {
83
+ this.dispatchEvent(new Event("error"));
84
+ this.dispatchEvent(new CloseEvent("close", { code: 1006, reason: "", wasClean: false }));
85
+ return;
86
+ }
87
+ this.dispatchEvent(new CloseEvent("close", { code: code ?? 1005, reason: reason ?? "", wasClean: true }));
88
+ }
89
+
90
+ // Server side - driven by the test
91
+
92
+ /** The server accepts the WebSocket connection - `connection_init` follows from the client */
93
+ serverOpen() {
94
+ if (this.readyState !== ControlledWebSocket.CONNECTING) {
95
+ throw new Error("[ControlledWebSocket] Only a connecting socket can be opened");
96
+ }
97
+ this.readyState = ControlledWebSocket.OPEN;
98
+ this.dispatchEvent(new Event("open"));
99
+ }
100
+
101
+ /** The server sends a raw frame */
102
+ serverSend(data: unknown) {
103
+ this.dispatchEvent(new MessageEvent("message", { data }));
104
+ }
105
+
106
+ /** The server sends a JSON message */
107
+ serverMessage(message: unknown) {
108
+ this.serverSend(JSON.stringify(message));
109
+ }
110
+
111
+ /** The server acknowledges `connection_init` */
112
+ serverAck(payload?: Record<string, unknown>) {
113
+ this.serverMessage(payload ? { type: "connection_ack", payload } : { type: "connection_ack" });
114
+ }
115
+
116
+ /** The server sends an event of the subscription */
117
+ serverNext(id: string, payload: Record<string, unknown>) {
118
+ this.serverMessage({ id, type: "next", payload });
119
+ }
120
+
121
+ /** The server fails the subscription */
122
+ serverError(id: string, errors: { message: string; [key: string]: unknown }[]) {
123
+ this.serverMessage({ id, type: "error", payload: errors });
124
+ }
125
+
126
+ /** The server completes the subscription */
127
+ serverComplete(id: string) {
128
+ this.serverMessage({ id, type: "complete" });
129
+ }
130
+
131
+ /**
132
+ * The handshake fails the way Node.js 22 reports it: an `error` event and no `close` - the socket stays CONNECTING.
133
+ * Browsers and newer Node.js versions always follow the error with a close event.
134
+ */
135
+ serverRefuseWithoutClose() {
136
+ this.dispatchEvent(new Event("error"));
137
+ }
138
+
139
+ /** The server (or the network) closes the connection */
140
+ serverClose({
141
+ code = 1006,
142
+ reason = "",
143
+ wasClean = false,
144
+ }: { code?: number; reason?: string; wasClean?: boolean } = {}) {
145
+ if (this.readyState === ControlledWebSocket.CLOSED) {
146
+ return;
147
+ }
148
+ this.readyState = ControlledWebSocket.CLOSED;
149
+ this.dispatchEvent(new CloseEvent("close", { code, reason, wasClean }));
150
+ }
151
+
152
+ /** Messages sent by the client */
153
+ get messages(): Record<string, any>[] {
154
+ return this.sent.map((frame) => JSON.parse(frame));
155
+ }
156
+
157
+ /** `subscribe` messages sent by the client */
158
+ get subscriptions(): { id: string; type: "subscribe"; payload: Record<string, any> }[] {
159
+ return this.messages.filter((message) => message.type === "subscribe") as never;
160
+ }
161
+
162
+ /** The last `subscribe` message - its id answers with `serverNext` / `serverError` / `serverComplete` */
163
+ get lastSubscription() {
164
+ const subscription = this.subscriptions.at(-1);
165
+ if (!subscription) {
166
+ throw new Error("[ControlledWebSocket] No subscription was sent");
167
+ }
168
+ return subscription;
169
+ }
170
+
171
+ /** Ids of the subscriptions the client completed */
172
+ get completed(): string[] {
173
+ return this.messages.filter((message) => message.type === "complete").map(({ id }) => id);
174
+ }
175
+ }
176
+
177
+ let originalWebSocket: typeof WebSocket | undefined;
178
+
179
+ /**
180
+ * Replaces the global WebSocket with {@link ControlledWebSocket} for the current test.
181
+ * Call `uninstallControlledWebSocket` in `afterEach`.
182
+ */
183
+ export const installControlledWebSocket = () => {
184
+ ControlledWebSocket.reset();
185
+ originalWebSocket ??= globalThis.WebSocket;
186
+ globalThis.WebSocket = ControlledWebSocket as unknown as typeof WebSocket;
187
+ return ControlledWebSocket;
188
+ };
189
+
190
+ export const uninstallControlledWebSocket = () => {
191
+ if (originalWebSocket) {
192
+ globalThis.WebSocket = originalWebSocket;
193
+ originalWebSocket = undefined;
194
+ }
195
+ ControlledWebSocket.reset();
196
+ };
197
+
198
+ /** Lets pending promise callbacks run - the adapter prepares every connection attempt asynchronously */
199
+ export const flushPromises = async (rounds = 10) => {
200
+ for (let i = 0; i < rounds; i += 1) {
201
+ // eslint-disable-next-line no-await-in-loop
202
+ await Promise.resolve();
203
+ }
204
+ };
205
+
206
+ /**
207
+ * Waits (promise ticks only) until the client created its `count`-th WebSocket and returns it.
208
+ * Defaults to "the next one that is not created yet".
209
+ */
210
+ export const waitForWebSocket = async (count = ControlledWebSocket.instances.length + 1) => {
211
+ for (let i = 0; i < 100 && ControlledWebSocket.instances.length < count; i += 1) {
212
+ // eslint-disable-next-line no-await-in-loop
213
+ await Promise.resolve();
214
+ }
215
+ const instance = ControlledWebSocket.instances[count - 1];
216
+ if (!instance) {
217
+ throw new Error(
218
+ `[ControlledWebSocket] Expected ${count} WebSocket(s), got ${ControlledWebSocket.instances.length}`,
219
+ );
220
+ }
221
+ return instance;
222
+ };
223
+
224
+ /** Waits for the WebSocket of a socket that is connecting right now and accepts it, without the acknowledgement */
225
+ export const openWebSocket = async (count = Math.max(ControlledWebSocket.instances.length, 1)) => {
226
+ const instance = await waitForWebSocket(count);
227
+ instance.serverOpen();
228
+ await flushPromises();
229
+ return instance;
230
+ };
231
+
232
+ /** Opens the WebSocket and acknowledges `connection_init` - the connection can carry subscriptions */
233
+ export const acknowledgeWebSocket = async (count = Math.max(ControlledWebSocket.instances.length, 1)) => {
234
+ const instance = await openWebSocket(count);
235
+ instance.serverAck();
236
+ await flushPromises();
237
+ return instance;
238
+ };
@@ -1,4 +1,9 @@
1
1
  import { GraphQlExtraType } from './adapter.types';
2
2
  export declare const defaultTimeout: number;
3
+ /**
4
+ * GraphQL over HTTP media type first, so spec compliant servers answer with meaningful status codes,
5
+ * and JSON as the fallback for the others.
6
+ */
7
+ export declare const gqlAcceptHeader = "application/graphql-response+json, application/json;q=0.9";
3
8
  export declare const gqlExtra: GraphQlExtraType;
4
9
  //# sourceMappingURL=adapter.constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.constants.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.constants.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,eAAO,MAAM,cAAc,QAAe,CAAC;AAE3C,eAAO,MAAM,QAAQ,EAAE,gBAGtB,CAAC"}
1
+ {"version":3,"file":"adapter.constants.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.constants.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,eAAO,MAAM,cAAc,QAAe,CAAC;AAE3C;;;GAGG;AACH,eAAO,MAAM,eAAe,8DAA8D,CAAC;AAE3F,eAAO,MAAM,QAAQ,EAAE,gBAGtB,CAAC"}
@@ -1,4 +1,5 @@
1
- import { Adapter, DefaultMapperType, EmptyTypes, QueryParamsType, stringifyQueryParams } from '@hyper-fetch/core';
1
+ import { Adapter, DefaultMapperType, EmptyTypes, ExtractEndpointPayloadType, ExtractEndpointResponseType, QueryParamsType, RequestInstance, stringifyQueryParams } from '@hyper-fetch/core';
2
+ import { GraphQLFormattedError } from 'graphql';
2
3
  import { DocumentNode } from 'graphql/language/ast';
3
4
  export declare enum GraphqlMethod {
4
5
  POST = "POST",
@@ -7,10 +8,45 @@ export declare enum GraphqlMethod {
7
8
  export type GraphQlExtraType = {
8
9
  headers: HeadersInit;
9
10
  extensions: Record<string, any>;
11
+ /**
12
+ * The `errors` of the response body, when the server sent any. With the `"all"` error policy a response with
13
+ * partial data succeeds, and this is where its errors are.
14
+ */
15
+ errors?: GraphQLFormattedError[];
10
16
  };
17
+ /**
18
+ * What a response that has both `data` and `errors` resolves to.
19
+ *
20
+ * - `"none"` - it fails: `data` is `null` and `error` holds the errors.
21
+ * - `"all"` - it succeeds with the partial `data`, and the errors are in `extra.errors`. A response without `data`
22
+ * still fails.
23
+ */
24
+ export type GraphqlErrorPolicy = "none" | "all";
25
+ /**
26
+ * Error of a GraphQL request: the `errors` of the response body, or a single network, abort or timeout error.
27
+ * Pass it to the client to type `response.error` - `new Client<GraphqlErrorType>({ url })`.
28
+ */
29
+ export type GraphqlErrorType = (GraphQLFormattedError | Error)[];
11
30
  export type GraphQlEndpointType = string | DocumentNode;
12
31
  export type FetchGraphqlAdapterOptionsType = Omit<RequestInit, "method" | "headers" | "body" | "signal"> & {
13
32
  timeout?: number;
33
+ /**
34
+ * What a response with both `data` and `errors` resolves to. `"none"` fails it, `"all"` keeps the partial data.
35
+ * @default "none"
36
+ */
37
+ errorPolicy?: GraphqlErrorPolicy;
38
+ /**
39
+ * Automatic persisted queries. The request sends the SHA-256 hash of the query instead of the query, and the full
40
+ * query only when the server does not know the hash yet. Hashed `GET` requests stay short, so CDNs can cache them.
41
+ * Without Web Crypto (`crypto.subtle`) the full query is sent.
42
+ * @default false
43
+ */
44
+ persistedQueries?: boolean;
45
+ };
46
+ /** The `persistedQuery` extension of automatic persisted queries */
47
+ export type GraphqlPersistedQueryType = {
48
+ version: 1;
49
+ sha256Hash: string;
14
50
  };
15
51
  export type GraphqlRequestOptionsType = {
16
52
  /**
@@ -19,7 +55,24 @@ export type GraphqlRequestOptionsType = {
19
55
  */
20
56
  document?: GraphQlEndpointType;
21
57
  };
22
- export type GraphqlAdapterType = Adapter<FetchGraphqlAdapterOptionsType, GraphqlMethod, number, GraphQlExtraType, QueryParamsType | string, QueryParamsType | string | undefined, GraphQlEndpointType, (endpoint: GraphQlEndpointType) => string, typeof stringifyQueryParams, DefaultMapperType, DefaultMapperType, GraphqlRequestOptionsType>;
58
+ /**
59
+ * Response and payload of a typed document - a `TypedDocumentNode` from graphql-codegen or a gql.tada document.
60
+ * `createRequest` reads them from a typed document passed as `endpoint` by itself. Use this type when the document is
61
+ * passed with the `document` option, or to combine the document types with other request types.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const getUser = client.createRequest<GraphqlDocumentTypes<typeof GetUserDocument>>()({
66
+ * endpoint: "/User",
67
+ * document: GetUserDocument,
68
+ * });
69
+ * ```
70
+ */
71
+ export type GraphqlDocumentTypes<Document> = {
72
+ response: ExtractEndpointResponseType<Document>;
73
+ payload: ExtractEndpointPayloadType<Document>;
74
+ };
75
+ export type GraphqlAdapterType = Adapter<FetchGraphqlAdapterOptionsType, GraphqlMethod, number, GraphQlExtraType, QueryParamsType | string, QueryParamsType | string | undefined, GraphQlEndpointType, (endpoint: GraphQlEndpointType) => string, typeof stringifyQueryParams, (request: RequestInstance) => HeadersInit, DefaultMapperType, GraphqlRequestOptionsType>;
23
76
  type GqlParamType = boolean | string | number | Record<string, any> | EmptyTypes;
24
77
  export type ExtractGqlRouteParams<T extends string> = string extends T ? EmptyTypes : T extends `${string}query ${infer Parameters} {${infer QueryRest}` ? Parameters extends `${string}$${infer Param},${infer Rest}` ? GqlParameter<Param, Rest> & (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>) : Parameters extends `${string}$${infer Param})${infer Rest}` ? GqlParameter<Param, Rest> & (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>) : EmptyTypes : T extends `${string}$${infer Parameters},${infer Rest}` ? GqlParameter<Parameters, Rest> : T extends `${string}$${infer Parameters})${infer Rest}` ? GqlParameter<Parameters, Rest> : EmptyTypes;
25
78
  export type GqlParameter<T extends string, Rest extends string> = T extends `${infer Param}:${string}!` ? {
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACvH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD,oBAAY,aAAa;IACvB,IAAI,SAAS;IACb,GAAG,QAAQ;CAC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IAAI,EAAE,GACrD,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,GAC9B,UAAU,CAAC;AAErB,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,SAAS,MAAM,IAAI,CAAC,SAAS,GAAG,MAAM,KAAK,IAAI,MAAM,GAAG,GACnG;KAAG,CAAC,IAAI,KAAK,GAAG,YAAY;CAAE,GAAG,CAAC,qBAAqB,CAAC,IAAI,CAAC,SAAS,UAAU,GAE5E,EAAE,GACF,qBAAqB,CAAC,IAAI,CAAC,CAAC,GAChC,CAAC,SAAS,GAAG,MAAM,KAAK,IAAI,MAAM,EAAE,GAClC;KAAG,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,YAAY;CAAE,GAAG,CAAC,qBAAqB,CAAC,IAAI,CAAC,SAAS,UAAU,GAE7E,EAAE,GACF,qBAAqB,CAAC,IAAI,CAAC,CAAC,GAChC,UAAU,CAAC"}
@@ -1,12 +1,31 @@
1
1
  import { RequestInstance } from '@hyper-fetch/core';
2
- import { GraphQlEndpointType } from './adapter.types';
2
+ import { GraphQlEndpointType, GraphqlPersistedQueryType } from './adapter.types';
3
+ /**
4
+ * A POST body is always JSON (`query`, `operationName`, `variables`, `extensions`), also for an operation without
5
+ * variables, so it needs the JSON content type. A GET request has no body and sends the operation in the URL, so it
6
+ * gets none - the header would only trigger a CORS preflight. A content type given with the request always wins.
7
+ */
8
+ export declare const getGqlHeaders: (request: RequestInstance) => HeadersInit;
3
9
  /** The document of the request - from the `document` option or, when it is omitted, from the `endpoint` */
4
10
  export declare const getGqlDocument: (request: RequestInstance) => GraphQlEndpointType;
5
- export declare const getRequestValues: <R extends RequestInstance>(request: R, queryParams?: unknown) => {
11
+ /**
12
+ * Url and body of the request in the GraphQL over HTTP format.
13
+ * With `persisted`, the `persistedQuery` extension is sent, and the query only when `withQuery` is set.
14
+ */
15
+ export declare const getRequestValues: <R extends RequestInstance>(request: R, queryParams?: unknown, persisted?: {
16
+ persistedQuery: GraphqlPersistedQueryType;
17
+ withQuery: boolean;
18
+ }) => {
6
19
  fullUrl: string;
7
20
  payload: string | null;
8
21
  };
9
22
  export declare const gqlEndpointMapper: <E extends GraphQlEndpointType>(endpoint: E) => string;
23
+ /**
24
+ * Name of the first operation of the document, sent as `operationName`.
25
+ * Anonymous operations and documents that do not parse have none.
26
+ */
27
+ export declare const getGqlOperationName: (document: GraphQlEndpointType) => string | undefined;
28
+ /** Readable name of the document - the operation name or, for anonymous operations, the first selected field */
10
29
  export declare const gqlEndpointNameMapper: (endpoint: GraphQlEndpointType) => string;
11
30
  /**
12
31
  * Name identifying the request in the cache, query and abort keys.
@@ -14,8 +33,36 @@ export declare const gqlEndpointNameMapper: (endpoint: GraphQlEndpointType) => s
14
33
  */
15
34
  export declare const gqlRequestNameMapper: (request: RequestInstance) => string;
16
35
  /**
17
- * Cache and query key of a request. The variables are a part of it: the same operation with other variables is
18
- * another query, with its own cached data and its own place in the queue.
36
+ * Cache and query key of a request. The document fingerprint and the variables are a part of it: two documents with
37
+ * one operation name but other selections, or the same operation with other variables, are other queries - with their
38
+ * own cached data and their own place in the queue.
19
39
  */
20
40
  export declare const gqlRequestKeyMapper: (request: RequestInstance) => string;
41
+ /**
42
+ * Matches the cache keys of every request of a GraphQL operation - with any variables, params, query params and
43
+ * method. Pass it to `client.cache.invalidate()` after a mutation, so the active queries of the operation refetch, like
44
+ * `refetchQueries` in Apollo.
45
+ *
46
+ * - A document matches its requests on every url path of the `document` option.
47
+ * - A request matches the requests of its document on its own url path.
48
+ * - Documents with the same operation name but other selections are other operations, they do not match.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * await updateUser.send({ payload: { id: "1", name: "Ann" } });
53
+ * client.cache.invalidate(matchGqlOperation(GetUserDocument));
54
+ * ```
55
+ */
56
+ export declare const matchGqlOperation: (operation: GraphQlEndpointType | RequestInstance) => RegExp;
57
+ /**
58
+ * SHA-256 hash of the query for automatic persisted queries, as lowercase hex.
59
+ * `null` when the runtime has no working Web Crypto - the full query is sent then.
60
+ */
61
+ export declare const getPersistedQueryHash: (query: string) => Promise<string | null>;
62
+ /**
63
+ * How the server answered a hashed request it could not run: it does not know the hash yet (`"not-found"`), or it does
64
+ * not support persisted queries (`"not-supported"`). `null` for every other response. Servers report it by the message
65
+ * or by the error code.
66
+ */
67
+ export declare const getPersistedQueryMiss: (body: unknown) => "not-found" | "not-supported" | null;
21
68
  //# sourceMappingURL=adapter.utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.utils.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA+B,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKtF,OAAO,KAAK,EAAE,mBAAmB,EAA6B,MAAM,iBAAiB,CAAC;AAQtF,2GAA2G;AAC3G,eAAO,MAAM,cAAc,GAAI,SAAS,eAAe,KAAG,mBAEzD,CAAC;AAgCF,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,eAAe,EACxD,SAAS,CAAC,EACV,cAAa,OAAgF;;;CAuB9F,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,mBAAmB,EAAE,UAAU,CAAC,KAAG,MAK9E,CAAC;AAwBF,eAAO,MAAM,qBAAqB,GAAI,UAAU,mBAAmB,KAAG,MAuBrE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,oBAAoB,GAAI,SAAS,eAAe,KAAG,MAI/D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAAI,SAAS,eAAe,KAAG,MAK9D,CAAC"}
1
+ {"version":3,"file":"adapter.utils.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA+B,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKtF,OAAO,KAAK,EAAE,mBAAmB,EAAE,yBAAyB,EAA6B,MAAM,iBAAiB,CAAC;AAajH;;;;GAIG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,CAAC;AAUzB,eAAO,MAAM,aAAa,QAAO,kBAkL5B,CAAC"}
1
+ {"version":3,"file":"http-adapter.fetch.d.ts","sourceRoot":"","sources":["../../src/adapter/http-adapter.fetch.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,kBAAkB,EAKnB,MAAM,iBAAiB,CAAC;AAczB,eAAO,MAAM,aAAa,QAAO,kBAgOhC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './adapter';
2
+ export * from './socket';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}