KnownDirectivesRule.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.KnownDirectivesRule = KnownDirectivesRule;
  6. var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
  7. var _invariant = _interopRequireDefault(require("../../jsutils/invariant.js"));
  8. var _GraphQLError = require("../../error/GraphQLError.js");
  9. var _kinds = require("../../language/kinds.js");
  10. var _directiveLocation = require("../../language/directiveLocation.js");
  11. var _directives = require("../../type/directives.js");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * Known directives
  15. *
  16. * A GraphQL document is only valid if all `@directives` are known by the
  17. * schema and legally positioned.
  18. */
  19. function KnownDirectivesRule(context) {
  20. var locationsMap = Object.create(null);
  21. var schema = context.getSchema();
  22. var definedDirectives = schema ? schema.getDirectives() : _directives.specifiedDirectives;
  23. for (var _i2 = 0; _i2 < definedDirectives.length; _i2++) {
  24. var directive = definedDirectives[_i2];
  25. locationsMap[directive.name] = directive.locations;
  26. }
  27. var astDefinitions = context.getDocument().definitions;
  28. for (var _i4 = 0; _i4 < astDefinitions.length; _i4++) {
  29. var def = astDefinitions[_i4];
  30. if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  31. locationsMap[def.name.value] = def.locations.map(function (name) {
  32. return name.value;
  33. });
  34. }
  35. }
  36. return {
  37. Directive: function Directive(node, _key, _parent, _path, ancestors) {
  38. var name = node.name.value;
  39. var locations = locationsMap[name];
  40. if (!locations) {
  41. context.reportError(new _GraphQLError.GraphQLError("Unknown directive \"@".concat(name, "\"."), node));
  42. return;
  43. }
  44. var candidateLocation = getDirectiveLocationForASTPath(ancestors);
  45. if (candidateLocation && locations.indexOf(candidateLocation) === -1) {
  46. context.reportError(new _GraphQLError.GraphQLError("Directive \"@".concat(name, "\" may not be used on ").concat(candidateLocation, "."), node));
  47. }
  48. }
  49. };
  50. }
  51. function getDirectiveLocationForASTPath(ancestors) {
  52. var appliedTo = ancestors[ancestors.length - 1];
  53. !Array.isArray(appliedTo) || (0, _invariant.default)(0);
  54. switch (appliedTo.kind) {
  55. case _kinds.Kind.OPERATION_DEFINITION:
  56. return getDirectiveLocationForOperation(appliedTo.operation);
  57. case _kinds.Kind.FIELD:
  58. return _directiveLocation.DirectiveLocation.FIELD;
  59. case _kinds.Kind.FRAGMENT_SPREAD:
  60. return _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD;
  61. case _kinds.Kind.INLINE_FRAGMENT:
  62. return _directiveLocation.DirectiveLocation.INLINE_FRAGMENT;
  63. case _kinds.Kind.FRAGMENT_DEFINITION:
  64. return _directiveLocation.DirectiveLocation.FRAGMENT_DEFINITION;
  65. case _kinds.Kind.VARIABLE_DEFINITION:
  66. return _directiveLocation.DirectiveLocation.VARIABLE_DEFINITION;
  67. case _kinds.Kind.SCHEMA_DEFINITION:
  68. case _kinds.Kind.SCHEMA_EXTENSION:
  69. return _directiveLocation.DirectiveLocation.SCHEMA;
  70. case _kinds.Kind.SCALAR_TYPE_DEFINITION:
  71. case _kinds.Kind.SCALAR_TYPE_EXTENSION:
  72. return _directiveLocation.DirectiveLocation.SCALAR;
  73. case _kinds.Kind.OBJECT_TYPE_DEFINITION:
  74. case _kinds.Kind.OBJECT_TYPE_EXTENSION:
  75. return _directiveLocation.DirectiveLocation.OBJECT;
  76. case _kinds.Kind.FIELD_DEFINITION:
  77. return _directiveLocation.DirectiveLocation.FIELD_DEFINITION;
  78. case _kinds.Kind.INTERFACE_TYPE_DEFINITION:
  79. case _kinds.Kind.INTERFACE_TYPE_EXTENSION:
  80. return _directiveLocation.DirectiveLocation.INTERFACE;
  81. case _kinds.Kind.UNION_TYPE_DEFINITION:
  82. case _kinds.Kind.UNION_TYPE_EXTENSION:
  83. return _directiveLocation.DirectiveLocation.UNION;
  84. case _kinds.Kind.ENUM_TYPE_DEFINITION:
  85. case _kinds.Kind.ENUM_TYPE_EXTENSION:
  86. return _directiveLocation.DirectiveLocation.ENUM;
  87. case _kinds.Kind.ENUM_VALUE_DEFINITION:
  88. return _directiveLocation.DirectiveLocation.ENUM_VALUE;
  89. case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION:
  90. case _kinds.Kind.INPUT_OBJECT_TYPE_EXTENSION:
  91. return _directiveLocation.DirectiveLocation.INPUT_OBJECT;
  92. case _kinds.Kind.INPUT_VALUE_DEFINITION:
  93. {
  94. var parentNode = ancestors[ancestors.length - 3];
  95. return parentNode.kind === _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION ? _directiveLocation.DirectiveLocation.INPUT_FIELD_DEFINITION : _directiveLocation.DirectiveLocation.ARGUMENT_DEFINITION;
  96. }
  97. }
  98. }
  99. function getDirectiveLocationForOperation(operation) {
  100. switch (operation) {
  101. case 'query':
  102. return _directiveLocation.DirectiveLocation.QUERY;
  103. case 'mutation':
  104. return _directiveLocation.DirectiveLocation.MUTATION;
  105. case 'subscription':
  106. return _directiveLocation.DirectiveLocation.SUBSCRIPTION;
  107. } // istanbul ignore next (Not reachable. All possible types have been considered)
  108. false || (0, _invariant.default)(0, 'Unexpected operation: ' + (0, _inspect.default)(operation));
  109. }