PossibleFragmentSpreadsRule.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.PossibleFragmentSpreadsRule = PossibleFragmentSpreadsRule;
  6. var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
  7. var _GraphQLError = require("../../error/GraphQLError.js");
  8. var _definition = require("../../type/definition.js");
  9. var _typeFromAST = require("../../utilities/typeFromAST.js");
  10. var _typeComparators = require("../../utilities/typeComparators.js");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Possible fragment spread
  14. *
  15. * A fragment spread is only valid if the type condition could ever possibly
  16. * be true: if there is a non-empty intersection of the possible parent types,
  17. * and possible types which pass the type condition.
  18. */
  19. function PossibleFragmentSpreadsRule(context) {
  20. return {
  21. InlineFragment: function InlineFragment(node) {
  22. var fragType = context.getType();
  23. var parentType = context.getParentType();
  24. if ((0, _definition.isCompositeType)(fragType) && (0, _definition.isCompositeType)(parentType) && !(0, _typeComparators.doTypesOverlap)(context.getSchema(), fragType, parentType)) {
  25. var parentTypeStr = (0, _inspect.default)(parentType);
  26. var fragTypeStr = (0, _inspect.default)(fragType);
  27. context.reportError(new _GraphQLError.GraphQLError("Fragment cannot be spread here as objects of type \"".concat(parentTypeStr, "\" can never be of type \"").concat(fragTypeStr, "\"."), node));
  28. }
  29. },
  30. FragmentSpread: function FragmentSpread(node) {
  31. var fragName = node.name.value;
  32. var fragType = getFragmentType(context, fragName);
  33. var parentType = context.getParentType();
  34. if (fragType && parentType && !(0, _typeComparators.doTypesOverlap)(context.getSchema(), fragType, parentType)) {
  35. var parentTypeStr = (0, _inspect.default)(parentType);
  36. var fragTypeStr = (0, _inspect.default)(fragType);
  37. context.reportError(new _GraphQLError.GraphQLError("Fragment \"".concat(fragName, "\" cannot be spread here as objects of type \"").concat(parentTypeStr, "\" can never be of type \"").concat(fragTypeStr, "\"."), node));
  38. }
  39. }
  40. };
  41. }
  42. function getFragmentType(context, name) {
  43. var frag = context.getFragment(name);
  44. if (frag) {
  45. var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), frag.typeCondition);
  46. if ((0, _definition.isCompositeType)(type)) {
  47. return type;
  48. }
  49. }
  50. }