@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
48 lines (47 loc) • 2.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExternalMissingOnBaseRule = ExternalMissingOnBaseRule;
const graphql_1 = require("graphql");
const format_js_1 = require("../../../utils/format.js");
function ExternalMissingOnBaseRule(context) {
return {
ObjectType(objectTypeState) {
if (Array.from(objectTypeState.byGraph).every(([_, stateInGraph]) => stateInGraph.external === true)) {
const subgraphs = objectTypeState.byGraph.size > 1 ? 'subgraphs' : 'subgraph';
context.reportError(new graphql_1.GraphQLError(`Type "${objectTypeState.name}" is marked @external on all the subgraphs in which it is listed (${subgraphs} ${((0, format_js_1.andList)(Array.from(objectTypeState.byGraph.keys()).map(graphId => context.graphIdToName(graphId))),
true,
'"')}).`, {
extensions: {
code: 'EXTERNAL_MISSING_ON_BASE',
},
}));
}
},
ObjectTypeField(objectState, fieldState) {
if (Array.from(fieldState.byGraph).every(([graphId, stateInGraph]) => {
const graphVersion = context.subgraphStates.get(graphId).federation.version;
if (stateInGraph.usedAsKey) {
if (graphVersion === 'v1.0') {
return stateInGraph.external && !objectState.byGraph.get(graphId).extension;
}
return (stateInGraph.external === true &&
objectState.byGraph.get(graphId).extensionType !== '@extends');
}
if (graphVersion === 'v1.0') {
if (stateInGraph.external === true && stateInGraph.used) {
return true;
}
return false;
}
return stateInGraph.external === true;
})) {
const subgraphs = fieldState.byGraph.size > 1 ? 'subgraphs' : 'subgraph';
context.reportError(new graphql_1.GraphQLError(`Field "${objectState.name}.${fieldState.name}" is marked @external on all the subgraphs in which it is listed (${subgraphs} ${(0, format_js_1.andList)(Array.from(fieldState.byGraph.keys()).map(context.graphIdToName), true, '"')}).`, {
extensions: {
code: 'EXTERNAL_MISSING_ON_BASE',
},
}));
}
},
};
}
;