@graphql-inspector/core 0.0.0-canary.e88122e → 0.0.0-canary.fec2df4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import { __awaiter } from 'tslib';
2
- import { Kind, TypeInfo, visit, visitWithTypeInfo, GraphQLError, getNamedType, isScalarType, isInterfaceType, isObjectType, isUnionType, isInputObjectType, isListType, isNonNullType, isWrappingType, isEnumType, parse, extendSchema, print, validate as validate$1, printType } from 'graphql';
2
+ import { Kind, TypeInfo, visit, visitWithTypeInfo, GraphQLError, getNamedType, isScalarType, isInterfaceType, isObjectType, isUnionType, isInputObjectType, isListType, isNonNullType, isWrappingType, isEnumType, parse, extendSchema, print, validate as validate$1, printType, TokenKind } from 'graphql';
3
3
  import inspect from 'object-inspect';
4
4
  import { DepGraph } from 'dependency-graph';
5
+ import { Parser } from 'graphql/language/parser';
5
6
 
6
7
  function keyMap(list, keyFn) {
7
8
  return list.reduce((map, item) => {
@@ -43,7 +44,7 @@ function isVoid(a) {
43
44
  return typeof a === 'undefined' || a === null;
44
45
  }
45
46
  function diffArrays(a, b) {
46
- return a.filter((c) => !b.some((d) => isEqual(d, c)));
47
+ return a.filter(c => !b.some(d => isEqual(d, c)));
47
48
  }
48
49
  function compareLists(oldList, newList, callbacks) {
49
50
  const oldMap = keyMap(oldList, ({ name }) => name);
@@ -94,7 +95,7 @@ function isDeprecated(fieldOrEnumValue) {
94
95
  if (fieldOrEnumValue.deprecationReason != null) {
95
96
  return true;
96
97
  }
97
- if ((_b = (_a = fieldOrEnumValue.astNode) === null || _a === void 0 ? void 0 : _a.directives) === null || _b === void 0 ? void 0 : _b.some((directive) => directive.name.value === 'deprecated')) {
98
+ if ((_b = (_a = fieldOrEnumValue.astNode) === null || _a === void 0 ? void 0 : _a.directives) === null || _b === void 0 ? void 0 : _b.some(directive => directive.name.value === 'deprecated')) {
98
99
  return true;
99
100
  }
100
101
  return false;
@@ -109,8 +110,7 @@ function safeChangeForField(oldType, newType) {
109
110
  return safeChangeForField(ofType, newType.ofType);
110
111
  }
111
112
  if (isListType(oldType)) {
112
- return ((isListType(newType) &&
113
- safeChangeForField(oldType.ofType, newType.ofType)) ||
113
+ return ((isListType(newType) && safeChangeForField(oldType.ofType, newType.ofType)) ||
114
114
  (isNonNullType(newType) && safeChangeForField(oldType, newType.ofType)));
115
115
  }
116
116
  return false;
@@ -145,7 +145,7 @@ function getTypePrefix(type) {
145
145
  return kindsMap[kind.toString()];
146
146
  }
147
147
  function isPrimitive(type) {
148
- return (['String', 'Int', 'Float', 'Boolean', 'ID'].indexOf(typeof type === 'string' ? type : type.name) !== -1);
148
+ return ['String', 'Int', 'Float', 'Boolean', 'ID'].indexOf(typeof type === 'string' ? type : type.name) !== -1;
149
149
  }
150
150
  function isForIntrospection(type) {
151
151
  return ([
@@ -170,7 +170,9 @@ function findDeprecatedUsages(schema, ast) {
170
170
  if (reason) {
171
171
  const fieldDef = typeInfo.getFieldDef();
172
172
  if (fieldDef) {
173
- errors.push(new GraphQLError(`The argument '${argument === null || argument === void 0 ? void 0 : argument.name}' of '${fieldDef.name}' is deprecated. ${reason}`, [node]));
173
+ errors.push(new GraphQLError(`The argument '${argument === null || argument === void 0 ? void 0 : argument.name}' of '${fieldDef.name}' is deprecated. ${reason}`, [
174
+ node,
175
+ ]));
174
176
  }
175
177
  }
176
178
  }
@@ -200,7 +202,7 @@ function findDeprecatedUsages(schema, ast) {
200
202
  }
201
203
  function removeFieldIfDirectives(node, directiveNames) {
202
204
  if (node.directives) {
203
- if (node.directives.some((d) => directiveNames.indexOf(d.name.value) !== -1)) {
205
+ if (node.directives.some(d => directiveNames.indexOf(d.name.value) !== -1)) {
204
206
  return null;
205
207
  }
206
208
  }
@@ -208,7 +210,7 @@ function removeFieldIfDirectives(node, directiveNames) {
208
210
  }
209
211
  function removeDirectives(node, directiveNames) {
210
212
  if (node.directives) {
211
- return Object.assign(Object.assign({}, node), { directives: node.directives.filter((d) => directiveNames.indexOf(d.name.value) === -1) });
213
+ return Object.assign(Object.assign({}, node), { directives: node.directives.filter(d => directiveNames.indexOf(d.name.value) === -1) });
212
214
  }
213
215
  return node;
214
216
  }
@@ -258,11 +260,7 @@ function getReachableTypes(schema) {
258
260
  }
259
261
  }
260
262
  };
261
- for (const type of [
262
- schema.getQueryType(),
263
- schema.getMutationType(),
264
- schema.getSubscriptionType(),
265
- ]) {
263
+ for (const type of [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]) {
266
264
  if (type) {
267
265
  collect(type);
268
266
  }
@@ -504,9 +502,7 @@ function directiveLocationRemoved(directive, location) {
504
502
  function directiveArgumentAdded(directive, arg) {
505
503
  return {
506
504
  criticality: {
507
- level: isNonNullType(arg.type)
508
- ? CriticalityLevel.Breaking
509
- : CriticalityLevel.NonBreaking,
505
+ level: isNonNullType(arg.type) ? CriticalityLevel.Breaking : CriticalityLevel.NonBreaking,
510
506
  },
511
507
  type: ChangeType.DirectiveArgumentAdded,
512
508
  message: `Argument '${arg.name}' was added to directive '${directive.name}'`,
@@ -705,7 +701,7 @@ function compareTwoStrings(str1, str2) {
705
701
  const pairs2 = wordLetterPairs(str2);
706
702
  const union = pairs1.length + pairs2.length;
707
703
  let intersection = 0;
708
- pairs1.forEach((pair1) => {
704
+ pairs1.forEach(pair1 => {
709
705
  for (let i = 0, pair2; (pair2 = pairs2[i]); i++) {
710
706
  if (pair1 !== pair2)
711
707
  continue;
@@ -719,7 +715,7 @@ function compareTwoStrings(str1, str2) {
719
715
  function findBestMatch(mainString, targetStrings) {
720
716
  if (!areArgsValid(mainString, targetStrings))
721
717
  throw new Error('Bad arguments: First argument should be a string, second should be an array of strings');
722
- const ratings = targetStrings.map((target) => ({
718
+ const ratings = targetStrings.map(target => ({
723
719
  target,
724
720
  rating: compareTwoStrings(mainString, target.value),
725
721
  }));
@@ -727,9 +723,7 @@ function findBestMatch(mainString, targetStrings) {
727
723
  return { ratings, bestMatch };
728
724
  }
729
725
  function flattenDeep(arr) {
730
- return Array.isArray(arr)
731
- ? arr.reduce((a, b) => a.concat(flattenDeep(b)), [])
732
- : [arr];
726
+ return Array.isArray(arr) ? arr.reduce((a, b) => a.concat(flattenDeep(b)), []) : [arr];
733
727
  }
734
728
  function areArgsValid(mainString, targetStrings) {
735
729
  if (typeof mainString !== 'string')
@@ -738,7 +732,7 @@ function areArgsValid(mainString, targetStrings) {
738
732
  return false;
739
733
  if (!targetStrings.length)
740
734
  return false;
741
- if (targetStrings.find((s) => typeof s.value !== 'string'))
735
+ if (targetStrings.find(s => typeof s.value !== 'string'))
742
736
  return false;
743
737
  return true;
744
738
  }
@@ -753,7 +747,9 @@ function wordLetterPairs(str) {
753
747
  return flattenDeep(pairs);
754
748
  }
755
749
  function safeString(obj) {
756
- return inspect(obj).replace(/\[Object\: null prototype\] /g, '').replace(/(^')|('$)/g, '');
750
+ return inspect(obj)
751
+ .replace(/\[Object\: null prototype\] /g, '')
752
+ .replace(/(^')|('$)/g, '');
757
753
  }
758
754
 
759
755
  function inputFieldRemoved(input, field) {
@@ -868,14 +864,12 @@ function changesInInputField(input, oldField, newField, addChange) {
868
864
  }
869
865
  }
870
866
  if (isNotEqual(oldField.defaultValue, newField.defaultValue)) {
871
- if (Array.isArray(oldField.defaultValue) &&
872
- Array.isArray(newField.defaultValue)) {
867
+ if (Array.isArray(oldField.defaultValue) && Array.isArray(newField.defaultValue)) {
873
868
  if (diffArrays(oldField.defaultValue, newField.defaultValue).length > 0) {
874
869
  addChange(inputFieldDefaultValueChanged(input, oldField, newField));
875
870
  }
876
871
  }
877
- else if (JSON.stringify(oldField.defaultValue) !==
878
- JSON.stringify(newField.defaultValue)) {
872
+ else if (JSON.stringify(oldField.defaultValue) !== JSON.stringify(newField.defaultValue)) {
879
873
  addChange(inputFieldDefaultValueChanged(input, oldField, newField));
880
874
  }
881
875
  }
@@ -1099,15 +1093,13 @@ function changesInArgument(type, field, oldArg, newArg, addChange) {
1099
1093
  addChange(fieldArgumentDescriptionChanged(type, field, oldArg, newArg));
1100
1094
  }
1101
1095
  if (isNotEqual(oldArg.defaultValue, newArg.defaultValue)) {
1102
- if (Array.isArray(oldArg.defaultValue) &&
1103
- Array.isArray(newArg.defaultValue)) {
1096
+ if (Array.isArray(oldArg.defaultValue) && Array.isArray(newArg.defaultValue)) {
1104
1097
  const diff = diffArrays(oldArg.defaultValue, newArg.defaultValue);
1105
1098
  if (diff.length > 0) {
1106
1099
  addChange(fieldArgumentDefaultChanged(type, field, oldArg, newArg));
1107
1100
  }
1108
1101
  }
1109
- else if (JSON.stringify(oldArg.defaultValue) !==
1110
- JSON.stringify(newArg.defaultValue)) {
1102
+ else if (JSON.stringify(oldArg.defaultValue) !== JSON.stringify(newArg.defaultValue)) {
1111
1103
  addChange(fieldArgumentDefaultChanged(type, field, oldArg, newArg));
1112
1104
  }
1113
1105
  }
@@ -1212,9 +1204,9 @@ function changesInDirective(oldDirective, newDirective, addChange) {
1212
1204
  removed: diffArrays(oldDirective.locations, newDirective.locations),
1213
1205
  };
1214
1206
  // locations added
1215
- locations.added.forEach((location) => addChange(directiveLocationAdded(newDirective, location)));
1207
+ locations.added.forEach(location => addChange(directiveLocationAdded(newDirective, location)));
1216
1208
  // locations removed
1217
- locations.removed.forEach((location) => addChange(directiveLocationRemoved(oldDirective, location)));
1209
+ locations.removed.forEach(location => addChange(directiveLocationRemoved(oldDirective, location)));
1218
1210
  compareLists(oldDirective.args, newDirective.args, {
1219
1211
  onAdded(arg) {
1220
1212
  addChange(directiveArgumentAdded(newDirective, arg));
@@ -1245,7 +1237,7 @@ function diffSchema(oldSchema, newSchema) {
1245
1237
  changes.push(change);
1246
1238
  }
1247
1239
  changesInSchema(oldSchema, newSchema, addChange);
1248
- compareLists(Object.values(oldSchema.getTypeMap()).filter((t) => !isPrimitive(t)), Object.values(newSchema.getTypeMap()).filter((t) => !isPrimitive(t)), {
1240
+ compareLists(Object.values(oldSchema.getTypeMap()).filter(t => !isPrimitive(t)), Object.values(newSchema.getTypeMap()).filter(t => !isPrimitive(t)), {
1249
1241
  onAdded(type) {
1250
1242
  addChange(typeAdded(type));
1251
1243
  },
@@ -1330,7 +1322,7 @@ function changesInType(oldType, newType, addChange) {
1330
1322
  }
1331
1323
 
1332
1324
  const dangerousBreaking = ({ changes }) => {
1333
- return changes.map((change) => {
1325
+ return changes.map(change => {
1334
1326
  if (change.criticality.level === CriticalityLevel.Dangerous) {
1335
1327
  return Object.assign(Object.assign({}, change), { criticality: Object.assign(Object.assign({}, change.criticality), { level: CriticalityLevel.Breaking }) });
1336
1328
  }
@@ -1342,8 +1334,8 @@ function parsePath(path) {
1342
1334
  return path.split('.');
1343
1335
  }
1344
1336
 
1345
- const suppressRemovalOfDeprecatedField = ({ changes, oldSchema, }) => {
1346
- return changes.map((change) => {
1337
+ const suppressRemovalOfDeprecatedField = ({ changes, oldSchema }) => {
1338
+ return changes.map(change => {
1347
1339
  if (change.type === ChangeType.FieldRemoved &&
1348
1340
  change.criticality.level === CriticalityLevel.Breaking &&
1349
1341
  change.path) {
@@ -1398,15 +1390,15 @@ const descriptionChangeTypes = [
1398
1390
  ChangeType.TypeDescriptionChanged,
1399
1391
  ];
1400
1392
  const ignoreDescriptionChanges = ({ changes }) => {
1401
- return changes.filter((change) => descriptionChangeTypes.indexOf(change.type) === -1);
1393
+ return changes.filter(change => descriptionChangeTypes.indexOf(change.type) === -1);
1402
1394
  };
1403
1395
 
1404
- const considerUsage = ({ changes, config, }) => __awaiter(void 0, void 0, void 0, function* () {
1396
+ const considerUsage = ({ changes, config }) => __awaiter(void 0, void 0, void 0, function* () {
1405
1397
  if (!config) {
1406
1398
  throw new Error(`considerUsage rule is missing config`);
1407
1399
  }
1408
1400
  const collectedBreakingField = [];
1409
- changes.forEach((change) => {
1401
+ changes.forEach(change => {
1410
1402
  if (change.criticality.level === CriticalityLevel.Breaking && change.path) {
1411
1403
  const [typeName, fieldName, argumentName] = parsePath(change.path);
1412
1404
  collectedBreakingField.push({
@@ -1423,11 +1415,11 @@ const considerUsage = ({ changes, config, }) => __awaiter(void 0, void 0, void 0
1423
1415
  const suppressedPaths = collectedBreakingField
1424
1416
  .filter((_, i) => usageList[i] === true)
1425
1417
  .map(({ type, field, argument }) => [type, field, argument].filter(Boolean).join('.'));
1426
- return changes.map((change) => {
1418
+ return changes.map(change => {
1427
1419
  // Turns those "safe to break" changes into "dangerous"
1428
1420
  if (change.criticality.level === CriticalityLevel.Breaking &&
1429
1421
  change.path &&
1430
- suppressedPaths.some((p) => change.path.startsWith(p))) {
1422
+ suppressedPaths.some(p => change.path.startsWith(p))) {
1431
1423
  return Object.assign(Object.assign({}, change), { criticality: Object.assign(Object.assign({}, change.criticality), { level: CriticalityLevel.Dangerous }), message: `${change.message} (non-breaking based on usage)` });
1432
1424
  }
1433
1425
  return change;
@@ -1436,7 +1428,7 @@ const considerUsage = ({ changes, config, }) => __awaiter(void 0, void 0, void 0
1436
1428
 
1437
1429
  const safeUnreachable = ({ changes, oldSchema }) => {
1438
1430
  const reachable = getReachableTypes(oldSchema);
1439
- return changes.map((change) => {
1431
+ return changes.map(change => {
1440
1432
  if (change.criticality.level === CriticalityLevel.Breaking && change.path) {
1441
1433
  const [typeName] = parsePath(change.path);
1442
1434
  if (!reachable.has(typeName)) {
@@ -1543,7 +1535,7 @@ function calculateDepth({ node, currentDepth, maxDepth, getFragment, }) {
1543
1535
  return 1 + maxInnerDepth;
1544
1536
  }
1545
1537
  case Kind.SELECTION_SET: {
1546
- return Math.max(...node.selections.map((selection) => {
1538
+ return Math.max(...node.selections.map(selection => {
1547
1539
  return calculateDepth({
1548
1540
  node: selection,
1549
1541
  currentDepth: currentDepth,
@@ -1553,7 +1545,7 @@ function calculateDepth({ node, currentDepth, maxDepth, getFragment, }) {
1553
1545
  }));
1554
1546
  }
1555
1547
  case Kind.DOCUMENT: {
1556
- return Math.max(...node.definitions.map((def) => {
1548
+ return Math.max(...node.definitions.map(def => {
1557
1549
  return calculateDepth({
1558
1550
  node: def,
1559
1551
  currentDepth: currentDepth,
@@ -1565,7 +1557,7 @@ function calculateDepth({ node, currentDepth, maxDepth, getFragment, }) {
1565
1557
  case Kind.OPERATION_DEFINITION:
1566
1558
  case Kind.INLINE_FRAGMENT:
1567
1559
  case Kind.FRAGMENT_DEFINITION: {
1568
- return Math.max(...node.selectionSet.selections.map((selection) => {
1560
+ return Math.max(...node.selectionSet.selections.map(selection => {
1569
1561
  return calculateDepth({
1570
1562
  node: selection,
1571
1563
  currentDepth,
@@ -1586,13 +1578,26 @@ function calculateDepth({ node, currentDepth, maxDepth, getFragment, }) {
1586
1578
  }
1587
1579
  }
1588
1580
  }
1581
+ function countDepth(node, parentDepth, getFragmentReference) {
1582
+ let depth = parentDepth;
1583
+ if ('selectionSet' in node && node.selectionSet) {
1584
+ for (let child of node.selectionSet.selections) {
1585
+ depth = Math.max(depth, countDepth(child, parentDepth + 1, getFragmentReference));
1586
+ }
1587
+ }
1588
+ if (node.kind == Kind.FRAGMENT_SPREAD) {
1589
+ const fragment = getFragmentReference(node.name.value);
1590
+ if (fragment) {
1591
+ depth = Math.max(depth, countDepth(fragment, parentDepth + 1, getFragmentReference));
1592
+ }
1593
+ }
1594
+ return depth;
1595
+ }
1589
1596
 
1590
1597
  function transformDocumentWithApollo(doc, { keepClientFields }) {
1591
1598
  return visit(doc, {
1592
1599
  Field(node) {
1593
- return keepClientFields
1594
- ? removeDirectives(node, ['client'])
1595
- : removeFieldIfDirectives(node, ['client']);
1600
+ return keepClientFields ? removeDirectives(node, ['client']) : removeFieldIfDirectives(node, ['client']);
1596
1601
  },
1597
1602
  });
1598
1603
  }
@@ -1602,6 +1607,68 @@ function transformSchemaWithApollo(schema) {
1602
1607
  `));
1603
1608
  }
1604
1609
 
1610
+ function validateAliasCount({ source, doc, maxAliasCount, fragmentGraph, }) {
1611
+ const getFragmentByFragmentName = (fragmentName) => fragmentGraph.getNodeData(fragmentName);
1612
+ for (const definition of doc.definitions) {
1613
+ if (definition.kind !== Kind.OPERATION_DEFINITION) {
1614
+ continue;
1615
+ }
1616
+ const aliasCount = countAliases(definition, getFragmentByFragmentName);
1617
+ if (aliasCount > maxAliasCount) {
1618
+ return new GraphQLError(`Too many aliases (${aliasCount}). Maximum allowed is ${maxAliasCount}`, [definition], source, definition.loc && definition.loc.start ? [definition.loc.start] : undefined);
1619
+ }
1620
+ }
1621
+ }
1622
+ function countAliases(node, getFragmentByName) {
1623
+ let aliases = 0;
1624
+ if ('alias' in node && node.alias) {
1625
+ ++aliases;
1626
+ }
1627
+ if ('selectionSet' in node && node.selectionSet) {
1628
+ for (let child of node.selectionSet.selections) {
1629
+ aliases += countAliases(child, getFragmentByName);
1630
+ }
1631
+ }
1632
+ else if (node.kind === Kind.FRAGMENT_SPREAD) {
1633
+ const fragmentNode = getFragmentByName(node.name.value);
1634
+ if (fragmentNode) {
1635
+ aliases += countAliases(fragmentNode, getFragmentByName);
1636
+ }
1637
+ }
1638
+ return aliases;
1639
+ }
1640
+
1641
+ function validateDirectiveCount({ source, doc, maxDirectiveCount, fragmentGraph, }) {
1642
+ const getFragmentByFragmentName = (fragmentName) => fragmentGraph.getNodeData(fragmentName);
1643
+ for (const definition of doc.definitions) {
1644
+ if (definition.kind !== Kind.OPERATION_DEFINITION) {
1645
+ continue;
1646
+ }
1647
+ const directiveCount = countDirectives(definition, getFragmentByFragmentName);
1648
+ if (directiveCount > maxDirectiveCount) {
1649
+ return new GraphQLError(`Too many directives (${directiveCount}). Maximum allowed is ${maxDirectiveCount}`, [definition], source, definition.loc && definition.loc.start ? [definition.loc.start] : undefined);
1650
+ }
1651
+ }
1652
+ }
1653
+ function countDirectives(node, getFragmentByName) {
1654
+ let directives = 0;
1655
+ if (node.directives) {
1656
+ directives += node.directives.length;
1657
+ }
1658
+ if ('selectionSet' in node && node.selectionSet) {
1659
+ for (let child of node.selectionSet.selections) {
1660
+ directives += countDirectives(child, getFragmentByName);
1661
+ }
1662
+ }
1663
+ if (node.kind == Kind.FRAGMENT_SPREAD) {
1664
+ const fragment = getFragmentByName(node.name.value);
1665
+ if (fragment) {
1666
+ directives += countDirectives(fragment, getFragmentByName);
1667
+ }
1668
+ }
1669
+ return directives;
1670
+ }
1671
+
1605
1672
  function validate(schema, sources, options) {
1606
1673
  const config = Object.assign({ strictDeprecated: true, strictFragments: true, keepClientFields: false, apollo: false }, options);
1607
1674
  const invalidDocuments = [];
@@ -1611,43 +1678,41 @@ function validate(schema, sources, options) {
1611
1678
  const fragments = [];
1612
1679
  const fragmentNames = [];
1613
1680
  const graph = new DepGraph({ circular: true });
1614
- documents.forEach((doc) => {
1615
- doc.fragments.forEach((fragment) => {
1681
+ documents.forEach(doc => {
1682
+ doc.fragments.forEach(fragment => {
1616
1683
  fragmentNames.push(fragment.node.name.value);
1617
1684
  fragments.push(fragment);
1618
1685
  graph.addNode(fragment.node.name.value, fragment.node);
1619
1686
  });
1620
1687
  });
1621
- fragments.forEach((fragment) => {
1688
+ fragments.forEach(fragment => {
1622
1689
  const depends = extractFragments(print(fragment.node));
1623
1690
  if (depends) {
1624
- depends.forEach((name) => {
1691
+ depends.forEach(name => {
1625
1692
  graph.addDependency(fragment.node.name.value, name);
1626
1693
  });
1627
1694
  }
1628
1695
  });
1629
1696
  documents
1630
1697
  // since we include fragments, validate only operations
1631
- .filter((doc) => doc.hasOperations)
1632
- .forEach((doc) => {
1698
+ .filter(doc => doc.hasOperations)
1699
+ .forEach(doc => {
1633
1700
  const docWithOperations = {
1634
1701
  kind: Kind.DOCUMENT,
1635
- definitions: doc.operations.map((d) => d.node),
1702
+ definitions: doc.operations.map(d => d.node),
1636
1703
  };
1637
1704
  const extractedFragments = (extractFragments(print(docWithOperations)) || [])
1638
1705
  // resolve all nested fragments
1639
- .map((fragmentName) => resolveFragment(graph.getNodeData(fragmentName), graph))
1706
+ .map(fragmentName => resolveFragment(graph.getNodeData(fragmentName), graph))
1640
1707
  // flatten arrays
1641
1708
  .reduce((list, current) => list.concat(current), [])
1642
1709
  // remove duplicates
1643
- .filter((def, i, all) => all.findIndex((item) => item.name.value === def.name.value) === i);
1710
+ .filter((def, i, all) => all.findIndex(item => item.name.value === def.name.value) === i);
1644
1711
  const merged = {
1645
1712
  kind: Kind.DOCUMENT,
1646
1713
  definitions: [...docWithOperations.definitions, ...extractedFragments],
1647
1714
  };
1648
- let transformedSchema = config.apollo
1649
- ? transformSchemaWithApollo(schema)
1650
- : schema;
1715
+ let transformedSchema = config.apollo ? transformSchemaWithApollo(schema) : schema;
1651
1716
  const transformedDoc = config.apollo
1652
1717
  ? transformDocumentWithApollo(merged, {
1653
1718
  keepClientFields: config.keepClientFields,
@@ -1665,12 +1730,30 @@ function validate(schema, sources, options) {
1665
1730
  errors.push(depthError);
1666
1731
  }
1667
1732
  }
1668
- const deprecated = config.strictDeprecated
1669
- ? findDeprecatedUsages(transformedSchema, transformedDoc)
1670
- : [];
1671
- const duplicatedFragments = config.strictFragments
1672
- ? findDuplicatedFragments(fragmentNames)
1673
- : [];
1733
+ if (config.maxAliasCount) {
1734
+ const aliasError = validateAliasCount({
1735
+ source: doc.source,
1736
+ doc: transformedDoc,
1737
+ maxAliasCount: config.maxAliasCount,
1738
+ fragmentGraph: graph,
1739
+ });
1740
+ if (aliasError) {
1741
+ errors.push(aliasError);
1742
+ }
1743
+ }
1744
+ if (config.maxDirectiveCount) {
1745
+ const directiveError = validateDirectiveCount({
1746
+ source: doc.source,
1747
+ doc: transformedDoc,
1748
+ maxDirectiveCount: config.maxDirectiveCount,
1749
+ fragmentGraph: graph,
1750
+ });
1751
+ if (directiveError) {
1752
+ errors.push(directiveError);
1753
+ }
1754
+ }
1755
+ const deprecated = config.strictDeprecated ? findDeprecatedUsages(transformedSchema, transformedDoc) : [];
1756
+ const duplicatedFragments = config.strictFragments ? findDuplicatedFragments(fragmentNames) : [];
1674
1757
  if (sumLengths(errors, duplicatedFragments, deprecated) > 0) {
1675
1758
  invalidDocuments.push({
1676
1759
  source: doc.source,
@@ -1684,7 +1767,7 @@ function validate(schema, sources, options) {
1684
1767
  function findDuplicatedFragments(fragmentNames) {
1685
1768
  return fragmentNames
1686
1769
  .filter((name, i, all) => all.indexOf(name) !== i)
1687
- .map((name) => new GraphQLError(`Name of '${name}' fragment is not unique`));
1770
+ .map(name => new GraphQLError(`Name of '${name}' fragment is not unique`));
1688
1771
  }
1689
1772
  //
1690
1773
  // PostInfo -> AuthorInfo
@@ -1693,13 +1776,10 @@ function findDuplicatedFragments(fragmentNames) {
1693
1776
  function resolveFragment(fragment, graph) {
1694
1777
  return graph
1695
1778
  .dependenciesOf(fragment.name.value)
1696
- .reduce((list, current) => [
1697
- ...list,
1698
- ...resolveFragment(graph.getNodeData(current), graph),
1699
- ], [fragment]);
1779
+ .reduce((list, current) => [...list, ...resolveFragment(graph.getNodeData(current), graph)], [fragment]);
1700
1780
  }
1701
1781
  function extractFragments(document) {
1702
- return (document.match(/[\.]{3}[a-z0-9\_]+\b/gi) || []).map((name) => name.replace('...', ''));
1782
+ return (document.match(/[\.]{3}[a-z0-9\_]+\b/gi) || []).map(name => name.replace('...', ''));
1703
1783
  }
1704
1784
  function sumLengths(...arrays) {
1705
1785
  return arrays.reduce((sum, { length }) => sum + length, 0);
@@ -1708,20 +1788,19 @@ function sumLengths(...arrays) {
1708
1788
  function similar(schema, typeName, threshold = 0.4) {
1709
1789
  const typeMap = schema.getTypeMap();
1710
1790
  const targets = Object.keys(schema.getTypeMap())
1711
- .filter((name) => !isPrimitive(name) && !isForIntrospection(name))
1712
- .map((name) => ({
1791
+ .filter(name => !isPrimitive(name) && !isForIntrospection(name))
1792
+ .map(name => ({
1713
1793
  typeId: name,
1714
1794
  value: stripType(typeMap[name]),
1715
1795
  }));
1716
1796
  const results = {};
1717
- if (typeof typeName !== 'undefined' &&
1718
- !targets.some((t) => t.typeId === typeName)) {
1797
+ if (typeof typeName !== 'undefined' && !targets.some(t => t.typeId === typeName)) {
1719
1798
  throw new Error(`Type '${typeName}' doesn't exist`);
1720
1799
  }
1721
- (typeName ? [{ typeId: typeName, value: '' }] : targets).forEach((source) => {
1800
+ (typeName ? [{ typeId: typeName, value: '' }] : targets).forEach(source => {
1722
1801
  const sourceType = schema.getType(source.typeId);
1723
- const matchWith = targets.filter((target) => schema.getType(target.typeId).astNode.kind ===
1724
- sourceType.astNode.kind && target.typeId !== source.typeId);
1802
+ const matchWith = targets.filter(target => schema.getType(target.typeId).astNode.kind === sourceType.astNode.kind &&
1803
+ target.typeId !== source.typeId);
1725
1804
  if (matchWith.length > 0) {
1726
1805
  const found = similarTo(sourceType, matchWith, threshold);
1727
1806
  if (found) {
@@ -1732,7 +1811,7 @@ function similar(schema, typeName, threshold = 0.4) {
1732
1811
  return results;
1733
1812
  }
1734
1813
  function similarTo(type, targets, threshold) {
1735
- const types = targets.filter((target) => target.typeId !== type.name);
1814
+ const types = targets.filter(target => target.typeId !== type.name);
1736
1815
  const result = findBestMatch(stripType(type), types);
1737
1816
  if (result.bestMatch.rating < threshold) {
1738
1817
  return;
@@ -1740,7 +1819,7 @@ function similarTo(type, targets, threshold) {
1740
1819
  return {
1741
1820
  bestMatch: result.bestMatch,
1742
1821
  ratings: result.ratings
1743
- .filter((r) => r.rating >= threshold && r.target !== result.bestMatch.target)
1822
+ .filter(r => r.rating >= threshold && r.target !== result.bestMatch.target)
1744
1823
  .sort((a, b) => a.rating - b.rating)
1745
1824
  .reverse(),
1746
1825
  };
@@ -1752,7 +1831,7 @@ function stripType(type) {
1752
1831
  .replace(/\}$/g, '')
1753
1832
  .trim()
1754
1833
  .split('\n')
1755
- .map((s) => s.trim())
1834
+ .map(s => s.trim())
1756
1835
  .sort((a, b) => a.localeCompare(b))
1757
1836
  .join(' ');
1758
1837
  }
@@ -1764,7 +1843,7 @@ function coverage(schema, sources) {
1764
1843
  };
1765
1844
  const typeMap = schema.getTypeMap();
1766
1845
  const typeInfo = new TypeInfo(schema);
1767
- const visitor = (source) => ({
1846
+ const visitor = source => ({
1768
1847
  Field(node) {
1769
1848
  const fieldDef = typeInfo.getFieldDef();
1770
1849
  const parent = typeInfo.getParentType();
@@ -1782,20 +1861,14 @@ function coverage(schema, sources) {
1782
1861
  typeCoverage.hits++;
1783
1862
  fieldCoverage.hits++;
1784
1863
  if (node.loc) {
1785
- fieldCoverage.locations[sourceName] = [
1786
- node.loc,
1787
- ...(locations || []),
1788
- ];
1864
+ fieldCoverage.locations[sourceName] = [node.loc, ...(locations || [])];
1789
1865
  }
1790
1866
  if (node.arguments) {
1791
1867
  for (const argNode of node.arguments) {
1792
1868
  const argCoverage = fieldCoverage.children[argNode.name.value];
1793
1869
  argCoverage.hits++;
1794
1870
  if (argNode.loc) {
1795
- argCoverage.locations[sourceName] = [
1796
- argNode.loc,
1797
- ...(argCoverage.locations[sourceName] || []),
1798
- ];
1871
+ argCoverage.locations[sourceName] = [argNode.loc, ...(argCoverage.locations[sourceName] || [])];
1799
1872
  }
1800
1873
  }
1801
1874
  }
@@ -1833,14 +1906,73 @@ function coverage(schema, sources) {
1833
1906
  const documents = coverage.sources.map(readDocument);
1834
1907
  documents.forEach((doc, i) => {
1835
1908
  const source = coverage.sources[i];
1836
- doc.operations.forEach((op) => {
1909
+ doc.operations.forEach(op => {
1837
1910
  visit(op.node, visitWithTypeInfo(typeInfo, visitor(source)));
1838
1911
  });
1839
- doc.fragments.forEach((fr) => {
1912
+ doc.fragments.forEach(fr => {
1840
1913
  visit(fr.node, visitWithTypeInfo(typeInfo, visitor(source)));
1841
1914
  });
1842
1915
  });
1843
1916
  return coverage;
1844
1917
  }
1845
1918
 
1846
- export { ChangeType, CriticalityLevel, DiffRule, coverage, diff, getTypePrefix, similar, validate };
1919
+ function calculateOperationComplexity(node, config, getFragmentByName, depth = 0) {
1920
+ let cost = config.scalarCost;
1921
+ if ('selectionSet' in node && node.selectionSet) {
1922
+ cost = config.objectCost;
1923
+ for (let child of node.selectionSet.selections) {
1924
+ cost += config.depthCostFactor * calculateOperationComplexity(child, config, getFragmentByName, depth + 1);
1925
+ }
1926
+ }
1927
+ if (node.kind == Kind.FRAGMENT_SPREAD) {
1928
+ const fragment = getFragmentByName(node.name.value);
1929
+ if (fragment) {
1930
+ cost += config.depthCostFactor * calculateOperationComplexity(fragment, config, getFragmentByName, depth + 1);
1931
+ }
1932
+ }
1933
+ return cost;
1934
+ }
1935
+
1936
+ class ParserWithLexer extends Parser {
1937
+ constructor(source, options) {
1938
+ super(source, options);
1939
+ this.__tokenCount = 0;
1940
+ const lexer = this._lexer;
1941
+ this._lexer = new Proxy(lexer, {
1942
+ get: (target, prop, receiver) => {
1943
+ if (prop === 'advance') {
1944
+ return () => {
1945
+ const token = target.advance();
1946
+ if (token.kind !== TokenKind.EOF) {
1947
+ this.__tokenCount++;
1948
+ }
1949
+ return token;
1950
+ };
1951
+ }
1952
+ return Reflect.get(target, prop, receiver);
1953
+ },
1954
+ });
1955
+ }
1956
+ get tokenCount() {
1957
+ return this.__tokenCount;
1958
+ }
1959
+ }
1960
+ function calculateTokenCount(args) {
1961
+ const parser = new ParserWithLexer(args.source);
1962
+ const document = parser.parseDocument();
1963
+ let { tokenCount } = parser;
1964
+ visit(document, {
1965
+ FragmentSpread(node) {
1966
+ const fragmentSource = args.getReferencedFragmentSource(node.name.value);
1967
+ if (fragmentSource) {
1968
+ tokenCount += calculateTokenCount({
1969
+ source: fragmentSource,
1970
+ getReferencedFragmentSource: args.getReferencedFragmentSource,
1971
+ });
1972
+ }
1973
+ },
1974
+ });
1975
+ return tokenCount;
1976
+ }
1977
+
1978
+ export { ChangeType, CriticalityLevel, DiffRule, calculateOperationComplexity, calculateTokenCount, countAliases, countDepth, countDirectives, coverage, diff, getTypePrefix, similar, validate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-inspector/core",
3
- "version": "0.0.0-canary.e88122e",
3
+ "version": "0.0.0-canary.fec2df4",
4
4
  "description": "Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {