-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathdirectives-perf-test.js
80 lines (70 loc) · 1.81 KB
/
directives-perf-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// @flow strict
import { expect } from 'chai';
import { it } from 'mocha';
import { parse } from '../../language/parser';
import { GraphQLSchema } from '../../type/schema';
import { GraphQLString } from '../../type/scalars';
import { GraphQLObjectType } from '../../type/definition';
import { execute } from '../execute';
import { forAwaitEach, isAsyncIterable } from 'iterall';
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'TestType',
fields: {
a: { type: GraphQLString },
b: { type: GraphQLString },
},
}),
});
let count = 0;
const rootValue = {
a() {
return 'a';
},
b() {
count += 1;
return `b${count}`;
},
};
function executeTestQuery(query) {
const document = parse(query);
return execute({ schema, document, rootValue });
}
it('performance test, if the same field is deferred several times, its resolve is called only once', async () => {
const index = 100;
let fragString = '';
let fragElementString = '';
for (const i of Array.from(Array(index).keys())) {
fragString += `
...Frag${i} @defer(label: "Frag_b_defer${i}")
`;
fragElementString += `
fragment Frag${i} on TestType {
b
}
`;
}
const result = await executeTestQuery(`
query {
a
${fragString}
}
${fragElementString}
`);
expect(isAsyncIterable(result)).to.equal(true);
const results = [];
await forAwaitEach(((result: any): AsyncIterable<mixed>), value => {
results.push(value);
});
expect(results.length).to.equal(index + 1);
expect(results[0]).to.deep.equal({
data: { a: 'a' },
});
for (const i of Array.from(Array(index).keys())) {
expect(results[i + 1]).to.deep.equal({
data: { b: 'b1' },
label: `Frag_b_defer${i}`,
path: [],
});
}
});