UNPKG

1.7 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.FragmentsOnCompositeTypesRule = FragmentsOnCompositeTypesRule;
7
8var _GraphQLError = require("../../error/GraphQLError.js");
9
10var _printer = require("../../language/printer.js");
11
12var _definition = require("../../type/definition.js");
13
14var _typeFromAST = require("../../utilities/typeFromAST.js");
15
16/**
17 * Fragments on composite type
18 *
19 * Fragments use a type condition to determine if they apply, since fragments
20 * can only be spread into a composite type (object, interface, or union), the
21 * type condition must also be a composite type.
22 */
23function FragmentsOnCompositeTypesRule(context) {
24 return {
25 InlineFragment: function InlineFragment(node) {
26 var typeCondition = node.typeCondition;
27
28 if (typeCondition) {
29 var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), typeCondition);
30
31 if (type && !(0, _definition.isCompositeType)(type)) {
32 var typeStr = (0, _printer.print)(typeCondition);
33 context.reportError(new _GraphQLError.GraphQLError("Fragment cannot condition on non composite type \"".concat(typeStr, "\"."), typeCondition));
34 }
35 }
36 },
37 FragmentDefinition: function FragmentDefinition(node) {
38 var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), node.typeCondition);
39
40 if (type && !(0, _definition.isCompositeType)(type)) {
41 var typeStr = (0, _printer.print)(node.typeCondition);
42 context.reportError(new _GraphQLError.GraphQLError("Fragment \"".concat(node.name.value, "\" cannot condition on non composite type \"").concat(typeStr, "\"."), node.typeCondition));
43 }
44 }
45 };
46}