1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.PossibleFragmentSpreadsRule = PossibleFragmentSpreadsRule;
|
7 |
|
8 | var _inspect = _interopRequireDefault(require("../../jsutils/inspect.js"));
|
9 |
|
10 | var _GraphQLError = require("../../error/GraphQLError.js");
|
11 |
|
12 | var _definition = require("../../type/definition.js");
|
13 |
|
14 | var _typeFromAST = require("../../utilities/typeFromAST.js");
|
15 |
|
16 | var _typeComparators = require("../../utilities/typeComparators.js");
|
17 |
|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function 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 |
|
53 | function 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 | }
|