@hyper-fetch/graphql 10.0.0 → 10.1.1
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/README.md +152 -84
- package/__tests__/e2e/e2e.conformance.spec.ts +206 -0
- package/__tests__/e2e/e2e.graphql-ws-server.ts +187 -0
- package/__tests__/e2e/e2e.graphql-ws.spec.ts +306 -0
- package/__tests__/e2e/e2e.react-subscriptions.spec.tsx +208 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +23 -5
- package/__tests__/features/adapter/adapter.document.spec.ts +17 -13
- package/__tests__/features/adapter/adapter.headers.spec.ts +65 -0
- package/__tests__/features/adapter/adapter.keys.spec.ts +235 -0
- package/__tests__/features/adapter/adapter.persisted-queries.spec.ts +207 -0
- package/__tests__/features/adapter/adapter.progress.spec.ts +39 -0
- package/__tests__/features/adapter/adapter.responses.spec.ts +162 -1
- package/__tests__/features/adapter/adapter.server.spec.ts +6 -6
- package/__tests__/features/adapter/adapter.types.spec.ts +121 -2
- package/__tests__/features/adapter/adapter.utils.spec.ts +4 -2
- package/__tests__/features/adapter/adapter.variables.spec.ts +3 -3
- package/__tests__/features/socket/socket.connection.spec.ts +892 -0
- package/__tests__/features/socket/socket.fuzz.spec.ts +329 -0
- package/__tests__/features/socket/socket.subscriptions.spec.ts +515 -0
- package/__tests__/features/socket/socket.types.spec.ts +160 -0
- package/__tests__/utils/controlled-websocket.ts +238 -0
- package/dist/adapter/adapter.constants.d.ts +5 -0
- package/dist/adapter/adapter.constants.d.ts.map +1 -1
- package/dist/adapter/adapter.types.d.ts +55 -2
- package/dist/adapter/adapter.types.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts +51 -4
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +958 -128
- package/dist/index.mjs.map +1 -1
- package/dist/socket/index.d.ts +5 -0
- package/dist/socket/index.d.ts.map +1 -0
- package/dist/socket/socket.constants.d.ts +44 -0
- package/dist/socket/socket.constants.d.ts.map +1 -0
- package/dist/socket/socket.subscriptions.d.ts +74 -0
- package/dist/socket/socket.subscriptions.d.ts.map +1 -0
- package/dist/socket/socket.types.d.ts +102 -0
- package/dist/socket/socket.types.d.ts.map +1 -0
- package/dist/socket/socket.utils.d.ts +80 -0
- package/dist/socket/socket.utils.d.ts.map +1 -0
- package/dist/socket/socket.websocket.d.ts +30 -0
- package/dist/socket/socket.websocket.d.ts.map +1 -0
- package/package.json +17 -18
- package/src/adapter/adapter.constants.ts +6 -0
- package/src/adapter/adapter.types.ts +69 -3
- package/src/adapter/adapter.utils.ts +196 -25
- package/src/adapter/http-adapter.fetch.ts +93 -43
- package/src/index.ts +1 -0
- package/src/socket/index.ts +4 -0
- package/src/socket/socket.constants.ts +59 -0
- package/src/socket/socket.subscriptions.ts +160 -0
- package/src/socket/socket.types.ts +118 -0
- package/src/socket/socket.utils.ts +150 -0
- package/src/socket/socket.websocket.ts +555 -0
- package/vite.config.ts +4 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Client } from "@hyper-fetch/core";
|
|
2
|
+
import { GraphqlAdapter } from "adapter";
|
|
3
|
+
|
|
4
|
+
const url = "https://shared-base-url/graphql";
|
|
5
|
+
const getUsers = "query GetUsers { users { name } }";
|
|
6
|
+
|
|
7
|
+
/** Fetch has no upload events - only the response shows that the whole body was sent. */
|
|
8
|
+
describe("Graphql Adapter [ Progress ]", () => {
|
|
9
|
+
let client = new Client({ url }).setAdapter(GraphqlAdapter());
|
|
10
|
+
let fetchSpy = vi.spyOn(globalThis, "fetch");
|
|
11
|
+
let bodySent = false;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
bodySent = false;
|
|
15
|
+
client = new Client({ url }).setAdapter(GraphqlAdapter());
|
|
16
|
+
fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async () => {
|
|
17
|
+
bodySent = true;
|
|
18
|
+
return Response.json({ data: { users: [] } });
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
fetchSpy.mockRestore();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should report the finished upload only once the body was sent", async () => {
|
|
27
|
+
const progress: { loaded: number; total: number; bodySent: boolean }[] = [];
|
|
28
|
+
const size = new Blob([JSON.stringify({ query: getUsers, operationName: "GetUsers" })]).size;
|
|
29
|
+
|
|
30
|
+
await client
|
|
31
|
+
.createRequest()({ endpoint: getUsers })
|
|
32
|
+
.send({ onUploadProgress: ({ loaded, total }) => progress.push({ loaded, total, bodySent }) });
|
|
33
|
+
|
|
34
|
+
expect(progress).toStrictEqual([
|
|
35
|
+
{ loaded: 0, total: size, bodySent: false },
|
|
36
|
+
{ loaded: size, total: size, bodySent: true },
|
|
37
|
+
]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -86,6 +86,14 @@ describe("Graphql Adapter [ Responses ]", () => {
|
|
|
86
86
|
expect(await send()).toMatchObject({ data: null, error: [{ message: "Partial" }], success: false });
|
|
87
87
|
});
|
|
88
88
|
|
|
89
|
+
it("should expose the GraphQL errors in the extra of a failed response", async () => {
|
|
90
|
+
respond('{"data":null,"errors":[{"message":"Not allowed"}]}');
|
|
91
|
+
|
|
92
|
+
const response = await send();
|
|
93
|
+
|
|
94
|
+
expect(response.extra?.errors).toEqual([{ message: "Not allowed" }]);
|
|
95
|
+
});
|
|
96
|
+
|
|
89
97
|
it.each([400, 401, 403, 404, 500, 503])("should return the errors of a %i response", async (status) => {
|
|
90
98
|
respond('{"errors":[{"message":"Bad"}]}', { status });
|
|
91
99
|
|
|
@@ -147,11 +155,123 @@ describe("Graphql Adapter [ Responses ]", () => {
|
|
|
147
155
|
|
|
148
156
|
expect(response.success).toBe(false);
|
|
149
157
|
expect(response.status).toBe(0);
|
|
150
|
-
expect(response.error).toEqual(getErrorMessage("timeout"));
|
|
158
|
+
expect(response.error).toEqual([getErrorMessage("timeout")]);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("with the error policy", () => {
|
|
163
|
+
const sendWithPolicy = (errorPolicy?: "none" | "all") =>
|
|
164
|
+
client
|
|
165
|
+
.createRequest<{ response: { a: number; b: number | null } }>()({
|
|
166
|
+
endpoint: "query Q { a b }",
|
|
167
|
+
options: { errorPolicy },
|
|
168
|
+
})
|
|
169
|
+
.send();
|
|
170
|
+
|
|
171
|
+
it.each([undefined, "none"] as const)("should fail on partial data with the %s policy", async (errorPolicy) => {
|
|
172
|
+
respond('{"data":{"a":1,"b":null},"errors":[{"message":"No b","path":["b"]}]}');
|
|
173
|
+
|
|
174
|
+
expect(await sendWithPolicy(errorPolicy)).toMatchObject({
|
|
175
|
+
data: null,
|
|
176
|
+
error: [{ message: "No b", path: ["b"] }],
|
|
177
|
+
success: false,
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("should succeed with the partial data and keep the errors in the extra with the all policy", async () => {
|
|
182
|
+
respond('{"data":{"a":1,"b":null},"errors":[{"message":"No b","path":["b"]}]}');
|
|
183
|
+
|
|
184
|
+
const response = await sendWithPolicy("all");
|
|
185
|
+
|
|
186
|
+
expect(response).toMatchObject({ data: { a: 1, b: null }, error: null, success: true, status: 200 });
|
|
187
|
+
expect(response.extra?.errors).toEqual([{ message: "No b", path: ["b"] }]);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("should fail errors without data with the all policy", async () => {
|
|
191
|
+
respond('{"data":null,"errors":[{"message":"Not allowed"}]}');
|
|
192
|
+
|
|
193
|
+
expect(await sendWithPolicy("all")).toMatchObject({
|
|
194
|
+
data: null,
|
|
195
|
+
error: [{ message: "Not allowed" }],
|
|
196
|
+
success: false,
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("should fail a failed status with data with the all policy", async () => {
|
|
201
|
+
respond('{"data":{"a":1,"b":null},"errors":[{"message":"Down"}]}', { status: 500 });
|
|
202
|
+
|
|
203
|
+
expect(await sendWithPolicy("all")).toMatchObject({ data: null, error: [{ message: "Down" }], success: false });
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("should not add errors to the extra of a response without them with the all policy", async () => {
|
|
207
|
+
respond('{"data":{"a":1,"b":2}}');
|
|
208
|
+
|
|
209
|
+
const response = await sendWithPolicy("all");
|
|
210
|
+
|
|
211
|
+
expect(response).toMatchObject({ data: { a: 1, b: 2 }, success: true });
|
|
212
|
+
expect(response.extra).not.toHaveProperty("errors");
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("should take the policy from the adapter defaults and let the request options override it", async () => {
|
|
216
|
+
const partial = '{"data":{"a":1,"b":null},"errors":[{"message":"No b"}]}';
|
|
217
|
+
const defaulted = new Client({ url }).setAdapter(
|
|
218
|
+
GraphqlAdapter().setAdapterDefaults(() => ({ errorPolicy: "all" })),
|
|
219
|
+
);
|
|
220
|
+
const request = defaulted.createRequest<{ response: { a: number; b: number | null } }>()({
|
|
221
|
+
endpoint: "query Q { a b }",
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
respond(partial);
|
|
225
|
+
expect(await request.send()).toMatchObject({ data: { a: 1, b: null }, success: true });
|
|
226
|
+
|
|
227
|
+
respond(partial);
|
|
228
|
+
expect(await request.setOptions({ errorPolicy: "none" }).send()).toMatchObject({ data: null, success: false });
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("should keep the partial data in the cache with the all policy", async () => {
|
|
232
|
+
respond('{"data":{"a":1,"b":null},"errors":[{"message":"No b"}]}');
|
|
233
|
+
const cachingClient = new Client({ url, mode: "client" }).setAdapter(GraphqlAdapter());
|
|
234
|
+
const request = cachingClient.createRequest<{ response: { a: number; b: number | null } }>()({
|
|
235
|
+
endpoint: "query Q { a b }",
|
|
236
|
+
options: { errorPolicy: "all" },
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
await request.send();
|
|
240
|
+
|
|
241
|
+
expect(cachingClient.cache.get(request.cacheKey)).toMatchObject({
|
|
242
|
+
data: { a: 1, b: null },
|
|
243
|
+
extra: { errors: [{ message: "No b" }] },
|
|
244
|
+
});
|
|
151
245
|
});
|
|
152
246
|
});
|
|
153
247
|
|
|
154
248
|
describe("when sending the operation", () => {
|
|
249
|
+
it("should accept the GraphQL response media type with JSON as the fallback", async () => {
|
|
250
|
+
respond('{"data":{"a":1}}');
|
|
251
|
+
|
|
252
|
+
await send();
|
|
253
|
+
|
|
254
|
+
expect(fetchSpy.mock.calls[0][1]?.headers).toMatchObject({
|
|
255
|
+
Accept: "application/graphql-response+json, application/json;q=0.9",
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it.each(["Accept", "accept"])("should keep an %s header set by the user", async (key) => {
|
|
260
|
+
respond('{"data":{"a":1}}');
|
|
261
|
+
|
|
262
|
+
await client
|
|
263
|
+
.createRequest<{ response: { a: number } }>()({
|
|
264
|
+
endpoint: "query Q { a }",
|
|
265
|
+
headers: { [key]: "application/json" },
|
|
266
|
+
})
|
|
267
|
+
.send();
|
|
268
|
+
|
|
269
|
+
const headers = fetchSpy.mock.calls[0][1]?.headers as Record<string, string>;
|
|
270
|
+
expect(Object.entries(headers).filter(([name]) => name.toLowerCase() === "accept")).toStrictEqual([
|
|
271
|
+
[key, "application/json"],
|
|
272
|
+
]);
|
|
273
|
+
});
|
|
274
|
+
|
|
155
275
|
it("should POST the query and the variables as JSON", async () => {
|
|
156
276
|
respond('{"data":{"a":1}}');
|
|
157
277
|
const request = client.createRequest<{ response: { a: number }; payload: { id: number } }>()({
|
|
@@ -166,6 +286,7 @@ describe("Graphql Adapter [ Responses ]", () => {
|
|
|
166
286
|
expect(init?.method).toBe("POST");
|
|
167
287
|
expect(JSON.parse(String(init?.body))).toEqual({
|
|
168
288
|
query: queryWithId,
|
|
289
|
+
operationName: "Q",
|
|
169
290
|
variables: { id: 7 },
|
|
170
291
|
});
|
|
171
292
|
expect(init?.headers).toMatchObject({ Authorization: "Bearer token" });
|
|
@@ -186,11 +307,51 @@ describe("Graphql Adapter [ Responses ]", () => {
|
|
|
186
307
|
const [input, init] = fetchSpy.mock.calls[0];
|
|
187
308
|
const params = new URL(String(input)).searchParams;
|
|
188
309
|
expect(params.get("query")).toBe(queryWithFilter);
|
|
310
|
+
expect(params.get("operationName")).toBe("Q");
|
|
189
311
|
expect(JSON.parse(params.get("variables") ?? "")).toEqual({ filter: { q: "x y&z=1", tags: ["ą", "b"] } });
|
|
190
312
|
expect(init?.method).toBe("GET");
|
|
191
313
|
expect(init?.body).toBeUndefined();
|
|
192
314
|
});
|
|
193
315
|
|
|
316
|
+
it("should not send an operationName for an anonymous operation", async () => {
|
|
317
|
+
respond('{"data":{"a":1}}');
|
|
318
|
+
|
|
319
|
+
await client.createRequest<{ response: { a: number } }>()({ endpoint: "{ a }" }).send();
|
|
320
|
+
|
|
321
|
+
expect(JSON.parse(String(fetchSpy.mock.calls[0][1]?.body))).toEqual({ query: "{ a }" });
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("should send the name of the first operation when the document holds many", async () => {
|
|
325
|
+
respond('{"data":{"a":1}}');
|
|
326
|
+
const document = "query First { a } query Second { b }";
|
|
327
|
+
|
|
328
|
+
await client.createRequest<{ response: { a: number } }>()({ endpoint: document }).send();
|
|
329
|
+
|
|
330
|
+
expect(JSON.parse(String(fetchSpy.mock.calls[0][1]?.body))).toMatchObject({ operationName: "First" });
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("should not send an operationName for a document that does not parse", async () => {
|
|
334
|
+
respond('{"errors":[{"message":"Syntax Error"}]}', { status: 400 });
|
|
335
|
+
|
|
336
|
+
await client.createRequest<{ response: { a: number } }>()({ endpoint: "query Q {" }).send();
|
|
337
|
+
|
|
338
|
+
expect(JSON.parse(String(fetchSpy.mock.calls[0][1]?.body))).toEqual({ query: "query Q {" });
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("should keep the operationName of the document over a query param with the same key in GET", async () => {
|
|
342
|
+
respond('{"data":{"a":1}}');
|
|
343
|
+
const request = client
|
|
344
|
+
.createRequest<{ response: { a: number }; queryParams: { operationName: string } }>()({
|
|
345
|
+
endpoint: "query Q { a }",
|
|
346
|
+
method: GraphqlMethod.GET,
|
|
347
|
+
})
|
|
348
|
+
.setQueryParams({ operationName: "Other" });
|
|
349
|
+
|
|
350
|
+
await request.send();
|
|
351
|
+
|
|
352
|
+
expect(new URL(String(fetchSpy.mock.calls[0][0])).searchParams.getAll("operationName")).toStrictEqual(["Q"]);
|
|
353
|
+
});
|
|
354
|
+
|
|
194
355
|
it("should pass fetch options through", async () => {
|
|
195
356
|
respond('{"data":{"a":1}}');
|
|
196
357
|
const request = client.createRequest<{ response: { a: number } }>()({
|
|
@@ -87,7 +87,7 @@ describe("Graphql Adapter [ Server ]", () => {
|
|
|
87
87
|
expect(expected.data).toStrictEqual(data);
|
|
88
88
|
expect(status).toBe(200);
|
|
89
89
|
expect(extra).toMatchObject({
|
|
90
|
-
headers: { "content-type": "application/json", "content-length": "48" },
|
|
90
|
+
headers: { "content-type": "application/graphql-response+json", "content-length": "48" },
|
|
91
91
|
extensions: {},
|
|
92
92
|
});
|
|
93
93
|
});
|
|
@@ -101,7 +101,7 @@ describe("Graphql Adapter [ Server ]", () => {
|
|
|
101
101
|
expect(status).toBe(200);
|
|
102
102
|
expect(error).toBe(null);
|
|
103
103
|
expect(extra).toMatchObject({
|
|
104
|
-
headers: { "content-type": "application/json", "content-length": "48" },
|
|
104
|
+
headers: { "content-type": "application/graphql-response+json", "content-length": "48" },
|
|
105
105
|
extensions: {},
|
|
106
106
|
});
|
|
107
107
|
});
|
|
@@ -118,7 +118,7 @@ describe("Graphql Adapter [ Server ]", () => {
|
|
|
118
118
|
expect(status).toBe(200);
|
|
119
119
|
expect(error).toBe(null);
|
|
120
120
|
expect(extra).toMatchObject({
|
|
121
|
-
headers: { "content-type": "application/json", "content-length": "48" },
|
|
121
|
+
headers: { "content-type": "application/graphql-response+json", "content-length": "48" },
|
|
122
122
|
extensions: {},
|
|
123
123
|
});
|
|
124
124
|
});
|
|
@@ -132,7 +132,7 @@ describe("Graphql Adapter [ Server ]", () => {
|
|
|
132
132
|
expect(status).toBe(400);
|
|
133
133
|
expect(error).toStrictEqual(expected.errors);
|
|
134
134
|
expect(extra).toMatchObject({
|
|
135
|
-
headers: { "content-type": "application/json", "content-length": "42" },
|
|
135
|
+
headers: { "content-type": "application/graphql-response+json", "content-length": "42" },
|
|
136
136
|
extensions: {},
|
|
137
137
|
});
|
|
138
138
|
});
|
|
@@ -150,7 +150,7 @@ describe("Graphql Adapter [ Server ]", () => {
|
|
|
150
150
|
expect(status).toBe(200);
|
|
151
151
|
expect(error).toBe(null);
|
|
152
152
|
expect(extra).toMatchObject({
|
|
153
|
-
headers: { "content-type": "application/json", "content-length": "48" },
|
|
153
|
+
headers: { "content-type": "application/graphql-response+json", "content-length": "48" },
|
|
154
154
|
extensions: {},
|
|
155
155
|
});
|
|
156
156
|
});
|
|
@@ -179,7 +179,7 @@ describe("Graphql Adapter [ Server ]", () => {
|
|
|
179
179
|
|
|
180
180
|
expect(data).toBeNull();
|
|
181
181
|
expect(status).toBe(0);
|
|
182
|
-
expect(error).toStrictEqual(getErrorMessage("timeout"));
|
|
182
|
+
expect(error).toStrictEqual([getErrorMessage("timeout")]);
|
|
183
183
|
});
|
|
184
184
|
|
|
185
185
|
// it("should allow to calculate payload size", async () => {
|
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
import type { ClientInstance, ExtractAdapterRequestOptionsType, RequestInstance } from "@hyper-fetch/core";
|
|
2
2
|
import { Client, HttpAdapter } from "@hyper-fetch/core";
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
ExtractGqlRouteParams,
|
|
5
|
+
GraphqlAdapterType,
|
|
6
|
+
GraphqlDocumentTypes,
|
|
7
|
+
GraphqlErrorType,
|
|
8
|
+
GraphqlRequestOptionsType,
|
|
9
|
+
} from "adapter";
|
|
4
10
|
import { GraphqlAdapter } from "adapter";
|
|
5
|
-
import type { DocumentNode } from "graphql";
|
|
11
|
+
import type { DocumentNode, GraphQLFormattedError } from "graphql";
|
|
6
12
|
import { expectTypeOf } from "vitest";
|
|
7
13
|
|
|
8
14
|
import { getUserQuery } from "../../constants/queries.constants";
|
|
9
15
|
|
|
10
16
|
type UserResponse = { username: string };
|
|
11
17
|
|
|
18
|
+
/** The decoration graphql-codegen and gql.tada add to documents - `DocumentTypeDecoration` of `@graphql-typed-document-node/core` */
|
|
19
|
+
type TypedDocument<Result, Variables> = DocumentNode & { __apiType?: (variables: Variables) => Result };
|
|
20
|
+
|
|
21
|
+
type GetUserResult = { user: { name: string } };
|
|
22
|
+
const GetUserDocument = getUserQuery as TypedDocument<GetUserResult, { id: string; active?: boolean }>;
|
|
23
|
+
// Codegen types an operation without variables this way
|
|
24
|
+
const GetUsersDocument = getUserQuery as TypedDocument<{ users: { name: string }[] }, { [key: string]: never }>;
|
|
25
|
+
|
|
12
26
|
describe("Graphql Adapter [ Types ]", () => {
|
|
13
27
|
const client = new Client({ url: "https://shared-base-url/graphql" }).setAdapter(GraphqlAdapter());
|
|
14
28
|
|
|
@@ -28,6 +42,15 @@ describe("Graphql Adapter [ Types ]", () => {
|
|
|
28
42
|
expectTypeOf(client.createRequest()).parameter(0).toEqualTypeOf<{ endpoint: string; document: number }>();
|
|
29
43
|
});
|
|
30
44
|
|
|
45
|
+
it("should send a string document with variables without asking for url params", () => {
|
|
46
|
+
const deleteUser = client.createRequest<{ response: UserResponse; payload: { id: string } }>()({
|
|
47
|
+
endpoint: "mutation DeleteUser($id: ID!) { deleteUser(id: $id) { username } }",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expectTypeOf(deleteUser.send).toBeCallableWith({ payload: { id: "1" } });
|
|
51
|
+
expectTypeOf(deleteUser.setPayload({ id: "1" }).send).toBeCallableWith();
|
|
52
|
+
});
|
|
53
|
+
|
|
31
54
|
it("should compile the example from the proposal", () => {
|
|
32
55
|
const getUser = client.createRequest<{ response: UserResponse; payload: { id: number } }>()({
|
|
33
56
|
endpoint: "/User",
|
|
@@ -112,4 +135,100 @@ describe("Graphql Adapter [ Types ]", () => {
|
|
|
112
135
|
// @ts-expect-error id is required by the document
|
|
113
136
|
request.setPayload({ name: "a" });
|
|
114
137
|
});
|
|
138
|
+
|
|
139
|
+
describe("when typing requests from a typed document", () => {
|
|
140
|
+
it("should take the response and the variables from the document", () => {
|
|
141
|
+
const request = client.createRequest<GraphqlDocumentTypes<typeof GetUserDocument>>()({
|
|
142
|
+
endpoint: GetUserDocument,
|
|
143
|
+
});
|
|
144
|
+
const send = () => request.send({ payload: { id: "1" } });
|
|
145
|
+
|
|
146
|
+
expectTypeOf<Awaited<ReturnType<typeof send>>["data"]>().toEqualTypeOf<GetUserResult | null>();
|
|
147
|
+
request.send({ payload: { id: "1", active: true } });
|
|
148
|
+
// @ts-expect-error the id variable is a string
|
|
149
|
+
request.send({ payload: { id: 1 } });
|
|
150
|
+
// @ts-expect-error the id variable is required
|
|
151
|
+
request.send({ payload: { active: true } });
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("should not need a payload for a document without variables", () => {
|
|
155
|
+
const request = client.createRequest<GraphqlDocumentTypes<typeof GetUsersDocument>>()({
|
|
156
|
+
endpoint: GetUsersDocument,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expectTypeOf<GraphqlDocumentTypes<typeof GetUsersDocument>["payload"]>().toEqualTypeOf<undefined>();
|
|
160
|
+
expectTypeOf(request.send).toBeCallableWith();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("should work with the document option and other request types", () => {
|
|
164
|
+
const request = client.createRequest<
|
|
165
|
+
GraphqlDocumentTypes<typeof GetUserDocument> & { queryParams: { source: string } }
|
|
166
|
+
>()({ endpoint: "/User", document: GetUserDocument });
|
|
167
|
+
|
|
168
|
+
request.setQueryParams({ source: "app" });
|
|
169
|
+
// @ts-expect-error wrong query params
|
|
170
|
+
request.setQueryParams({ source: 1 });
|
|
171
|
+
request.setPayload({ id: "1" });
|
|
172
|
+
// @ts-expect-error the id variable is a string
|
|
173
|
+
request.setPayload({ id: 1 });
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("when inferring the types from a typed document endpoint", () => {
|
|
178
|
+
it("should take the response and the variables without generics", () => {
|
|
179
|
+
const request = client.createRequest()({ endpoint: GetUserDocument });
|
|
180
|
+
const send = () => request.send({ payload: { id: "1" } });
|
|
181
|
+
|
|
182
|
+
expectTypeOf<Awaited<ReturnType<typeof send>>["data"]>().toEqualTypeOf<GetUserResult | null>();
|
|
183
|
+
// @ts-expect-error the id variable is a string
|
|
184
|
+
request.send({ payload: { id: 1 } });
|
|
185
|
+
// @ts-expect-error the id variable is required
|
|
186
|
+
request.send({ payload: { active: true } });
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("should not need a payload for a document without variables", () => {
|
|
190
|
+
const request = client.createRequest()({ endpoint: GetUsersDocument });
|
|
191
|
+
|
|
192
|
+
expectTypeOf(request.send).toBeCallableWith();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("should keep the declared generics over the document", () => {
|
|
196
|
+
const request = client.createRequest<{ response: UserResponse }>()({ endpoint: GetUserDocument });
|
|
197
|
+
const send = () => request.send({ payload: { id: "1" } });
|
|
198
|
+
|
|
199
|
+
expectTypeOf<Awaited<ReturnType<typeof send>>["data"]>().toEqualTypeOf<UserResponse | null>();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("should keep an untyped DocumentNode untyped", () => {
|
|
203
|
+
const request = client.createRequest()({ endpoint: getUserQuery });
|
|
204
|
+
const send = () => request.send();
|
|
205
|
+
|
|
206
|
+
expectTypeOf<Awaited<ReturnType<typeof send>>["data"]>().toEqualTypeOf<undefined | null>();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe("when typing errors", () => {
|
|
211
|
+
it("should type the response error with GraphqlErrorType", () => {
|
|
212
|
+
const typed = new Client<GraphqlErrorType>({ url: "" }).setAdapter(GraphqlAdapter());
|
|
213
|
+
const send = () => typed.createRequest<{ response: UserResponse }>()({ endpoint: getUserQuery }).send();
|
|
214
|
+
|
|
215
|
+
expectTypeOf<GraphqlErrorType[number]>().toEqualTypeOf<GraphQLFormattedError | Error>();
|
|
216
|
+
expectTypeOf<Awaited<ReturnType<typeof send>>["error"]>().toExtend<GraphqlErrorType | null>();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should type the GraphQL errors in the extra", () => {
|
|
220
|
+
const send = () => client.createRequest<{ response: UserResponse }>()({ endpoint: getUserQuery }).send();
|
|
221
|
+
|
|
222
|
+
expectTypeOf<NonNullable<Awaited<ReturnType<typeof send>>["extra"]>["errors"]>().toEqualTypeOf<
|
|
223
|
+
GraphQLFormattedError[] | undefined
|
|
224
|
+
>();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("should accept only known error policies", () => {
|
|
228
|
+
client.createRequest()({ endpoint: getUserQuery, options: { errorPolicy: "all" } });
|
|
229
|
+
client.createRequest()({ endpoint: getUserQuery, options: { errorPolicy: "none" } });
|
|
230
|
+
// @ts-expect-error unknown error policy
|
|
231
|
+
client.createRequest()({ endpoint: getUserQuery, options: { errorPolicy: "ignore" } });
|
|
232
|
+
});
|
|
233
|
+
});
|
|
115
234
|
});
|
|
@@ -36,7 +36,9 @@ describe("Graphql Adapter [ Utils ]", () => {
|
|
|
36
36
|
const { fullUrl, payload } = getRequestValues(request);
|
|
37
37
|
|
|
38
38
|
expect(fullUrl).toBe("https://shared-base-url/graphql");
|
|
39
|
-
expect(payload).toBe(
|
|
39
|
+
expect(payload).toBe(
|
|
40
|
+
String.raw`{"query":"query GetUser {\n username {\n username\n firstName\n }\n}","operationName":"GetUser"}`,
|
|
41
|
+
);
|
|
40
42
|
expect(request.method).toBe("POST");
|
|
41
43
|
expect(request.endpoint).toBeString();
|
|
42
44
|
});
|
|
@@ -44,7 +46,7 @@ describe("Graphql Adapter [ Utils ]", () => {
|
|
|
44
46
|
const { fullUrl, payload } = getRequestValues(requestGet);
|
|
45
47
|
|
|
46
48
|
expect(fullUrl).toBe(
|
|
47
|
-
"https://shared-base-url/graphql?query=%0Aquery%20GetUser%20%7B%0A%20%20username%20%7B%0A%20%20%20%20username%0A%20%20%20%20firstName%0A%20%20%7D%0A%7D%0A",
|
|
49
|
+
"https://shared-base-url/graphql?query=%0Aquery%20GetUser%20%7B%0A%20%20username%20%7B%0A%20%20%20%20username%0A%20%20%20%20firstName%0A%20%20%7D%0A%7D%0A&operationName=GetUser",
|
|
48
50
|
);
|
|
49
51
|
expect(payload).toBeNull();
|
|
50
52
|
expect(requestGet.method).toBe("GET");
|
|
@@ -59,13 +59,13 @@ describe("Graphql Adapter [ Variables ]", () => {
|
|
|
59
59
|
expect(request.setPayload({ id: 1 }).queryKey).toBe(request.setPayload({ id: 1 }).queryKey);
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
it("should keep the keys of a request without
|
|
62
|
+
it("should keep the variables out of the keys of a request without them", () => {
|
|
63
63
|
const request = client.createRequest<{ response: UserResponse }>()({
|
|
64
64
|
endpoint: "query GetUsers { users { name } }",
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
expect(request.cacheKey).
|
|
68
|
-
expect(request.queryKey).
|
|
67
|
+
expect(request.cacheKey).toMatch(/^POST_GetUsers@\w+--$/);
|
|
68
|
+
expect(request.queryKey).toMatch(/^POST_GetUsers@\w+--$/);
|
|
69
69
|
});
|
|
70
70
|
|
|
71
71
|
it("should keep one abort key for an operation, whatever its variables", () => {
|