UNPKG

@tmeasday/aer-limited

Version:

Do not use if you don't know what this does.

91 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const graphql_1 = require("graphql"); function hideLiterals(ast) { return graphql_1.visit(ast, { IntValue(node) { return Object.assign({}, node, { value: '0' }); }, FloatValue(node) { return Object.assign({}, node, { value: '0' }); }, StringValue(node) { return Object.assign({}, node, { value: '', block: false }); }, ListValue(node) { return Object.assign({}, node, { values: [] }); }, ObjectValue(node) { return Object.assign({}, node, { fields: [] }); }, }); } exports.hideLiterals = hideLiterals; function dropUnusedDefinitions(ast, operationName) { const separated = graphql_1.separateOperations(ast)[operationName]; if (!separated) { return ast; } return separated; } exports.dropUnusedDefinitions = dropUnusedDefinitions; function sorted(items, ...iteratees) { if (items) { return lodash_1.sortBy(items, ...iteratees); } return undefined; } function sortAST(ast) { return graphql_1.visit(ast, { OperationDefinition(node) { return Object.assign({}, node, { variableDefinitions: sorted(node.variableDefinitions, 'variable.name.value') }); }, SelectionSet(node) { return Object.assign({}, node, { selections: lodash_1.sortBy(node.selections, 'kind', 'name.value') }); }, Field(node) { return Object.assign({}, node, { arguments: sorted(node.arguments, 'name.value') }); }, FragmentSpread(node) { return Object.assign({}, node, { directives: sorted(node.directives, 'name.value') }); }, InlineFragment(node) { return Object.assign({}, node, { directives: sorted(node.directives, 'name.value') }); }, FragmentDefinition(node) { return Object.assign({}, node, { directives: sorted(node.directives, 'name.value'), variableDefinitions: sorted(node.variableDefinitions, 'variable.name.value') }); }, Directive(node) { return Object.assign({}, node, { arguments: sorted(node.arguments, 'name.value') }); }, }); } exports.sortAST = sortAST; function removeAliases(ast) { return graphql_1.visit(ast, { Field(node) { return Object.assign({}, node, { alias: undefined }); }, }); } exports.removeAliases = removeAliases; function printWithReducedWhitespace(ast) { const sanitizedAST = graphql_1.visit(ast, { StringValue(node) { return Object.assign({}, node, { value: Buffer.from(node.value, 'utf8').toString('hex'), block: false }); }, }); const withWhitespace = graphql_1.print(sanitizedAST); const minimizedButStillHex = withWhitespace .replace(/\s+/g, ' ') .replace(/([^_a-zA-Z0-9]) /g, (_, c) => c) .replace(/ ([^_a-zA-Z0-9])/g, (_, c) => c); return minimizedButStillHex.replace(/"([a-f0-9]+)"/g, (_, hex) => JSON.stringify(Buffer.from(hex, 'hex').toString('utf8'))); } exports.printWithReducedWhitespace = printWithReducedWhitespace; function defaultSignature(ast, operationName) { return printWithReducedWhitespace(sortAST(removeAliases(hideLiterals(dropUnusedDefinitions(ast, operationName))))); } exports.defaultSignature = defaultSignature; //# sourceMappingURL=signature.js.map