KnownDirectives.js 4.8 KB

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