@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
23 lines (22 loc) • 1.01 kB
JavaScript
import { GraphQLError } from "graphql";
export function ContextualArgumentRule(context) {
return {
ObjectTypeFieldArg(objectState, fieldState, argState) {
if (!argState.fromContext) {
return;
}
const isRequiredSomewhere = Array.from(argState.byGraph.values()).some((argInGraph) => !argInGraph.fromContext &&
argInGraph.type.endsWith("!") &&
!argInGraph.defaultValue);
if (!isRequiredSomewhere) {
return;
}
const coordinate = `${objectState.name}.${fieldState.name}(${argState.name}:)`;
context.reportError(new GraphQLError(`Argument "${coordinate}" is contextual in at least one subgraph but in "${coordinate}" it does not have @fromContext, is not nullable and has no default value.`, {
extensions: {
code: "CONTEXTUAL_ARGUMENT_NOT_CONTEXTUAL_IN_ALL_SUBGRAPHS",
},
}));
},
};
}