blob: 0216087cfa1eafe1416b98ddf1bb365632da6160 [file] [log] [blame]
import didYouMean from "../../jsutils/didYouMean.mjs";
import suggestionList from "../../jsutils/suggestionList.mjs";
import { GraphQLError } from "../../error/GraphQLError.mjs";
import { isTypeDefinitionNode, isTypeSystemDefinitionNode, isTypeSystemExtensionNode } from "../../language/predicates.mjs";
import { specifiedScalarTypes } from "../../type/scalars.mjs";
/**
* Known type names
*
* A GraphQL document is only valid if referenced types (specifically
* variable definitions and fragment conditions) are defined by the type schema.
*/
export function KnownTypeNamesRule(context) {
var schema = context.getSchema();
var existingTypesMap = schema ? schema.getTypeMap() : Object.create(null);
var definedTypes = Object.create(null);
for (var _i2 = 0, _context$getDocument$2 = context.getDocument().definitions; _i2 < _context$getDocument$2.length; _i2++) {
var def = _context$getDocument$2[_i2];
if (isTypeDefinitionNode(def)) {
definedTypes[def.name.value] = true;
}
}
var typeNames = Object.keys(existingTypesMap).concat(Object.keys(definedTypes));
return {
NamedType: function NamedType(node, _1, parent, _2, ancestors) {
var typeName = node.name.value;
if (!existingTypesMap[typeName] && !definedTypes[typeName]) {
var _ancestors$;
var definitionNode = (_ancestors$ = ancestors[2]) !== null && _ancestors$ !== void 0 ? _ancestors$ : parent;
var isSDL = definitionNode != null && isSDLNode(definitionNode);
if (isSDL && isSpecifiedScalarName(typeName)) {
return;
}
var suggestedTypes = suggestionList(typeName, isSDL ? specifiedScalarsNames.concat(typeNames) : typeNames);
context.reportError(new GraphQLError("Unknown type \"".concat(typeName, "\".") + didYouMean(suggestedTypes), node));
}
}
};
}
var specifiedScalarsNames = specifiedScalarTypes.map(function (type) {
return type.name;
});
function isSpecifiedScalarName(typeName) {
return specifiedScalarsNames.indexOf(typeName) !== -1;
}
function isSDLNode(value) {
return !Array.isArray(value) && (isTypeSystemDefinitionNode(value) || isTypeSystemExtensionNode(value));
}