ProvidedRequiredArgumentsRule.js 5.9 KB

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