Mirror: The spec-compliant minimum of client-side GraphQL.
1const fs = require('fs');
2const graphqlWeb = require('..');
3const graphql15 = require('graphql15');
4const graphql16 = require('graphql16');
5const graphql17 = require('graphql17');
6
7const kitchenSink =
8 fs.readFileSync('../src/__tests__/kitchen_sink.graphql', { encoding: 'utf8' });
9const document = require('../src/__tests__/kitchen_sink.json');
10
11suite('parse kitchen sink query', () => {
12 benchmark('0no-co/graphql.web', () => {
13 graphqlWeb.parse(kitchenSink);
14 });
15
16 benchmark('graphql@15', () => {
17 graphql15.parse(kitchenSink, { noLocation: true });
18 });
19
20 benchmark('graphql@16', () => {
21 graphql16.parse(kitchenSink, { noLocation: true });
22 });
23
24 benchmark('graphql@17', () => {
25 graphql17.parse(kitchenSink, { noLocation: true });
26 });
27});
28
29suite('print kitchen sink query', () => {
30 benchmark('0no-co/graphql.web', () => {
31 graphqlWeb.print(document);
32 });
33
34 benchmark('graphql@15', () => {
35 graphql15.print(document);
36 });
37
38 benchmark('graphql@16', () => {
39 graphql16.print(document);
40 });
41
42 benchmark('graphql@17', () => {
43 graphql17.print(document);
44 });
45});
46
47suite('visit kitchen sink query', () => {
48 benchmark('0no-co/graphql.web', () => {
49 graphqlWeb.visit(document, {
50 Field: formatNode,
51 InlineFragment: formatNode,
52 });
53 });
54
55 benchmark('graphql@15', () => {
56 graphql15.visit(document, {
57 Field: formatNode,
58 InlineFragment: formatNode,
59 });
60 });
61
62 benchmark('graphql@16', () => {
63 graphql16.visit(document, {
64 Field: formatNode,
65 InlineFragment: formatNode,
66 });
67 });
68
69 benchmark('graphql@17', () => {
70 graphql17.visit(document, {
71 Field: formatNode,
72 InlineFragment: formatNode,
73 });
74 });
75});
76
77function formatNode(node) {
78 if (!node.selectionSet) return node;
79 for (const selection of node.selectionSet.selections)
80 if (
81 selection.kind === 'Field' &&
82 selection.name.value === '__typename' &&
83 !selection.alias
84 )
85 return node;
86
87 return {
88 ...node,
89 selectionSet: {
90 ...node.selectionSet,
91 selections: [
92 ...node.selectionSet.selections,
93 {
94 kind: 'Field',
95 name: {
96 kind: 'Name',
97 value: '__typename',
98 },
99 },
100 ],
101 },
102 };
103}