KnownArgumentNamesRule.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.KnownArgumentNamesRule = KnownArgumentNamesRule;
  6. exports.KnownArgumentNamesOnDirectivesRule = KnownArgumentNamesOnDirectivesRule;
  7. var _didYouMean = _interopRequireDefault(require("../../jsutils/didYouMean.js"));
  8. var _suggestionList = _interopRequireDefault(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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. 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; }
  14. 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; }
  15. 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; }
  16. /**
  17. * Known argument names
  18. *
  19. * A GraphQL field is only valid if all supplied arguments are defined by
  20. * that field.
  21. */
  22. function KnownArgumentNamesRule(context) {
  23. return _objectSpread(_objectSpread({}, KnownArgumentNamesOnDirectivesRule(context)), {}, {
  24. Argument: function Argument(argNode) {
  25. var argDef = context.getArgument();
  26. var fieldDef = context.getFieldDef();
  27. var parentType = context.getParentType();
  28. if (!argDef && fieldDef && parentType) {
  29. var argName = argNode.name.value;
  30. var knownArgsNames = fieldDef.args.map(function (arg) {
  31. return arg.name;
  32. });
  33. var suggestions = (0, _suggestionList.default)(argName, knownArgsNames);
  34. context.reportError(new _GraphQLError.GraphQLError("Unknown argument \"".concat(argName, "\" on field \"").concat(parentType.name, ".").concat(fieldDef.name, "\".") + (0, _didYouMean.default)(suggestions), argNode));
  35. }
  36. }
  37. });
  38. }
  39. /**
  40. * @internal
  41. */
  42. function KnownArgumentNamesOnDirectivesRule(context) {
  43. var directiveArgs = Object.create(null);
  44. var schema = context.getSchema();
  45. var definedDirectives = schema ? schema.getDirectives() : _directives.specifiedDirectives;
  46. for (var _i2 = 0; _i2 < definedDirectives.length; _i2++) {
  47. var directive = definedDirectives[_i2];
  48. directiveArgs[directive.name] = directive.args.map(function (arg) {
  49. return arg.name;
  50. });
  51. }
  52. var astDefinitions = context.getDocument().definitions;
  53. for (var _i4 = 0; _i4 < astDefinitions.length; _i4++) {
  54. var def = astDefinitions[_i4];
  55. if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  56. var _def$arguments;
  57. // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  58. var argsNodes = (_def$arguments = def.arguments) !== null && _def$arguments !== void 0 ? _def$arguments : [];
  59. directiveArgs[def.name.value] = argsNodes.map(function (arg) {
  60. return arg.name.value;
  61. });
  62. }
  63. }
  64. return {
  65. Directive: function Directive(directiveNode) {
  66. var directiveName = directiveNode.name.value;
  67. var knownArgs = directiveArgs[directiveName];
  68. if (directiveNode.arguments && knownArgs) {
  69. for (var _i6 = 0, _directiveNode$argume2 = directiveNode.arguments; _i6 < _directiveNode$argume2.length; _i6++) {
  70. var argNode = _directiveNode$argume2[_i6];
  71. var argName = argNode.name.value;
  72. if (knownArgs.indexOf(argName) === -1) {
  73. var suggestions = (0, _suggestionList.default)(argName, knownArgs);
  74. context.reportError(new _GraphQLError.GraphQLError("Unknown argument \"".concat(argName, "\" on directive \"@").concat(directiveName, "\".") + (0, _didYouMean.default)(suggestions), argNode));
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. };
  81. }