PossibleFragmentSpreads.js 2.7 KB

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