graphql
Version:
A Query Language and Runtime which can target any service.
117 lines (96 loc) • 4.5 kB
JavaScript
var _defKindToExtKind;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import didYouMean from '../../jsutils/didYouMean';
import suggestionList from '../../jsutils/suggestionList';
import { GraphQLError } from '../../error/GraphQLError';
import { Kind } from '../../language/kinds';
import { isTypeDefinitionNode } from '../../language/predicates';
import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } from '../../type/definition';
export function extendingUnknownTypeMessage(typeName, suggestedTypes) {
return "Cannot extend type \"".concat(typeName, "\" because it is not defined.") + didYouMean(suggestedTypes.map(function (x) {
return "\"".concat(x, "\"");
}));
}
export function extendingDifferentTypeKindMessage(typeName, kind) {
return "Cannot extend non-".concat(kind, " type \"").concat(typeName, "\".");
}
/**
* Possible type extension
*
* A type extension is only valid if the type is defined and has the same kind.
*/
export function PossibleTypeExtensions(context) {
var schema = context.getSchema();
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] = def;
}
}
return {
ScalarTypeExtension: checkExtension,
ObjectTypeExtension: checkExtension,
InterfaceTypeExtension: checkExtension,
UnionTypeExtension: checkExtension,
EnumTypeExtension: checkExtension,
InputObjectTypeExtension: checkExtension
};
function checkExtension(node) {
var typeName = node.name.value;
var defNode = definedTypes[typeName];
var existingType = schema && schema.getType(typeName);
if (defNode) {
var expectedKind = defKindToExtKind[defNode.kind];
if (expectedKind !== node.kind) {
context.reportError(new GraphQLError(extendingDifferentTypeKindMessage(typeName, extensionKindToTypeName(expectedKind)), [defNode, node]));
}
} else if (existingType) {
var _expectedKind = typeToExtKind(existingType);
if (_expectedKind !== node.kind) {
context.reportError(new GraphQLError(extendingDifferentTypeKindMessage(typeName, extensionKindToTypeName(_expectedKind)), node));
}
} else {
var allTypeNames = Object.keys(definedTypes);
if (schema) {
allTypeNames = allTypeNames.concat(Object.keys(schema.getTypeMap()));
}
var suggestedTypes = suggestionList(typeName, allTypeNames);
context.reportError(new GraphQLError(extendingUnknownTypeMessage(typeName, suggestedTypes), node.name));
}
}
}
var defKindToExtKind = (_defKindToExtKind = {}, _defineProperty(_defKindToExtKind, Kind.SCALAR_TYPE_DEFINITION, Kind.SCALAR_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.OBJECT_TYPE_DEFINITION, Kind.OBJECT_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.INTERFACE_TYPE_DEFINITION, Kind.INTERFACE_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.UNION_TYPE_DEFINITION, Kind.UNION_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.ENUM_TYPE_DEFINITION, Kind.ENUM_TYPE_EXTENSION), _defineProperty(_defKindToExtKind, Kind.INPUT_OBJECT_TYPE_DEFINITION, Kind.INPUT_OBJECT_TYPE_EXTENSION), _defKindToExtKind);
function typeToExtKind(type) {
if (isScalarType(type)) {
return Kind.SCALAR_TYPE_EXTENSION;
} else if (isObjectType(type)) {
return Kind.OBJECT_TYPE_EXTENSION;
} else if (isInterfaceType(type)) {
return Kind.INTERFACE_TYPE_EXTENSION;
} else if (isUnionType(type)) {
return Kind.UNION_TYPE_EXTENSION;
} else if (isEnumType(type)) {
return Kind.ENUM_TYPE_EXTENSION;
} else if (isInputObjectType(type)) {
return Kind.INPUT_OBJECT_TYPE_EXTENSION;
}
}
function extensionKindToTypeName(kind) {
switch (kind) {
case Kind.SCALAR_TYPE_EXTENSION:
return 'scalar';
case Kind.OBJECT_TYPE_EXTENSION:
return 'object';
case Kind.INTERFACE_TYPE_EXTENSION:
return 'interface';
case Kind.UNION_TYPE_EXTENSION:
return 'union';
case Kind.ENUM_TYPE_EXTENSION:
return 'enum';
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
return 'input object';
default:
return 'unknown type';
}
}