@@ -1052,6 +1052,7 @@ export function getStitchingOptionsFromSupergraphSdl(
10521052 }
10531053 let executor : Executor = buildHTTPExecutor ( {
10541054 endpoint,
1055+ serviceName : subgraphName ,
10551056 ...httpExecutorOpts ,
10561057 } ) ;
10571058 if ( globalThis . process ?. env ?. [ 'DEBUG' ] ) {
@@ -1307,6 +1308,31 @@ export function getStitchingOptionsFromSupergraphSdl(
13071308 }
13081309 const jobs : Promise < void > [ ] = [ ] ;
13091310 let hasPromise = false ;
1311+ const fieldNames = new Set < string > ( ) ;
1312+ currentAvailableSelectionSet ?. selections . forEach ( ( selection ) => {
1313+ if (
1314+ selection . kind === Kind . FIELD &&
1315+ selection . name . value === info . fieldName
1316+ ) {
1317+ selection . selectionSet ?. selections . forEach ( ( selection ) => {
1318+ if ( selection . kind === Kind . FIELD ) {
1319+ fieldNames . add ( selection . name . value ) ;
1320+ }
1321+ } ) ;
1322+ }
1323+ } ) ;
1324+ currentUnavailableSelectionSet ?. selections . forEach ( ( selection ) => {
1325+ if (
1326+ selection . kind === Kind . FIELD &&
1327+ selection . name . value === info . fieldName
1328+ ) {
1329+ selection . selectionSet ?. selections . forEach ( ( selection ) => {
1330+ if ( selection . kind === Kind . FIELD ) {
1331+ fieldNames . add ( selection . name . value ) ;
1332+ }
1333+ } ) ;
1334+ }
1335+ } ) ;
13101336 const mainJob = delegateToSchema ( {
13111337 schema : currentSubschema ,
13121338 operation :
@@ -1357,9 +1383,11 @@ export function getStitchingOptionsFromSupergraphSdl(
13571383 return jobs [ 0 ] ;
13581384 }
13591385 if ( hasPromise ) {
1360- return Promise . all ( jobs ) . then ( ( results ) => mergeResults ( results ) ) ;
1386+ return Promise . all ( jobs ) . then ( ( results ) =>
1387+ mergeResults ( results , fieldNames ) ,
1388+ ) ;
13611389 }
1362- return mergeResults ( jobs ) ;
1390+ return mergeResults ( jobs , fieldNames ) ;
13631391 } ;
13641392 if ( operationType === 'subscription' ) {
13651393 return {
@@ -1573,14 +1601,50 @@ const specifiedTypeNames = [
15731601 '_Entity' ,
15741602] ;
15751603
1576- function makeExternalObject ( data : any , errors : Error [ ] ) {
1604+ function makeExternalObject (
1605+ data : any ,
1606+ errors : Error [ ] ,
1607+ fieldNames : Set < string > ,
1608+ ) {
15771609 if ( ! isExternalObject ( data ) && typeof data === 'object' && data != null ) {
15781610 data [ UNPATHED_ERRORS_SYMBOL ] = errors ;
15791611 }
1612+ if ( errors . length ) {
1613+ const errorsToPop = [ ...errors ] ;
1614+ const fieldNamesToPop : string [ ] = [ ] ;
1615+ for ( const fieldName of fieldNames ) {
1616+ if ( data ?. [ fieldName ] == null ) {
1617+ fieldNamesToPop . push ( fieldName ) ;
1618+ }
1619+ }
1620+ while ( fieldNamesToPop . length && errorsToPop . length ) {
1621+ const fieldName = fieldNamesToPop . pop ( ) ;
1622+ if ( ! fieldName ) {
1623+ break ;
1624+ }
1625+ const error = errorsToPop . pop ( ) ;
1626+ if ( ! error ) {
1627+ break ;
1628+ }
1629+ let errorToSet : Error | undefined ;
1630+ if ( fieldNamesToPop . length || ! errorsToPop . length ) {
1631+ errorToSet = error ;
1632+ } else {
1633+ errorToSet = new AggregateError (
1634+ errorsToPop ,
1635+ errorsToPop . map ( ( error ) => error . message ) . join ( ', \n' ) ,
1636+ ) ;
1637+ }
1638+ if ( errorToSet ) {
1639+ data ||= { } ;
1640+ data [ fieldName ] = errorToSet ;
1641+ }
1642+ }
1643+ }
15801644 return data ;
15811645}
15821646
1583- function mergeResults ( results : unknown [ ] ) {
1647+ function mergeResults ( results : unknown [ ] , fieldNames : Set < string > ) {
15841648 const errors : Error [ ] = [ ] ;
15851649 const datas : unknown [ ] = [ ] ;
15861650 for ( const result of results ) {
@@ -1594,9 +1658,13 @@ function mergeResults(results: unknown[]) {
15941658 }
15951659 if ( datas . length ) {
15961660 if ( datas . length === 1 ) {
1597- return makeExternalObject ( datas [ 0 ] , errors ) ;
1661+ return makeExternalObject ( datas [ 0 ] , errors , fieldNames ) ;
15981662 }
1599- return makeExternalObject ( mergeDeep ( datas , undefined , true , true ) , errors ) ;
1663+ return makeExternalObject (
1664+ mergeDeep ( datas , undefined , true , true ) ,
1665+ errors ,
1666+ fieldNames ,
1667+ ) ;
16001668 }
16011669 if ( errors . length ) {
16021670 if ( errors . length === 1 ) {
0 commit comments