import { printSchema , Schema , Subgraphs , defaultPrintOptions , orderPrintedDefinitions , ServiceDefinition , subgraphsFromServiceList , ERRORS , } from "@apollo/federation-internals" ; import { GraphQLError } from "graphql" ; import { buildFederatedQueryGraph , buildSupergraphAPIQueryGraph } from "@apollo/query-graphs" ; import { mergeSubgraphs } from "./merging" ; import { validateGraphComposition } from "./validate" ; import { CompositionHint } from "./hints" ; export type CompositionResult = CompositionFailure | CompositionSuccess ; export interface CompositionFailure { errors : GraphQLError [ ] ; schema ? : undefined ; supergraphSdl ? : undefined ; hints ? : undefined ; } export interface CompositionSuccess { schema : Schema ; supergraphSdl : string ; hints : CompositionHint [ ] ; errors ? : undefined ; } export function compose ( subgraphs ) { const mergeResult = mergeSubgraphs ( subgraphs ) ; if ( mergeResult . errors ) { return { errors : mergeResult . errors } ; } const supergraphSchema = mergeResult . supergraph ; const supergraphQueryGraph = buildSupergraphAPIQueryGraph ( supergraphSchema ) ; const federatedQueryGraph = buildFederatedQueryGraph ( supergraphSchema , false ) ; const validationResult = validateGraphComposition ( supergraphQueryGraph , federatedQueryGraph ) ; if ( validationResult . errors ) { return { errors : validationResult . errors . map ( e => ERRORS . SATISFIABILITY_ERROR . err ( { message : e . message } ) ) } ; } let supergraphSdl ; try { supergraphSdl = printSchema ( supergraphSchema , orderPrintedDefinitions ( defaultPrintOptions ) ) ; } catch ( err ) { return { errors : [ err ] } ; } return { schema : supergraphSchema , supergraphSdl , hints : mergeResult . hints } ; } export function composeServices ( services ) { const subgraphs = subgraphsFromServiceList ( services ) ; if ( Array . isArray ( subgraphs ) ) { return { errors : subgraphs } ; } return compose ( subgraphs ) ; }