KnownArgumentNamesRule.mjs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  2. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  4. import didYouMean from "../../jsutils/didYouMean.mjs";
  5. import suggestionList from "../../jsutils/suggestionList.mjs";
  6. import { GraphQLError } from "../../error/GraphQLError.mjs";
  7. import { Kind } from "../../language/kinds.mjs";
  8. import { specifiedDirectives } from "../../type/directives.mjs";
  9. /**
  10. * Known argument names
  11. *
  12. * A GraphQL field is only valid if all supplied arguments are defined by
  13. * that field.
  14. */
  15. export function KnownArgumentNamesRule(context) {
  16. return _objectSpread(_objectSpread({}, KnownArgumentNamesOnDirectivesRule(context)), {}, {
  17. Argument: function Argument(argNode) {
  18. var argDef = context.getArgument();
  19. var fieldDef = context.getFieldDef();
  20. var parentType = context.getParentType();
  21. if (!argDef && fieldDef && parentType) {
  22. var argName = argNode.name.value;
  23. var knownArgsNames = fieldDef.args.map(function (arg) {
  24. return arg.name;
  25. });
  26. var suggestions = suggestionList(argName, knownArgsNames);
  27. context.reportError(new GraphQLError("Unknown argument \"".concat(argName, "\" on field \"").concat(parentType.name, ".").concat(fieldDef.name, "\".") + didYouMean(suggestions), argNode));
  28. }
  29. }
  30. });
  31. }
  32. /**
  33. * @internal
  34. */
  35. export function KnownArgumentNamesOnDirectivesRule(context) {
  36. var directiveArgs = Object.create(null);
  37. var schema = context.getSchema();
  38. var definedDirectives = schema ? schema.getDirectives() : specifiedDirectives;
  39. for (var _i2 = 0; _i2 < definedDirectives.length; _i2++) {
  40. var directive = definedDirectives[_i2];
  41. directiveArgs[directive.name] = directive.args.map(function (arg) {
  42. return arg.name;
  43. });
  44. }
  45. var astDefinitions = context.getDocument().definitions;
  46. for (var _i4 = 0; _i4 < astDefinitions.length; _i4++) {
  47. var def = astDefinitions[_i4];
  48. if (def.kind === Kind.DIRECTIVE_DEFINITION) {
  49. var _def$arguments;
  50. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  51. var argsNodes = (_def$arguments = def.arguments) !== null && _def$arguments !== void 0 ? _def$arguments : [];
  52. directiveArgs[def.name.value] = argsNodes.map(function (arg) {
  53. return arg.name.value;
  54. });
  55. }
  56. }
  57. return {
  58. Directive: function Directive(directiveNode) {
  59. var directiveName = directiveNode.name.value;
  60. var knownArgs = directiveArgs[directiveName];
  61. if (directiveNode.arguments && knownArgs) {
  62. for (var _i6 = 0, _directiveNode$argume2 = directiveNode.arguments; _i6 < _directiveNode$argume2.length; _i6++) {
  63. var argNode = _directiveNode$argume2[_i6];
  64. var argName = argNode.name.value;
  65. if (knownArgs.indexOf(argName) === -1) {
  66. var suggestions = suggestionList(argName, knownArgs);
  67. context.reportError(new GraphQLError("Unknown argument \"".concat(argName, "\" on directive \"@").concat(directiveName, "\".") + didYouMean(suggestions), argNode));
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. };
  74. }