ProvidedRequiredArguments.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.missingFieldArgMessage = missingFieldArgMessage;
  6. exports.missingDirectiveArgMessage = missingDirectiveArgMessage;
  7. exports.ProvidedRequiredArguments = ProvidedRequiredArguments;
  8. exports.ProvidedRequiredArgumentsOnDirectives = ProvidedRequiredArgumentsOnDirectives;
  9. var _inspect = _interopRequireDefault(require("../../jsutils/inspect"));
  10. var _keyMap = _interopRequireDefault(require("../../jsutils/keyMap"));
  11. var _GraphQLError = require("../../error/GraphQLError");
  12. var _kinds = require("../../language/kinds");
  13. var _printer = require("../../language/printer");
  14. var _directives = require("../../type/directives");
  15. var _definition = require("../../type/definition");
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. 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; }
  18. 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; }
  19. 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; }
  20. function missingFieldArgMessage(fieldName, argName, type) {
  21. return "Field \"".concat(fieldName, "\" argument \"").concat(argName, "\" of type \"").concat(type, "\" is required, but it was not provided.");
  22. }
  23. function missingDirectiveArgMessage(directiveName, argName, type) {
  24. return "Directive \"@".concat(directiveName, "\" argument \"").concat(argName, "\" of type \"").concat(type, "\" is required, but it was not provided.");
  25. }
  26. /**
  27. * Provided required arguments
  28. *
  29. * A field or directive is only valid if all required (non-null without a
  30. * default value) field arguments have been provided.
  31. */
  32. function ProvidedRequiredArguments(context) {
  33. return _objectSpread({}, ProvidedRequiredArgumentsOnDirectives(context), {
  34. Field: {
  35. // Validate on leave to allow for deeper errors to appear first.
  36. leave: function leave(fieldNode) {
  37. var fieldDef = context.getFieldDef();
  38. if (!fieldDef) {
  39. return false;
  40. }
  41. var argNodes = fieldNode.arguments || [];
  42. var argNodeMap = (0, _keyMap.default)(argNodes, function (arg) {
  43. return arg.name.value;
  44. });
  45. for (var _i2 = 0, _fieldDef$args2 = fieldDef.args; _i2 < _fieldDef$args2.length; _i2++) {
  46. var argDef = _fieldDef$args2[_i2];
  47. var argNode = argNodeMap[argDef.name];
  48. if (!argNode && (0, _definition.isRequiredArgument)(argDef)) {
  49. context.reportError(new _GraphQLError.GraphQLError(missingFieldArgMessage(fieldDef.name, argDef.name, (0, _inspect.default)(argDef.type)), fieldNode));
  50. }
  51. }
  52. }
  53. }
  54. });
  55. } // @internal
  56. function ProvidedRequiredArgumentsOnDirectives(context) {
  57. var requiredArgsMap = Object.create(null);
  58. var schema = context.getSchema();
  59. var definedDirectives = schema ? schema.getDirectives() : _directives.specifiedDirectives;
  60. for (var _i4 = 0; _i4 < definedDirectives.length; _i4++) {
  61. var directive = definedDirectives[_i4];
  62. requiredArgsMap[directive.name] = (0, _keyMap.default)(directive.args.filter(_definition.isRequiredArgument), function (arg) {
  63. return arg.name;
  64. });
  65. }
  66. var astDefinitions = context.getDocument().definitions;
  67. for (var _i6 = 0; _i6 < astDefinitions.length; _i6++) {
  68. var def = astDefinitions[_i6];
  69. if (def.kind === _kinds.Kind.DIRECTIVE_DEFINITION) {
  70. requiredArgsMap[def.name.value] = (0, _keyMap.default)(def.arguments ? def.arguments.filter(isRequiredArgumentNode) : [], function (arg) {
  71. return arg.name.value;
  72. });
  73. }
  74. }
  75. return {
  76. Directive: {
  77. // Validate on leave to allow for deeper errors to appear first.
  78. leave: function leave(directiveNode) {
  79. var directiveName = directiveNode.name.value;
  80. var requiredArgs = requiredArgsMap[directiveName];
  81. if (requiredArgs) {
  82. var argNodes = directiveNode.arguments || [];
  83. var argNodeMap = (0, _keyMap.default)(argNodes, function (arg) {
  84. return arg.name.value;
  85. });
  86. for (var _i8 = 0, _Object$keys2 = Object.keys(requiredArgs); _i8 < _Object$keys2.length; _i8++) {
  87. var argName = _Object$keys2[_i8];
  88. if (!argNodeMap[argName]) {
  89. var argType = requiredArgs[argName].type;
  90. context.reportError(new _GraphQLError.GraphQLError(missingDirectiveArgMessage(directiveName, argName, (0, _definition.isType)(argType) ? (0, _inspect.default)(argType) : (0, _printer.print)(argType)), directiveNode));
  91. }
  92. }
  93. }
  94. }
  95. }
  96. };
  97. }
  98. function isRequiredArgumentNode(arg) {
  99. return arg.type.kind === _kinds.Kind.NON_NULL_TYPE && arg.defaultValue == null;
  100. }