ProvidedRequiredArgumentsRule.mjs 5.3 KB

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