KnownArgumentNames.mjs 4.1 KB

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