ScalarLeafsRule.js 1.5 KB

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