1- import { dset } from 'dset/merge' ;
21import { GraphQLError } from 'graphql' ;
32import { ExecutionResult } from './Interfaces.js' ;
3+ import { mergeDeep } from './mergeDeep.js' ;
44
55export function mergeIncrementalResult ( {
66 incrementalResult,
@@ -13,14 +13,14 @@ export function mergeIncrementalResult({
1313
1414 if ( incrementalResult . items ) {
1515 for ( const item of incrementalResult . items ) {
16- dset ( executionResult , path , item ) ;
16+ setObjectKeyPath ( executionResult , path , item ) ;
1717 // Increment the last path segment (the array index) to merge the next item at the next index
1818 ( path [ path . length - 1 ] as number ) ++ ;
1919 }
2020 }
2121
2222 if ( incrementalResult . data ) {
23- dset ( executionResult , path , incrementalResult . data ) ;
23+ setObjectKeyPath ( executionResult , path , incrementalResult . data ) ;
2424 }
2525
2626 if ( incrementalResult . errors ) {
@@ -29,7 +29,7 @@ export function mergeIncrementalResult({
2929 }
3030
3131 if ( incrementalResult . extensions ) {
32- dset ( executionResult , 'extensions' , incrementalResult . extensions ) ;
32+ setObjectKeyPath ( executionResult , [ 'extensions' ] , incrementalResult . extensions ) ;
3333 }
3434
3535 if ( incrementalResult . incremental ) {
@@ -41,3 +41,25 @@ export function mergeIncrementalResult({
4141 } ) ;
4242 }
4343}
44+
45+ function setObjectKeyPath ( obj : Record < string , any > , keyPath : ( string | number ) [ ] , value : any ) {
46+ let current = obj ;
47+ let i : number ;
48+ for ( i = 0 ; i < keyPath . length - 1 ; i ++ ) {
49+ const key = keyPath [ i ] ;
50+ if ( key === '__proto__' || key === 'constructor' || key === 'prototype' ) {
51+ return ;
52+ }
53+ if ( current [ key ] == null ) {
54+ // Determine if the next key is a number to create an array, otherwise create an object
55+ current [ key ] = typeof keyPath [ i + 1 ] === 'number' ? [ ] : { } ;
56+ }
57+ current = current [ key ] ;
58+ }
59+ const finalKey = keyPath [ i ] ;
60+ if ( finalKey === '__proto__' || finalKey === 'constructor' || finalKey === 'prototype' ) {
61+ return ;
62+ }
63+ const existingValue = current [ finalKey ] ;
64+ current [ finalKey ] = existingValue != null ? mergeDeep ( [ existingValue , value ] ) : value ;
65+ }
0 commit comments