UNPKG

@theguild/federation-composition

Version:
49 lines (48 loc) 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FieldArgumentsOfTheSameTypeRule = FieldArgumentsOfTheSameTypeRule; const graphql_1 = require("graphql"); 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 graphql_1.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', }, })); } }, }; }