KnownArgumentNamesRule.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.KnownArgumentNamesOnDirectivesRule = KnownArgumentNamesOnDirectivesRule;
  6. exports.KnownArgumentNamesRule = KnownArgumentNamesRule;
  7. var _didYouMean = require('../../jsutils/didYouMean.js');
  8. var _suggestionList = require('../../jsutils/suggestionList.js');
  9. var _GraphQLError = require('../../error/GraphQLError.js');
  10. var _kinds = require('../../language/kinds.js');
  11. var _directives = require('../../type/directives.js');
  12. /**
  13. * Known argument names
  14. *
  15. * A GraphQL field is only valid if all supplied arguments are defined by
  16. * that field.
  17. *
  18. * See https://spec.graphql.org/draft/#sec-Argument-Names
  19. * See https://spec.graphql.org/draft/#sec-Directives-Are-In-Valid-Locations
  20. */
  21. function KnownArgumentNamesRule(context) {
  22. return {
  23. // eslint-disable-next-line new-cap
  24. ...KnownArgumentNamesOnDirectivesRule(context),
  25. Argument(argNode) {
  26. const argDef = context.getArgument();
  27. const fieldDef = context.getFieldDef();
  28. const parentType = context.getParentType();
  29. if (!argDef && fieldDef && parentType) {
  30. const argName = argNode.name.value;
  31. const knownArgsNames = fieldDef.args.map((arg) => arg.name);
  32. const suggestions = (0, _suggestionList.suggestionList)(
  33. argName,
  34. knownArgsNames,
  35. );
  36. context.reportError(
  37. new _GraphQLError.GraphQLError(
  38. `Unknown argument "${argName}" on field "${parentType.name}.${fieldDef.name}".` +
  39. (0, _didYouMean.didYouMean)(suggestions),
  40. {
  41. nodes: argNode,
  42. },
  43. ),
  44. );
  45. }
  46. },
  47. };
  48. }
  49. /**
  50. * @internal
  51. */
  52. function KnownArgumentNamesOnDirectivesRule(context) {
  53. const directiveArgs = Object.create(null);
  54. const schema = context.getSchema();
  55. const definedDirectives = schema
  56. ? schema.getDirectives()
  57. : _directives.specifiedDirectives;
  58. for (const directive of definedDirectives) {
  59. directiveArgs[directive.name] = directive.args.map((arg) => arg.name);
  60. }
  61. const astDefinitions = context.getDocument().definitions;
  62. for (const def of astDefinitions) {
  63. if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  64. var _def$arguments;
  65. // FIXME: https://github.com/graphql/graphql-js/issues/2203
  66. /* c8 ignore next */
  67. const argsNodes =
  68. (_def$arguments = def.arguments) !== null && _def$arguments !== void 0
  69. ? _def$arguments
  70. : [];
  71. directiveArgs[def.name.value] = argsNodes.map((arg) => arg.name.value);
  72. }
  73. }
  74. return {
  75. Directive(directiveNode) {
  76. const directiveName = directiveNode.name.value;
  77. const knownArgs = directiveArgs[directiveName];
  78. if (directiveNode.arguments && knownArgs) {
  79. for (const argNode of directiveNode.arguments) {
  80. const argName = argNode.name.value;
  81. if (!knownArgs.includes(argName)) {
  82. const suggestions = (0, _suggestionList.suggestionList)(
  83. argName,
  84. knownArgs,
  85. );
  86. context.reportError(
  87. new _GraphQLError.GraphQLError(
  88. `Unknown argument "${argName}" on directive "@${directiveName}".` +
  89. (0, _didYouMean.didYouMean)(suggestions),
  90. {
  91. nodes: argNode,
  92. },
  93. ),
  94. );
  95. }
  96. }
  97. }
  98. return false;
  99. },
  100. };
  101. }