KnownArgumentNamesRule.mjs 3.0 KB

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