KnownDirectives.mjs 4.1 KB

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