@@ -114,29 +114,46 @@ class DeferredJsonMerger {
114
114
}
115
115
116
116
private fun mergeIncrementalData (incrementalItem : JsonMap ) {
117
- val id = incrementalItem[" id" ] as String? ? : error(" No id found in incremental item" )
118
- val data = incrementalItem[" data" ] as JsonMap ? ? : error(" No data found in incremental item" )
117
+ val id = incrementalItem[" id" ] as String? ? : error(" No id found in incremental result" )
118
+ val data = incrementalItem[" data" ] as JsonMap ?
119
+ val items = incrementalItem[" items" ] as List <Any >?
119
120
val subPath = incrementalItem[" subPath" ] as List <Any >? ? : emptyList()
120
121
val path = (_pendingFragmentIds [id]?.path ? : error(" Id '$id ' not found in pending results" )) + subPath
121
122
val mergedData = merged[" data" ] as JsonMap
122
- val nodeToMergeInto = nodeAtPath(mergedData, path) as MutableJsonMap
123
- deepMerge(nodeToMergeInto, data)
123
+ val nodeToMergeInto = nodeAtPath(mergedData, path)
124
+ when {
125
+ data != null -> {
126
+ deepMergeObject(nodeToMergeInto as MutableJsonMap , data)
127
+ }
128
+
129
+ items != null -> {
130
+ mergeList(nodeToMergeInto as MutableList <Any >, items)
131
+ }
132
+
133
+ else -> {
134
+ error(" Neither data nor items found in incremental result" )
135
+ }
136
+ }
124
137
}
125
138
126
- private fun deepMerge (destination : MutableJsonMap , map : JsonMap ) {
127
- for ((key, value) in map ) {
139
+ private fun deepMergeObject (destination : MutableJsonMap , obj : JsonMap ) {
140
+ for ((key, value) in obj ) {
128
141
if (destination.containsKey(key) && destination[key] is MutableMap <* , * >) {
129
142
// Objects: merge recursively
130
143
val fieldDestination = destination[key] as MutableJsonMap
131
144
val fieldMap = value as ? JsonMap ? : error(" '$key ' is an object in destination but not in map" )
132
- deepMerge (destination = fieldDestination, map = fieldMap)
145
+ deepMergeObject (destination = fieldDestination, obj = fieldMap)
133
146
} else {
134
147
// Other types: add / overwrite
135
148
destination[key] = value
136
149
}
137
150
}
138
151
}
139
152
153
+ private fun mergeList (destination : MutableList <Any >, items : List <Any >) {
154
+ destination.addAll(items)
155
+ }
156
+
140
157
private fun jsonToMap (json : BufferedSource ): JsonMap = BufferedSourceJsonReader (json).readAny() as JsonMap
141
158
142
159
0 commit comments