@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
46 lines (45 loc) • 2.38 kB
JavaScript
import { GraphQLError } from "graphql";
import { andList } from "../../../utils/format.js";
export 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 GraphQLError(`Type "${objectTypeState.name}" is marked @external on all the subgraphs in which it is listed (${subgraphs} ${(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 GraphQLError(`Field "${objectState.name}.${fieldState.name}" is marked @external on all the subgraphs in which it is listed (${subgraphs} ${andList(Array.from(fieldState.byGraph.keys()).map(context.graphIdToName), true, '"')}).`, {
extensions: {
code: "EXTERNAL_MISSING_ON_BASE",
},
}));
}
},
};
}