Skip to content

Commit 696028e

Browse files
committed
Test
1 parent 1a653dd commit 696028e

File tree

7 files changed

+229
-63
lines changed

7 files changed

+229
-63
lines changed

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@apollo/composition": "2.9.3",
5656
"@apollo/subgraph": "2.9.3",
5757
"async-retry": "1.3.3",
58+
"@urql/core": "5.1.0",
5859
"dotenv": "16.4.7",
5960
"fets": "0.8.4",
6061
"get-port": "7.1.0",

src/test-suites/defer/shared-root/category.subgraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default createSubgraph("category", {
4343
},
4444
Product: {
4545
async category(p: typeof product) {
46-
await waitFor(1000);
46+
await waitFor(2000);
4747
return p.category;
4848
},
4949
},

src/test-suites/defer/shared-root/name.subgraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default createSubgraph("name", {
4444
},
4545
Product: {
4646
async name(p: typeof product) {
47-
await waitFor(2000);
47+
await waitFor(1000);
4848
return p.name;
4949
},
5050
},

src/test-suites/defer/shared-root/test.ts

Lines changed: 102 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,135 @@ export default [
66
query {
77
product {
88
id
9-
name {
10-
id
11-
brand
12-
model
9+
... @defer {
10+
name {
11+
id
12+
brand
13+
model
14+
}
1315
}
14-
category {
15-
id
16-
name
16+
... @defer {
17+
category {
18+
id
19+
name
20+
}
1721
}
18-
price {
19-
id
20-
amount
21-
currency
22+
... @defer {
23+
price {
24+
id
25+
amount
26+
currency
27+
}
2228
}
2329
}
2430
}
2531
`,
26-
{
27-
data: {
28-
product: {
29-
id: "1",
30-
name: {
32+
[
33+
{
34+
data: {
35+
product: {
3136
id: "1",
32-
brand: "Brand 1",
33-
model: "Model 1",
3437
},
35-
price: {
38+
},
39+
},
40+
{
41+
data: {
42+
product: {
3643
id: "1",
37-
amount: 1000,
38-
currency: "USD",
44+
name: {
45+
id: "1",
46+
brand: "Brand 1",
47+
model: "Model 1",
48+
},
3949
},
40-
category: {
50+
},
51+
},
52+
{
53+
data: {
54+
product: {
4155
id: "1",
42-
name: "Category 1",
56+
name: {
57+
id: "1",
58+
brand: "Brand 1",
59+
model: "Model 1",
60+
},
61+
category: {
62+
id: "1",
63+
name: "Category 1",
64+
},
4365
},
4466
},
4567
},
46-
}
47-
),
48-
createTest(
49-
/* GraphQL */ `
50-
query {
51-
products {
52-
id
53-
name {
54-
id
55-
brand
56-
model
57-
}
58-
category {
59-
id
60-
name
61-
}
62-
price {
63-
id
64-
amount
65-
currency
66-
}
67-
}
68-
}
69-
`,
70-
{
71-
data: {
72-
products: [
73-
{
68+
{
69+
data: {
70+
product: {
7471
id: "1",
7572
name: {
7673
id: "1",
7774
brand: "Brand 1",
7875
model: "Model 1",
7976
},
77+
category: {
78+
id: "1",
79+
name: "Category 1",
80+
},
8081
price: {
8182
id: "1",
8283
amount: 1000,
8384
currency: "USD",
8485
},
85-
category: {
86-
id: "1",
87-
name: "Category 1",
88-
},
8986
},
90-
],
87+
},
9188
},
89+
],
90+
{
91+
accept: 'multipart/mixed; boundary="-"; deferSpec=20220824;',
9292
}
9393
),
94+
// createTest(
95+
// /* GraphQL */ `
96+
// query {
97+
// products {
98+
// id
99+
// name {
100+
// id
101+
// brand
102+
// model
103+
// }
104+
// category {
105+
// id
106+
// name
107+
// }
108+
// price {
109+
// id
110+
// amount
111+
// currency
112+
// }
113+
// }
114+
// }
115+
// `,
116+
// {
117+
// data: {
118+
// products: [
119+
// {
120+
// id: "1",
121+
// name: {
122+
// id: "1",
123+
// brand: "Brand 1",
124+
// model: "Model 1",
125+
// },
126+
// price: {
127+
// id: "1",
128+
// amount: 1000,
129+
// currency: "USD",
130+
// },
131+
// category: {
132+
// id: "1",
133+
// name: "Category 1",
134+
// },
135+
// },
136+
// ],
137+
// },
138+
// }
139+
// ),
94140
];

src/test.ts

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ExecutionResult } from "graphql";
1+
import { ExecutionResult, GraphQLError } from "graphql";
2+
import { createClient, fetchExchange } from "@urql/core";
23
import type { createTest } from "./testkit.js";
34
import { diff } from "jest-diff";
45
import { test } from "node:test";
@@ -38,6 +39,91 @@ const tests = await fetchTests(TESTS_ENDPOINT);
3839
let index = 0;
3940
for (const { query, expected: expectedResult, headers } of tests) {
4041
test(`${index++}`, async () => {
42+
if (Array.isArray(expectedResult)) {
43+
// we're testing @defer here
44+
const client = createClient({
45+
url: GRAPHQL_ENDPOINT,
46+
exchanges: [fetchExchange],
47+
fetchOptions: () => {
48+
return {
49+
headers: {
50+
Accept: "multipart/mixed",
51+
...headers,
52+
},
53+
};
54+
},
55+
});
56+
57+
const res = client.query(
58+
query,
59+
{},
60+
{
61+
requestPolicy: "network-only",
62+
}
63+
);
64+
65+
const receivedResponses: Array<{
66+
data: Record<string, unknown> | null;
67+
errors: GraphQLError[] | null;
68+
}> = [];
69+
70+
await new Promise<void>((resolve) => {
71+
let timeout: ReturnType<typeof setTimeout>;
72+
let unsubscribe: () => void = () => {};
73+
74+
function unsubscribeAfterTime() {
75+
if (timeout) {
76+
clearTimeout(timeout);
77+
}
78+
79+
timeout = setTimeout(() => {
80+
unsubscribe();
81+
resolve();
82+
}, 5_000);
83+
}
84+
85+
unsubscribeAfterTime();
86+
87+
const subscription = res.subscribe((result) => {
88+
unsubscribeAfterTime();
89+
receivedResponses.push({
90+
data: result.data,
91+
errors: result.error?.graphQLErrors ?? null,
92+
});
93+
});
94+
95+
unsubscribe = subscription.unsubscribe;
96+
});
97+
98+
for (let i = 0; i < expectedResult.length; i++) {
99+
const expectedResultItem = expectedResult[i];
100+
const errorsOptional = typeof expectedResultItem.errors !== "boolean";
101+
const response = receivedResponses[i];
102+
103+
const received = {
104+
data: response.data ?? null,
105+
errors: errorsOptional
106+
? null
107+
: response.errors?.length
108+
? true
109+
: false,
110+
};
111+
112+
const expected = {
113+
data: expectedResultItem.data ?? null,
114+
errors: errorsOptional ? null : (expectedResultItem.errors ?? false),
115+
};
116+
117+
deepStrictEqual(
118+
received,
119+
expected,
120+
[`Test failed for query`, query, diff(expected, received)].join("\n")
121+
);
122+
}
123+
124+
return;
125+
}
126+
41127
const response = await graphql(GRAPHQL_ENDPOINT, query, headers);
42128

43129
const errorsOptional = typeof expectedResult.errors !== "boolean";

src/testkit.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
export function createTest(
22
query: string,
3-
expected: {
4-
data?: any;
5-
errors?: boolean;
6-
},
3+
expected:
4+
| {
5+
data?: any;
6+
errors?: boolean;
7+
}
8+
| Array<{ data?: any; errors?: boolean }>,
79
headers?: Record<string, string>
810
) {
911
return {

0 commit comments

Comments
 (0)