KnownArgumentNamesRule.js.flow 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // @flow strict
  2. import didYouMean from '../../jsutils/didYouMean';
  3. import suggestionList from '../../jsutils/suggestionList';
  4. import { GraphQLError } from '../../error/GraphQLError';
  5. import type { ASTVisitor } from '../../language/visitor';
  6. import { Kind } from '../../language/kinds';
  7. import { specifiedDirectives } from '../../type/directives';
  8. import type {
  9. ValidationContext,
  10. SDLValidationContext,
  11. } from '../ValidationContext';
  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. export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor {
  19. return {
  20. ...KnownArgumentNamesOnDirectivesRule(context),
  21. Argument(argNode) {
  22. const argDef = context.getArgument();
  23. const fieldDef = context.getFieldDef();
  24. const parentType = context.getParentType();
  25. if (!argDef && fieldDef && parentType) {
  26. const argName = argNode.name.value;
  27. const knownArgsNames = fieldDef.args.map((arg) => arg.name);
  28. const suggestions = suggestionList(argName, knownArgsNames);
  29. context.reportError(
  30. new GraphQLError(
  31. `Unknown argument "${argName}" on field "${parentType.name}.${fieldDef.name}".` +
  32. didYouMean(suggestions),
  33. argNode,
  34. ),
  35. );
  36. }
  37. },
  38. };
  39. }
  40. /**
  41. * @internal
  42. */
  43. export function KnownArgumentNamesOnDirectivesRule(
  44. context: ValidationContext | SDLValidationContext,
  45. ): ASTVisitor {
  46. const directiveArgs = Object.create(null);
  47. const schema = context.getSchema();
  48. const definedDirectives = schema
  49. ? schema.getDirectives()
  50. : specifiedDirectives;
  51. for (const directive of definedDirectives) {
  52. directiveArgs[directive.name] = directive.args.map((arg) => arg.name);
  53. }
  54. const astDefinitions = context.getDocument().definitions;
  55. for (const def of astDefinitions) {
  56. if (def.kind === Kind.DIRECTIVE_DEFINITION) {
  57. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  58. const argsNodes = def.arguments ?? [];
  59. directiveArgs[def.name.value] = argsNodes.map((arg) => arg.name.value);
  60. }
  61. }
  62. return {
  63. Directive(directiveNode) {
  64. const directiveName = directiveNode.name.value;
  65. const knownArgs = directiveArgs[directiveName];
  66. if (directiveNode.arguments && knownArgs) {
  67. for (const argNode of directiveNode.arguments) {
  68. const argName = argNode.name.value;
  69. if (knownArgs.indexOf(argName) === -1) {
  70. const suggestions = suggestionList(argName, knownArgs);
  71. context.reportError(
  72. new GraphQLError(
  73. `Unknown argument "${argName}" on directive "@${directiveName}".` +
  74. didYouMean(suggestions),
  75. argNode,
  76. ),
  77. );
  78. }
  79. }
  80. }
  81. return false;
  82. },
  83. };
  84. }