UNPKG

2.5 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.PossibleFragmentSpreadsRule = PossibleFragmentSpreadsRule;
7
8var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
9
10var _GraphQLError = require("../../error/GraphQLError.js");
11
12var _definition = require("../../type/definition.js");
13
14var _typeFromAST = require("../../utilities/typeFromAST.js");
15
16var _typeComparators = require("../../utilities/typeComparators.js");
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 */
27function PossibleFragmentSpreadsRule(context) {
28 return {
29 InlineFragment: function InlineFragment(node) {
30 var fragType = context.getType();
31 var parentType = context.getParentType();
32
33 if ((0, _definition.isCompositeType)(fragType) && (0, _definition.isCompositeType)(parentType) && !(0, _typeComparators.doTypesOverlap)(context.getSchema(), fragType, parentType)) {
34 var parentTypeStr = (0, _inspect.default)(parentType);
35 var fragTypeStr = (0, _inspect.default)(fragType);
36 context.reportError(new _GraphQLError.GraphQLError("Fragment cannot be spread here as objects of type \"".concat(parentTypeStr, "\" can never be of type \"").concat(fragTypeStr, "\"."), node));
37 }
38 },
39 FragmentSpread: function FragmentSpread(node) {
40 var fragName = node.name.value;
41 var fragType = getFragmentType(context, fragName);
42 var parentType = context.getParentType();
43
44 if (fragType && parentType && !(0, _typeComparators.doTypesOverlap)(context.getSchema(), fragType, parentType)) {
45 var parentTypeStr = (0, _inspect.default)(parentType);
46 var fragTypeStr = (0, _inspect.default)(fragType);
47 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));
48 }
49 }
50 };
51}
52
53function getFragmentType(context, name) {
54 var frag = context.getFragment(name);
55
56 if (frag) {
57 var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), frag.typeCondition);
58
59 if ((0, _definition.isCompositeType)(type)) {
60 return type;
61 }
62 }
63}