UniqueDirectivesPerLocationRule.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.UniqueDirectivesPerLocationRule = UniqueDirectivesPerLocationRule;
  6. var _GraphQLError = require("../../error/GraphQLError.js");
  7. var _kinds = require("../../language/kinds.js");
  8. var _predicates = require("../../language/predicates.js");
  9. var _directives = require("../../type/directives.js");
  10. /**
  11. * Unique directive names per location
  12. *
  13. * A GraphQL document is only valid if all non-repeatable directives at
  14. * a given location are uniquely named.
  15. */
  16. function UniqueDirectivesPerLocationRule(context) {
  17. var uniqueDirectiveMap = Object.create(null);
  18. var schema = context.getSchema();
  19. var definedDirectives = schema ? schema.getDirectives() : _directives.specifiedDirectives;
  20. for (var _i2 = 0; _i2 < definedDirectives.length; _i2++) {
  21. var directive = definedDirectives[_i2];
  22. uniqueDirectiveMap[directive.name] = !directive.isRepeatable;
  23. }
  24. var astDefinitions = context.getDocument().definitions;
  25. for (var _i4 = 0; _i4 < astDefinitions.length; _i4++) {
  26. var def = astDefinitions[_i4];
  27. if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  28. uniqueDirectiveMap[def.name.value] = !def.repeatable;
  29. }
  30. }
  31. var schemaDirectives = Object.create(null);
  32. var typeDirectivesMap = Object.create(null);
  33. return {
  34. // Many different AST nodes may contain directives. Rather than listing
  35. // them all, just listen for entering any node, and check to see if it
  36. // defines any directives.
  37. enter: function enter(node) {
  38. if (node.directives == null) {
  39. return;
  40. }
  41. var seenDirectives;
  42. if (node.kind === _kinds.Kind.SCHEMA_DEFINITION || node.kind === _kinds.Kind.SCHEMA_EXTENSION) {
  43. seenDirectives = schemaDirectives;
  44. } else if ((0, _predicates.isTypeDefinitionNode)(node) || (0, _predicates.isTypeExtensionNode)(node)) {
  45. var typeName = node.name.value;
  46. seenDirectives = typeDirectivesMap[typeName];
  47. if (seenDirectives === undefined) {
  48. typeDirectivesMap[typeName] = seenDirectives = Object.create(null);
  49. }
  50. } else {
  51. seenDirectives = Object.create(null);
  52. }
  53. for (var _i6 = 0, _node$directives2 = node.directives; _i6 < _node$directives2.length; _i6++) {
  54. var _directive = _node$directives2[_i6];
  55. var directiveName = _directive.name.value;
  56. if (uniqueDirectiveMap[directiveName]) {
  57. if (seenDirectives[directiveName]) {
  58. context.reportError(new _GraphQLError.GraphQLError("The directive \"@".concat(directiveName, "\" can only be used once at this location."), [seenDirectives[directiveName], _directive]));
  59. } else {
  60. seenDirectives[directiveName] = _directive;
  61. }
  62. }
  63. }
  64. }
  65. };
  66. }