-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
769 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...n/java/graphql/nadel/NadelResultMerger.kt → ...graphql/nadel/result/NadelResultMerger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/src/main/java/graphql/nadel/result/NadelResultPathBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package graphql.nadel.result | ||
|
||
internal interface NadelResultPathBuilder { | ||
fun add(key: String): NadelResultPathBuilder | ||
fun add(index: Int): NadelResultPathBuilder | ||
fun build(): List<NadelResultPathSegment> | ||
|
||
companion object { | ||
operator fun invoke( | ||
path: List<NadelResultPathSegment> = emptyList(), | ||
): NadelResultPathBuilder { | ||
return object : NadelResultPathBuilder { | ||
private var segments = path.toMutableList() | ||
|
||
override fun add(key: String): NadelResultPathBuilder { | ||
segments.add(NadelResultPathSegment.Object(key)) | ||
return this | ||
} | ||
|
||
override fun add(index: Int): NadelResultPathBuilder { | ||
segments.add(NadelResultPathSegment.Array(index)) | ||
return this | ||
} | ||
|
||
override fun build(): List<NadelResultPathSegment> { | ||
return segments | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal fun List<NadelResultPathSegment>.toBuilder(): NadelResultPathBuilder { | ||
return NadelResultPathBuilder(this) | ||
} |
9 changes: 9 additions & 0 deletions
9
lib/src/main/java/graphql/nadel/result/NadelResultPathSegment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package graphql.nadel.result | ||
|
||
internal sealed interface NadelResultPathSegment { | ||
@JvmInline | ||
value class Object(val key: String) : NadelResultPathSegment | ||
|
||
@JvmInline | ||
value class Array(val index: Int) : NadelResultPathSegment | ||
} |
154 changes: 154 additions & 0 deletions
154
lib/src/main/java/graphql/nadel/result/NadelResultTracker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package graphql.nadel.result | ||
|
||
import graphql.ExecutionResult | ||
import graphql.incremental.DelayedIncrementalPartialResult | ||
import graphql.nadel.engine.transform.query.NadelQueryPath | ||
import graphql.nadel.engine.transform.result.json.JsonNode | ||
import graphql.nadel.engine.util.AnyList | ||
import graphql.nadel.engine.util.AnyMap | ||
import kotlinx.coroutines.CompletableDeferred | ||
|
||
/** | ||
* todo: this needs to track multiple responses | ||
*/ | ||
internal class NadelResultTracker { | ||
private val result = CompletableDeferred<ExecutionResult>() | ||
|
||
private enum class NavigationOutcome { | ||
QueuedChild, | ||
ExpandedArray, | ||
DeadEnd, | ||
} | ||
|
||
suspend fun getResultPath( | ||
queryPath: NadelQueryPath, | ||
node: JsonNode, | ||
): List<NadelResultPathSegment>? { | ||
val result = result.await() | ||
val data = result.getData<Any?>() | ||
|
||
val queryPathSegments = queryPath.segments | ||
val currentResultPathSegments = mutableListOf<NadelResultPathSegment>() | ||
val currentQueryPathSegments = mutableListOf<String>() | ||
|
||
val queue = mutableListOf<Any?>(data) | ||
|
||
fun printState(): String { | ||
return """ | ||
Current query path: ${ | ||
currentQueryPathSegments.joinToString( | ||
separator = ",", | ||
prefix = "[", | ||
postfix = "]" | ||
) | ||
} | ||
Current result path: ${ | ||
currentResultPathSegments.joinToString( | ||
separator = ",", | ||
prefix = "[", | ||
postfix = "]" | ||
) | ||
} | ||
""".replaceIndent(' '.toString().repeat(n = 4)) | ||
} | ||
|
||
println( | ||
"Trying to find $node from ${ | ||
queryPath.segments.joinToString( | ||
separator = ",", | ||
prefix = "[", | ||
postfix = "]" | ||
) | ||
}" | ||
) | ||
|
||
while (queue.isNotEmpty()) { | ||
val element = queue.removeLast() | ||
println("Visiting $element") | ||
if (element === node.value) { | ||
println("Got em $currentResultPathSegments") | ||
return currentResultPathSegments | ||
} | ||
|
||
val outcome: NavigationOutcome = when (element) { | ||
is AnyList -> { | ||
if (element.isNotEmpty()) { | ||
queue.addAll(element) | ||
currentResultPathSegments.add(NadelResultPathSegment.Array(element.lastIndex)) | ||
NavigationOutcome.ExpandedArray | ||
} else { | ||
NavigationOutcome.DeadEnd | ||
} | ||
} | ||
is AnyMap -> { | ||
if (currentQueryPathSegments.size < queryPathSegments.size) { | ||
val nextQueryPathSegment = queryPathSegments[currentQueryPathSegments.size] | ||
val nextElement = element[nextQueryPathSegment] | ||
|
||
if (nextElement == null) { | ||
NavigationOutcome.DeadEnd // todo: tests | ||
} else { | ||
queue.add(nextElement) | ||
currentResultPathSegments.add(NadelResultPathSegment.Object(nextQueryPathSegment)) | ||
currentQueryPathSegments.add(nextQueryPathSegment) | ||
NavigationOutcome.QueuedChild | ||
} | ||
} else { | ||
NavigationOutcome.DeadEnd // todo: tests | ||
} | ||
} | ||
else -> NavigationOutcome.DeadEnd // todo: tests | ||
} | ||
|
||
when (outcome) { | ||
NavigationOutcome.QueuedChild -> { | ||
} | ||
NavigationOutcome.ExpandedArray -> { | ||
} | ||
NavigationOutcome.DeadEnd -> { | ||
if (queue.isNotEmpty()) { // i.e. we have other array elements to visit | ||
while (currentResultPathSegments.isNotEmpty()) { | ||
val last = currentResultPathSegments.lastOrNull() ?: break | ||
|
||
when (last) { | ||
is NadelResultPathSegment.Array -> { | ||
if (last.index == 0) { | ||
// Nothing more to visit in the array, remember that we traverse end -> front | ||
println("Popping dead end array") | ||
currentResultPathSegments.removeLast() | ||
} else { | ||
println("Moving to next array element ${last.index - 1}") | ||
// We're moving to the next element | ||
currentResultPathSegments[currentResultPathSegments.lastIndex] = | ||
NadelResultPathSegment.Array(last.index - 1) | ||
// Nothing further to fix, stop | ||
break | ||
} | ||
} | ||
is NadelResultPathSegment.Object -> { | ||
println("Popping up dead end object") | ||
currentResultPathSegments.removeLast() | ||
currentQueryPathSegments.removeLast() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
println("Loop end\n${printState()}") | ||
} | ||
|
||
println("Didn't get em") | ||
return null | ||
} | ||
|
||
fun complete(value: ExecutionResult) { | ||
result.complete(value) | ||
} | ||
|
||
fun complete(value: DelayedIncrementalPartialResult) { | ||
// result.complete(value) | ||
// todo: track here | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package graphql.nadel | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
|
||
val jsonObjectMapper = jacksonObjectMapper() |
Oops, something went wrong.