ProvidedRequiredArguments.mjs 4.8 KB

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