@@ -5,7 +5,7 @@ import {PredefinedFragments} from '../fragments';
55
66function findParametersInDocument ( doc ) {
77 if ( doc && doc . definitions ) {
8- return _ . flatMap ( doc . definitions , def => findParametersInSelectionSet ( def . selectionSet ) ) ;
8+ return doc . definitions . flatMap ( def => findParametersInSelectionSet ( def . selectionSet ) ) ;
99 }
1010
1111 return [ ] ;
@@ -31,7 +31,7 @@ function replaceFragmentsInDocument(doc, fragments) {
3131 let clonedQuery = null ;
3232 if ( doc && doc . definitions ) {
3333 clonedQuery = _ . cloneDeep ( doc ) ;
34- _ . each ( clonedQuery . definitions , def => replaceFragmentsInSelectionSet ( def . selectionSet , fragments , def , clonedQuery ) ) ;
34+ clonedQuery . definitions . forEach ( def => replaceFragmentsInSelectionSet ( def . selectionSet , fragments , def , clonedQuery ) ) ;
3535 clonedQuery . definitions [ 0 ] . name . value = key ;
3636 }
3737
@@ -42,10 +42,10 @@ function replaceFragmentsInDocument(doc, fragments) {
4242
4343function findParametersInSelectionSet ( selectionSet ) {
4444 if ( selectionSet && selectionSet . selections ) {
45- return _ . flatMap ( selectionSet . selections , sel =>
46- _ . without ( _ . concat (
47- _ . flatMap ( _ . filter ( sel . arguments , arg => ( arg . value . kind === 'Variable' ) ) , arg => arg . value . name . value ) ,
48- findParametersInSelectionSet ( sel . selectionSet ) ) , undefined )
45+ return selectionSet . selections . flatMap ( sel =>
46+ sel . arguments . filter ( arg => ( arg . value . kind === 'Variable' ) ) . flatMap ( arg => arg . value . name . value )
47+ . concat ( findParametersInSelectionSet ( sel . selectionSet ) )
48+ . filter ( f => typeof f !== ' undefined' )
4949 ) ;
5050 }
5151
@@ -54,9 +54,8 @@ function findParametersInSelectionSet(selectionSet) {
5454
5555function findFragmentsInSelectionSet ( selectionSet ) {
5656 if ( selectionSet && selectionSet . selections ) {
57- return _ . concat (
58- _ . map ( _ . filter ( selectionSet . selections , sel => sel . kind === 'FragmentSpread' ) , sel => sel . name . value ) ,
59- _ . flatMap ( selectionSet . selections , sel => findFragmentsInSelectionSet ( sel . selectionSet ) ) ) ;
57+ return selectionSet . selections . filter ( sel => sel . kind === 'FragmentSpread' ) . map ( sel => sel . name . value )
58+ . concat ( selectionSet . selections . flatMap ( sel => findFragmentsInSelectionSet ( sel . selectionSet ) ) ) ;
6059 }
6160
6261 return [ ] ;
@@ -67,39 +66,38 @@ function replaceFragmentsInSelectionSet(selectionSet, fragments, def, document)
6766 let newFragmentsSpreads = [ ] ;
6867 let removedFragmentSpreads = [ ] ;
6968 // Look for all existing fragment spreads in selection set
70- _ . each ( _ . filter ( selectionSet . selections , sel => sel . kind === 'FragmentSpread' ) , sel => {
69+ selectionSet . selections . filter ( sel => sel . kind === 'FragmentSpread' ) . forEach ( sel => {
7170 // Handle only named fragments
7271 if ( sel . name . value ) {
7372 // Check if spread exists in current doc - if not, we replace or remove it
74- let existing = _ . find ( document . definitions , definition => definition . kind === 'FragmentDefinition' && definition . name . value === sel . name . value ) ;
73+ let existing = document . definitions . find ( definition => definition . kind === 'FragmentDefinition' && definition . name . value === sel . name . value ) ;
7574
7675 if ( ! existing ) {
7776 // First remove the spread, as it has no match in document
7877 removedFragmentSpreads . push ( sel ) ;
7978
8079 // Check if a replacement is provided for this pseudo-fragment, then insert spreads and definitions
8180 if ( fragments ) {
82- fragments = _ . map ( fragments , frag => ( typeof frag === 'string' ) ? PredefinedFragments [ frag ] : frag ) ;
81+ fragments = fragments . map ( frag => ( typeof frag === 'string' ) ? PredefinedFragments [ frag ] : frag ) ;
8382
84- let applyableFragments = _ . filter ( fragments , frag => frag . applyFor === sel . name . value ) ;
83+ const applyableFragments = fragments . filter ( frag => frag . applyFor === sel . name . value ) ;
8584
86- let allFragmentsDefinitions = _ . flatMap ( applyableFragments , fragment => fragment . gql . definitions ) ;
87- _ . each ( allFragmentsDefinitions , frag => {
88- let newSpread = _ . cloneDeep ( sel ) ;
85+ applyableFragments . flatMap ( fragment => fragment . gql . definitions ) . forEach ( frag => {
86+ const newSpread = _ . cloneDeep ( sel ) ;
8987 newSpread . name . value = frag . name . value ;
9088 newFragmentsSpreads . push ( newSpread ) ;
9189
9290 // Add the new fragment definition in document if it has not already been added
93- let existing = _ . find ( document . definitions , definition => definition . kind === 'FragmentDefinition' && definition . name . value === frag . name . value ) ;
91+ let existing = document . definitions . find ( definition => definition . kind === 'FragmentDefinition' && definition . name . value === frag . name . value ) ;
9492 if ( ! existing ) {
9593 document . definitions . push ( frag ) ;
9694 }
9795 } ) ;
9896
9997 // Adds the associated variables to the query
100- let allVariables = _ . reduce ( applyableFragments , ( result , n ) => _ . assign ( result , n . variables ) , { } ) ;
101- _ . each ( allVariables , ( value , name ) => {
102- let existing = _ . find ( def . variableDefinitions , def => def . variable . name . value === name ) ;
98+ let allVariables = applyableFragments . reduce ( ( result , n ) => ( { ... result , ... n . variables } ) , { } ) ;
99+ Object . entries ( allVariables ) . forEach ( ( [ name , value ] ) => {
100+ let existing = def . variableDefinitions . find ( def => def . variable . name . value === name ) ;
103101 if ( ! existing ) {
104102 let type = parseType ( value , { noLocation : true } ) ;
105103 def . variableDefinitions . push ( {
@@ -121,13 +119,13 @@ function replaceFragmentsInSelectionSet(selectionSet, fragments, def, document)
121119 } ) ;
122120
123121 // Removed replaced spreads
124- selectionSet . selections = _ . filter ( selectionSet . selections , sel => removedFragmentSpreads . indexOf ( sel ) === - 1 ) ;
122+ selectionSet . selections = selectionSet . selections . filter ( sel => removedFragmentSpreads . indexOf ( sel ) === - 1 ) ;
125123
126124 // Add all new spreads
127125 selectionSet . selections . push ( ...newFragmentsSpreads ) ;
128126
129127 // Recursively call on sub-selections set
130- _ . each ( selectionSet . selections , sel => replaceFragmentsInSelectionSet ( sel . selectionSet , fragments , def , document ) ) ;
128+ selectionSet . selections . forEach ( sel => replaceFragmentsInSelectionSet ( sel . selectionSet , fragments , def , document ) ) ;
131129 }
132130}
133131
0 commit comments