KnownArgumentNames.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.unknownArgMessage = unknownArgMessage;
  6. exports.unknownDirectiveArgMessage = unknownDirectiveArgMessage;
  7. exports.KnownArgumentNames = KnownArgumentNames;
  8. exports.KnownArgumentNamesOnDirectives = KnownArgumentNamesOnDirectives;
  9. var _didYouMean = _interopRequireDefault(require("../../jsutils/didYouMean"));
  10. var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList"));
  11. var _GraphQLError = require("../../error/GraphQLError");
  12. var _kinds = require("../../language/kinds");
  13. var _directives = require("../../type/directives");
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. 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; }
  16. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  17. 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; }
  18. function unknownArgMessage(argName, fieldName, typeName, suggestedArgs) {
  19. return "Unknown argument \"".concat(argName, "\" on field \"").concat(fieldName, "\" of type \"").concat(typeName, "\".") + (0, _didYouMean.default)(suggestedArgs.map(function (x) {
  20. return "\"".concat(x, "\"");
  21. }));
  22. }
  23. function unknownDirectiveArgMessage(argName, directiveName, suggestedArgs) {
  24. return "Unknown argument \"".concat(argName, "\" on directive \"@").concat(directiveName, "\".") + (0, _didYouMean.default)(suggestedArgs.map(function (x) {
  25. return "\"".concat(x, "\"");
  26. }));
  27. }
  28. /**
  29. * Known argument names
  30. *
  31. * A GraphQL field is only valid if all supplied arguments are defined by
  32. * that field.
  33. */
  34. function KnownArgumentNames(context) {
  35. return _objectSpread({}, KnownArgumentNamesOnDirectives(context), {
  36. Argument: function Argument(argNode) {
  37. var argDef = context.getArgument();
  38. var fieldDef = context.getFieldDef();
  39. var parentType = context.getParentType();
  40. if (!argDef && fieldDef && parentType) {
  41. var argName = argNode.name.value;
  42. var knownArgsNames = fieldDef.args.map(function (arg) {
  43. return arg.name;
  44. });
  45. context.reportError(new _GraphQLError.GraphQLError(unknownArgMessage(argName, fieldDef.name, parentType.name, (0, _suggestionList.default)(argName, knownArgsNames)), argNode));
  46. }
  47. }
  48. });
  49. } // @internal
  50. function KnownArgumentNamesOnDirectives(context) {
  51. var directiveArgs = Object.create(null);
  52. var schema = context.getSchema();
  53. var definedDirectives = schema ? schema.getDirectives() : _directives.specifiedDirectives;
  54. for (var _i2 = 0; _i2 < definedDirectives.length; _i2++) {
  55. var directive = definedDirectives[_i2];
  56. directiveArgs[directive.name] = directive.args.map(function (arg) {
  57. return arg.name;
  58. });
  59. }
  60. var astDefinitions = context.getDocument().definitions;
  61. for (var _i4 = 0; _i4 < astDefinitions.length; _i4++) {
  62. var def = astDefinitions[_i4];
  63. if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  64. directiveArgs[def.name.value] = def.arguments ? def.arguments.map(function (arg) {
  65. return arg.name.value;
  66. }) : [];
  67. }
  68. }
  69. return {
  70. Directive: function Directive(directiveNode) {
  71. var directiveName = directiveNode.name.value;
  72. var knownArgs = directiveArgs[directiveName];
  73. if (directiveNode.arguments && knownArgs) {
  74. for (var _i6 = 0, _directiveNode$argume2 = directiveNode.arguments; _i6 < _directiveNode$argume2.length; _i6++) {
  75. var argNode = _directiveNode$argume2[_i6];
  76. var argName = argNode.name.value;
  77. if (knownArgs.indexOf(argName) === -1) {
  78. var suggestions = (0, _suggestionList.default)(argName, knownArgs);
  79. context.reportError(new _GraphQLError.GraphQLError(unknownDirectiveArgMessage(argName, directiveName, suggestions), argNode));
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85. };
  86. }