ScalarLeafs.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.noSubselectionAllowedMessage = noSubselectionAllowedMessage;
  6. exports.requiredSubselectionMessage = requiredSubselectionMessage;
  7. exports.ScalarLeafs = ScalarLeafs;
  8. var _inspect = _interopRequireDefault(require("../../jsutils/inspect"));
  9. var _GraphQLError = require("../../error/GraphQLError");
  10. var _definition = require("../../type/definition");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function noSubselectionAllowedMessage(fieldName, type) {
  13. return "Field \"".concat(fieldName, "\" must not have a selection since type \"").concat(type, "\" has no subfields.");
  14. }
  15. function requiredSubselectionMessage(fieldName, type) {
  16. return "Field \"".concat(fieldName, "\" of type \"").concat(type, "\" must have a selection of subfields. Did you mean \"").concat(fieldName, " { ... }\"?");
  17. }
  18. /**
  19. * Scalar leafs
  20. *
  21. * A GraphQL document is valid only if all leaf fields (fields without
  22. * sub selections) are of scalar or enum types.
  23. */
  24. function ScalarLeafs(context) {
  25. return {
  26. Field: function Field(node) {
  27. var type = context.getType();
  28. var selectionSet = node.selectionSet;
  29. if (type) {
  30. if ((0, _definition.isLeafType)((0, _definition.getNamedType)(type))) {
  31. if (selectionSet) {
  32. context.reportError(new _GraphQLError.GraphQLError(noSubselectionAllowedMessage(node.name.value, (0, _inspect.default)(type)), selectionSet));
  33. }
  34. } else if (!selectionSet) {
  35. context.reportError(new _GraphQLError.GraphQLError(requiredSubselectionMessage(node.name.value, (0, _inspect.default)(type)), node));
  36. }
  37. }
  38. }
  39. };
  40. }