UNPKG

1.23 kBJavaScriptView Raw
1import inspect from "../../jsutils/inspect.mjs";
2import { GraphQLError } from "../../error/GraphQLError.mjs";
3import { getNamedType, isLeafType } from "../../type/definition.mjs";
4
5/**
6 * Scalar leafs
7 *
8 * A GraphQL document is valid only if all leaf fields (fields without
9 * sub selections) are of scalar or enum types.
10 */
11export function ScalarLeafsRule(context) {
12 return {
13 Field: function Field(node) {
14 var type = context.getType();
15 var selectionSet = node.selectionSet;
16
17 if (type) {
18 if (isLeafType(getNamedType(type))) {
19 if (selectionSet) {
20 var fieldName = node.name.value;
21 var typeStr = inspect(type);
22 context.reportError(new GraphQLError("Field \"".concat(fieldName, "\" must not have a selection since type \"").concat(typeStr, "\" has no subfields."), selectionSet));
23 }
24 } else if (!selectionSet) {
25 var _fieldName = node.name.value;
26
27 var _typeStr = inspect(type);
28
29 context.reportError(new GraphQLError("Field \"".concat(_fieldName, "\" of type \"").concat(_typeStr, "\" must have a selection of subfields. Did you mean \"").concat(_fieldName, " { ... }\"?"), node));
30 }
31 }
32 }
33 };
34}