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