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C,KAAK,CAAC;oBAClC,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;oBAC9B,OAAO,CAAC,WAAW,CACjB,IAAI,YAAY,CACd,UAAU,SAAS,cAAc,OAAO,0CAA0C,EAClF,EAAE,KAAK,EAAE,IAAI,EAAE,CAChB,CACF,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["/** @category Validation Rules */\n\nimport { inspect } from '../../jsutils/inspect.ts';\n\nimport { GraphQLError } from '../../error/GraphQLError.ts';\n\nimport type { FieldNode } from '../../language/ast.ts';\nimport type { ASTVisitor } from '../../language/visitor.ts';\n\nimport { getNamedType, isLeafType } from '../../type/definition.ts';\n\nimport type { ValidationContext } from '../ValidationContext.ts';\n\n/**\n * Scalar leafs\n *\n * A GraphQL document is valid only if all leaf fields (fields without\n * sub selections) are of scalar or enum types.\n * @param context - The validation context used while checking the document.\n * @returns A visitor that reports validation errors for this rule.\n * @example\n * ```ts\n * import { buildSchema, parse, validate } from 'graphql';\n * import { ScalarLeafsRule } from 'graphql/validation';\n *\n * const schema = buildSchema(`\n *   type Query {\n *     name: String\n *   }\n * `);\n *\n * const invalidDocument = parse(`\n *   { name { length } }\n * `);\n * const invalidErrors = validate(schema, invalidDocument, [ScalarLeafsRule]);\n *\n * invalidErrors.length; // => 1\n *\n * const validDocument = parse(`\n *   { name }\n * `);\n * const validErrors = validate(schema, validDocument, [ScalarLeafsRule]);\n *\n * validErrors; // => []\n * ```\n */\nexport function ScalarLeafsRule(context: ValidationContext): ASTVisitor {\n  return {\n    Field(node: FieldNode) {\n      const type = context.getType();\n      const selectionSet = node.selectionSet;\n      if (type) {\n        if (isLeafType(getNamedType(type))) {\n          if (selectionSet) {\n            const fieldName = node.name.value;\n            const typeStr = inspect(type);\n            context.reportError(\n              new GraphQLError(\n                `Field \"${fieldName}\" must not have a selection since type \"${typeStr}\" has no subfields.`,\n                { nodes: selectionSet },\n              ),\n            );\n          }\n        } else if (!selectionSet) {\n          const fieldName = node.name.value;\n          const typeStr = inspect(type);\n          context.reportError(\n            new GraphQLError(\n              `Field \"${fieldName}\" of type \"${typeStr}\" must have a selection of subfields. Did you mean \"${fieldName} { ... }\"?`,\n              { nodes: node },\n            ),\n          );\n        } else if (selectionSet.selections.length === 0) {\n          const fieldName = node.name.value;\n          const typeStr = inspect(type);\n          context.reportError(\n            new GraphQLError(\n              `Field \"${fieldName}\" of type \"${typeStr}\" must have at least one field selected.`,\n              { nodes: node },\n            ),\n          );\n        }\n      }\n    },\n  };\n}\n"]}