UNPKG

@graphql-inspector/action

Version:

GraphQL Inspector functionality for GitHub Actions

38 lines (37 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.countAliases = exports.validateAliasCount = void 0; const graphql_1 = require("graphql"); function validateAliasCount({ source, doc, maxAliasCount, fragmentGraph, }) { var _a; const getFragmentByFragmentName = (fragmentName) => fragmentGraph.getNodeData(fragmentName); for (const definition of doc.definitions) { if (definition.kind !== graphql_1.Kind.OPERATION_DEFINITION) { continue; } const aliasCount = countAliases(definition, getFragmentByFragmentName); if (aliasCount > maxAliasCount) { return new graphql_1.GraphQLError(`Too many aliases (${aliasCount}). Maximum allowed is ${maxAliasCount}`, [definition], source, ((_a = definition.loc) === null || _a === void 0 ? void 0 : _a.start) ? [definition.loc.start] : undefined); } } } exports.validateAliasCount = validateAliasCount; function countAliases(node, getFragmentByName) { let aliases = 0; if ('alias' in node && node.alias) { ++aliases; } if ('selectionSet' in node && node.selectionSet) { for (const child of node.selectionSet.selections) { aliases += countAliases(child, getFragmentByName); } } else if (node.kind === graphql_1.Kind.FRAGMENT_SPREAD) { const fragmentNode = getFragmentByName(node.name.value); if (fragmentNode) { aliases += countAliases(fragmentNode, getFragmentByName); } } return aliases; } exports.countAliases = countAliases;