diff --git a/composition-js/src/compose.ts b/composition-js/src/compose.ts index 3ec5711fc..d1aaf563f 100644 --- a/composition-js/src/compose.ts +++ b/composition-js/src/compose.ts @@ -25,6 +25,7 @@ export interface CompositionFailure { schema?: undefined; supergraphSdl?: undefined; hints?: undefined; + subgraphSdl?: { [subgraph: string]: string} } export interface CompositionSuccess { @@ -32,6 +33,7 @@ export interface CompositionSuccess { supergraphSdl: string; hints: CompositionHint[]; errors?: undefined; + subgraphSdl?: { [subgraph: string]: string} } export interface CompositionOptions { @@ -39,6 +41,14 @@ export interface CompositionOptions { allowedFieldTypeMergingSubtypingRules?: SubtypingRule[]; /// Flag to toggle if satisfiability should be performed during composition runSatisfiability?: boolean; + + // note that pipeline options are temporary and will be removed in the future + pipelineOptions?: { + runUpgradeSubgraphs?: boolean; + runValidateSubgraphs?: boolean; + runComposition?: boolean; + outputUpgradedSubgraphs?: boolean; + } } function validateCompositionOptions(options: CompositionOptions) { @@ -63,6 +73,24 @@ export function compose(subgraphs: Subgraphs, options: CompositionOptions = {}): if (mergeResult.errors) { return { errors: mergeResult.errors }; } + + const subgraphSdl: { [name: string]: string } | undefined = !!options.pipelineOptions?.outputUpgradedSubgraphs ? {} : undefined; + if (subgraphSdl !== undefined && mergeResult.subgraphs) { + for (const subgraph of mergeResult.subgraphs.values()) { + subgraphSdl[subgraph.name] = printSchema(subgraph.schema, sdlPrintOptions ?? shallowOrderPrintedDefinitions(defaultPrintOptions)); + } + } + + // if we don't have a supergraph, we're using the `runComposition`=false option. + if (!mergeResult.supergraph) { + return { + schema: undefined, + supergraphSdl: undefined, + hints: undefined, + subgraphSdl, + errors: [], + } + } let satisfiabilityResult; if (runSatisfiability) { @@ -84,11 +112,12 @@ export function compose(subgraphs: Subgraphs, options: CompositionOptions = {}): } catch (err) { return { errors: [err] }; } - + return { schema: mergeResult.supergraph, supergraphSdl, hints: [...mergeResult.hints, ...(satisfiabilityResult?.hints ?? [])], + subgraphSdl, }; } @@ -136,7 +165,7 @@ export function validateSatisfiability({ supergraphSchema, supergraphSdl} : Sati return validateGraphComposition(supergraph.schema, supergraph.subgraphNameToGraphEnumValue(), supergraphQueryGraph, federatedQueryGraph); } -type ValidateSubgraphsAndMergeResult = MergeResult | { errors: GraphQLError[] }; +type ValidateSubgraphsAndMergeResult = MergeResult | { errors: GraphQLError[] } | { subgraphs: Subgraphs, errors?: undefined, supergraph?: undefined }; /** * Upgrade subgraphs if necessary, then validates subgraphs before attempting to merge @@ -144,17 +173,28 @@ type ValidateSubgraphsAndMergeResult = MergeResult | { errors: GraphQLError[] }; * @param subgraphs * @returns ValidateSubgraphsAndMergeResult */ -function validateSubgraphsAndMerge(subgraphs: Subgraphs) : ValidateSubgraphsAndMergeResult { - const upgradeResult = upgradeSubgraphsIfNecessary(subgraphs); - if (upgradeResult.errors) { - return { errors: upgradeResult.errors }; +function validateSubgraphsAndMerge(subgraphs: Subgraphs, options: CompositionOptions = {}) : ValidateSubgraphsAndMergeResult { + let toMerge: Subgraphs; + + if (options.pipelineOptions?.runUpgradeSubgraphs !== false) { + const upgradeResult = upgradeSubgraphsIfNecessary(subgraphs); + if (upgradeResult.errors) { + return { errors: upgradeResult.errors }; + } + toMerge = upgradeResult.subgraphs; + } else { + toMerge = subgraphs; } - const toMerge = upgradeResult.subgraphs; - const validationErrors = toMerge.validate(); - if (validationErrors) { - return { errors: validationErrors }; + if (options.pipelineOptions?.runValidateSubgraphs !== false) { + const validationErrors = toMerge.validate(); + if (validationErrors) { + return { errors: validationErrors }; + } } - - return mergeSubgraphs(toMerge); + + if (options.pipelineOptions?.runComposition !== false) { + return mergeSubgraphs(toMerge); + } + return { subgraphs: toMerge }; } diff --git a/composition-js/src/merging/merge.ts b/composition-js/src/merging/merge.ts index 11975a65a..f83f5251c 100644 --- a/composition-js/src/merging/merge.ts +++ b/composition-js/src/merging/merge.ts @@ -220,6 +220,7 @@ export interface MergeSuccess { supergraph: Schema; hints: CompositionHint[]; errors?: undefined; + subgraphs?: Subgraphs, } export interface MergeFailure { @@ -769,7 +770,8 @@ class Merger { } else { return { supergraph: this.merged, - hints: this.hints + hints: this.hints, + subgraphs: this.subgraphs, } } }