UNPKG

@theguild/federation-composition

Version:

Open Source Composition library for Apollo Federation

33 lines (32 loc) 1.58 kB
import { GraphQLError } from 'graphql'; export function OverrideSourceHasOverrideRule(context) { return { ObjectTypeField(objectTypeState, fieldState) { if (fieldState.override === null) { return; } const graphsWithOverride = Array.from(fieldState.byGraph).filter(onlyWithOverride); if (graphsWithOverride.length === 1) { return; } for (let i = 0; i < graphsWithOverride.length; i++) { const [graph, fieldStateInGraph] = graphsWithOverride[i]; const overrideValue = fieldStateInGraph.override.toUpperCase(); const graphFromOverride = fieldState.byGraph.get(overrideValue); const anotherGraphId = graphFromOverride && graphFromOverride.override !== null ? overrideValue : graphsWithOverride[i + 1] ? graphsWithOverride[i + 1][0] : graphsWithOverride[0][0]; context.reportError(new GraphQLError(`Field "${objectTypeState.name}.${fieldState.name}" on subgraph "${context.graphIdToName(graph)}" is also marked with directive @override in subgraph "${context.graphIdToName(anotherGraphId)}". Only one @override directive is allowed per field.`, { extensions: { code: 'OVERRIDE_SOURCE_HAS_OVERRIDE', }, })); } }, }; } function onlyWithOverride(entry) { return entry[1].override !== null; }