KnownDirectivesRule.mjs 4.1 KB

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