UNPKG

1.43 kBJavaScriptView Raw
1import { GraphQLError } from "../../error/GraphQLError.mjs";
2import { print } from "../../language/printer.mjs";
3import { isCompositeType } from "../../type/definition.mjs";
4import { typeFromAST } from "../../utilities/typeFromAST.mjs";
5
6/**
7 * Fragments on composite type
8 *
9 * Fragments use a type condition to determine if they apply, since fragments
10 * can only be spread into a composite type (object, interface, or union), the
11 * type condition must also be a composite type.
12 */
13export function FragmentsOnCompositeTypesRule(context) {
14 return {
15 InlineFragment: function InlineFragment(node) {
16 var typeCondition = node.typeCondition;
17
18 if (typeCondition) {
19 var type = typeFromAST(context.getSchema(), typeCondition);
20
21 if (type && !isCompositeType(type)) {
22 var typeStr = print(typeCondition);
23 context.reportError(new GraphQLError("Fragment cannot condition on non composite type \"".concat(typeStr, "\"."), typeCondition));
24 }
25 }
26 },
27 FragmentDefinition: function FragmentDefinition(node) {
28 var type = typeFromAST(context.getSchema(), node.typeCondition);
29
30 if (type && !isCompositeType(type)) {
31 var typeStr = print(node.typeCondition);
32 context.reportError(new GraphQLError("Fragment \"".concat(node.name.value, "\" cannot condition on non composite type \"").concat(typeStr, "\"."), node.typeCondition));
33 }
34 }
35 };
36}