@apollo/federation
Version:
Apollo Federation Utilities
49 lines • 3.34 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.keyFieldsSelectInvalidType = void 0;
const graphql_1 = require("graphql");
const utils_1 = require("../../utils");
const keyFieldsSelectInvalidType = ({ schema, serviceList, }) => {
const errors = [];
const types = schema.getTypeMap();
for (const [typeName, namedType] of Object.entries(types)) {
if (!(0, graphql_1.isObjectType)(namedType))
continue;
const typeFederationMetadata = (0, utils_1.getFederationMetadata)(namedType);
if (typeFederationMetadata === null || typeFederationMetadata === void 0 ? void 0 : typeFederationMetadata.keys) {
const allFieldsInType = namedType.getFields();
for (const [serviceName, selectionSets = []] of Object.entries(typeFederationMetadata.keys)) {
for (const selectionSet of selectionSets) {
for (const field of selectionSet) {
const name = field.name.value;
const matchingField = allFieldsInType[name];
const typeNode = (0, utils_1.findTypeNodeInServiceList)(typeName, serviceName, serviceList);
const selectionSetNode = !(0, utils_1.isDirectiveDefinitionNode)(typeNode) ?
(0, utils_1.findSelectionSetOnNode)(typeNode, 'key', (0, utils_1.printFieldSet)(selectionSet)) : undefined;
if (!matchingField) {
errors.push((0, utils_1.errorWithCode)('KEY_FIELDS_SELECT_INVALID_TYPE', (0, utils_1.logServiceAndType)(serviceName, typeName) +
`A @key selects ${name}, but ${typeName}.${name} could not be found`, selectionSetNode));
}
if (matchingField) {
if ((0, graphql_1.isInterfaceType)(matchingField.type) ||
((0, graphql_1.isNonNullType)(matchingField.type) &&
(0, graphql_1.isInterfaceType)((0, graphql_1.getNullableType)(matchingField.type)))) {
errors.push((0, utils_1.errorWithCode)('KEY_FIELDS_SELECT_INVALID_TYPE', (0, utils_1.logServiceAndType)(serviceName, typeName) +
`A @key selects ${typeName}.${name}, which is an interface type. Keys cannot select interfaces.`, selectionSetNode));
}
if ((0, graphql_1.isUnionType)(matchingField.type) ||
((0, graphql_1.isNonNullType)(matchingField.type) &&
(0, graphql_1.isUnionType)((0, graphql_1.getNullableType)(matchingField.type)))) {
errors.push((0, utils_1.errorWithCode)('KEY_FIELDS_SELECT_INVALID_TYPE', (0, utils_1.logServiceAndType)(serviceName, typeName) +
`A @key selects ${typeName}.${name}, which is a union type. Keys cannot select union types.`, selectionSetNode));
}
}
}
}
}
}
}
return errors;
};
exports.keyFieldsSelectInvalidType = keyFieldsSelectInvalidType;
//# sourceMappingURL=keyFieldsSelectInvalidType.js.map
;