@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
42 lines (41 loc) • 2.18 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, fieldStateInGraph]) => {
const graphVersion = context.subgraphStates.get(graphId).federation.version;
if (fieldStateInGraph.usedAsKey) {
return (fieldStateInGraph.external &&
!objectState.byGraph.get(graphId).extension);
}
if (graphVersion === "v1.0") {
if (fieldStateInGraph.external === true && fieldStateInGraph.used) {
return true;
}
return false;
}
return fieldStateInGraph.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",
},
}));
}
},
};
}