UNPKG

@theguild/federation-composition

Version:
46 lines (45 loc) 2.15 kB
import { GraphQLError } from 'graphql'; export function FieldArgumentsOfTheSameTypeRule(context) { return { ObjectTypeFieldArg(objectTypeState, fieldState, argState) { const typeToGraphs = new Map(); argState.byGraph.forEach((arg, graphName) => { const isNonNullable = arg.type.endsWith('!'); const isNonNullableInSupergraph = argState.type.endsWith('!'); const isMatchingNonNullablePart = argState.type.replace(/!$/, '') === arg.type.replace(/!$/, ''); let normalizedOutputType; if (isMatchingNonNullablePart) { normalizedOutputType = isNonNullableInSupergraph ? isNonNullable ? arg.type : arg.type + '!' : arg.type; } else { normalizedOutputType = arg.type; } const existing = typeToGraphs.get(normalizedOutputType); if (existing) { existing.push(graphName); } else { typeToGraphs.set(normalizedOutputType, [graphName]); } }); if (typeToGraphs.size > 1) { const groups = Array.from(typeToGraphs.entries()).map(([outputType, graphs]) => { const plural = graphs.length > 1 ? 's' : ''; return `type "${outputType}" in subgraph${plural} "${graphs .map(context.graphIdToName) .join('", "')}"`; }); const [first, second, ...rest] = groups; context.reportError(new GraphQLError(`Type of argument "${objectTypeState.name}.${fieldState.name}(${argState.name}:)" is incompatible across subgraphs: it has ${first} but ${second}${rest.length ? ` and ${rest.join(' and ')}` : ''}`, { extensions: { code: 'FIELD_ARGUMENT_TYPE_MISMATCH', }, })); } }, }; }