UniqueDirectivesPerLocationRule.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * See https://spec.graphql.org/draft/#sec-Directives-Are-Unique-Per-Location
  17. */
  18. function UniqueDirectivesPerLocationRule(context) {
  19. const uniqueDirectiveMap = Object.create(null);
  20. const schema = context.getSchema();
  21. const definedDirectives = schema
  22. ? schema.getDirectives()
  23. : _directives.specifiedDirectives;
  24. for (const directive of definedDirectives) {
  25. uniqueDirectiveMap[directive.name] = !directive.isRepeatable;
  26. }
  27. const astDefinitions = context.getDocument().definitions;
  28. for (const def of astDefinitions) {
  29. if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  30. uniqueDirectiveMap[def.name.value] = !def.repeatable;
  31. }
  32. }
  33. const schemaDirectives = Object.create(null);
  34. const typeDirectivesMap = Object.create(null);
  35. return {
  36. // Many different AST nodes may contain directives. Rather than listing
  37. // them all, just listen for entering any node, and check to see if it
  38. // defines any directives.
  39. enter(node) {
  40. if (!('directives' in node) || !node.directives) {
  41. return;
  42. }
  43. let seenDirectives;
  44. if (
  45. node.kind === _kinds.Kind.SCHEMA_DEFINITION ||
  46. node.kind === _kinds.Kind.SCHEMA_EXTENSION
  47. ) {
  48. seenDirectives = schemaDirectives;
  49. } else if (
  50. (0, _predicates.isTypeDefinitionNode)(node) ||
  51. (0, _predicates.isTypeExtensionNode)(node)
  52. ) {
  53. const typeName = node.name.value;
  54. seenDirectives = typeDirectivesMap[typeName];
  55. if (seenDirectives === undefined) {
  56. typeDirectivesMap[typeName] = seenDirectives = Object.create(null);
  57. }
  58. } else {
  59. seenDirectives = Object.create(null);
  60. }
  61. for (const directive of node.directives) {
  62. const directiveName = directive.name.value;
  63. if (uniqueDirectiveMap[directiveName]) {
  64. if (seenDirectives[directiveName]) {
  65. context.reportError(
  66. new _GraphQLError.GraphQLError(
  67. `The directive "@${directiveName}" can only be used once at this location.`,
  68. {
  69. nodes: [seenDirectives[directiveName], directive],
  70. },
  71. ),
  72. );
  73. } else {
  74. seenDirectives[directiveName] = directive;
  75. }
  76. }
  77. }
  78. },
  79. };
  80. }