@@ -797,12 +797,35 @@ export function getStitchingOptionsFromSupergraphSdl(
797797 const fieldsConfig : Record < string , MergedFieldConfig > =
798798 ( mergedTypeConfig . fields = { } ) ;
799799 for ( const [ fieldName , fieldNameKey ] of fieldsKeyMap ) {
800- const aliasedFieldNameKey = fieldNameKey . includes ( '(' )
801- ? `_${ fieldNameKey . split ( '(' ) [ 0 ] } : ${ fieldNameKey } `
802- : fieldNameKey ;
803- extraKeys . add ( aliasedFieldNameKey ) ;
800+ const selectionSetNode = parseSelectionSet ( `{${ fieldNameKey } }` ) ;
801+ ( function aliasFieldsWithArgs ( selectionSetNode : SelectionSetNode ) {
802+ for ( const selection of selectionSetNode . selections ) {
803+ if (
804+ selection . kind === Kind . FIELD &&
805+ selection . arguments ?. length
806+ ) {
807+ // @ts -expect-error it's ok we're mutating consciously
808+ selection . alias = {
809+ kind : Kind . NAME ,
810+ value : '_' + selection . name . value ,
811+ } ;
812+ }
813+ if ( 'selectionSet' in selection && selection . selectionSet ) {
814+ aliasFieldsWithArgs ( selection . selectionSet ) ;
815+ }
816+ }
817+ } ) ( selectionSetNode ) ;
818+ const selectionSet = print ( selectionSetNode )
819+ // remove new lines
820+ . replaceAll ( / \n / g, ' ' )
821+ // remove extra spaces (only one space between tokens)
822+ . replaceAll ( / \s + / g, ' ' ) ;
823+ extraKeys . add (
824+ // remove first and last characters (curly braces)
825+ selectionSet . slice ( 1 , - 1 ) ,
826+ ) ;
804827 fieldsConfig [ fieldName ] = {
805- selectionSet : `{ ${ aliasedFieldNameKey } }` ,
828+ selectionSet,
806829 computed : true ,
807830 } ;
808831 }
0 commit comments