Skip to content

Commit

Permalink
Begin work splitting source IDs to different hydration services (#476)
Browse files Browse the repository at this point in the history
* Begin work splitting source IDs to different hydration services

* Fix test inaccuracies

* Fix more oopsies
  • Loading branch information
gnawf committed Dec 6, 2023
1 parent fa44500 commit 8691951
Show file tree
Hide file tree
Showing 12 changed files with 710 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class NadelAliasHelper private constructor(private val alias: String) {
fun getQueryPath(
path: NadelQueryPath,
): NadelQueryPath {
// todo: why not just clone and set first index here?
return path.mapIndexed { index, segment ->
when (index) {
0 -> getResultKey(segment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ internal object NadelHydrationFieldsBuilder {
userContext = userContext,
)

return makeBatchActorQueries(
executionBlueprint = executionBlueprint,
instruction = instruction,
aliasHelper = aliasHelper,
hydratedField = hydratedField,
argBatches = argBatches,
)
}

fun makeBatchActorQueries(
executionBlueprint: NadelOverallExecutionBlueprint,
instruction: NadelBatchHydrationFieldInstruction,
aliasHelper: NadelAliasHelper,
hydratedField: ExecutableNormalizedField,
argBatches: List<Map<NadelHydrationActorInputDef, NormalizedInputValue>>,
): List<ExecutableNormalizedField> {
val actorFieldOverallObjectTypeNames = getActorFieldOverallObjectTypenames(instruction, executionBlueprint)
val fieldChildren = deepClone(fields = hydratedField.children)
.mapNotNull { childField ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import graphql.nadel.engine.blueprint.hydration.NadelHydrationStrategy
import graphql.nadel.engine.transform.artificial.NadelAliasHelper
import graphql.nadel.engine.transform.result.json.JsonNode
import graphql.nadel.engine.transform.result.json.JsonNodeExtractor
import graphql.nadel.engine.util.AnyAstValue
import graphql.nadel.engine.util.emptyOrSingle
import graphql.nadel.engine.util.flatten
import graphql.nadel.engine.util.javaValueToAstValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal object NadelBatchHydrationInputBuilder {
return batchArgs.map { nonBatchArgs + it }
}

private fun getNonBatchInputValues(
internal fun getNonBatchInputValues(
instruction: NadelBatchHydrationFieldInstruction,
hydrationField: ExecutableNormalizedField,
): Map<NadelHydrationActorInputDef, NormalizedInputValue> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package graphql.nadel.engine.transform.hydration.batch

import graphql.nadel.engine.blueprint.NadelBatchHydrationFieldInstruction
import graphql.nadel.engine.blueprint.hydration.NadelBatchHydrationMatchStrategy
import graphql.nadel.engine.blueprint.hydration.NadelHydrationActorInputDef
import graphql.nadel.engine.transform.hydration.batch.NadelBatchHydrationInputBuilder.getBatchInputDef
import graphql.nadel.engine.transform.hydration.batch.NadelBatchHydrationInputBuilder.getNonBatchInputValues
import graphql.nadel.engine.transform.result.json.JsonNode
import graphql.nadel.engine.util.javaValueToAstValue
import graphql.nadel.hooks.NadelExecutionHooks
import graphql.normalized.ExecutableNormalizedField
import graphql.normalized.NormalizedInputValue
import graphql.schema.GraphQLTypeUtil

/**
* todo: did I follow this README when forking?
*
* README
*
* Please ensure that the batch arguments are ordered according to the input.
* This is required for [NadelBatchHydrationMatchStrategy.MatchIndex].
*/
internal object NadelNewBatchHydrationInputBuilder {
fun getInputValueBatches(
hooks: NadelExecutionHooks,
userContext: Any?,
instruction: NadelBatchHydrationFieldInstruction,
hydrationField: ExecutableNormalizedField,
sourceIds: List<JsonNode>,
): List<Map<NadelHydrationActorInputDef, NormalizedInputValue>> {
val nonBatchArgs = getNonBatchInputValues(instruction, hydrationField)
val batchArgs = getBatchInputValues(instruction, sourceIds, hooks, userContext)

return batchArgs.map { nonBatchArgs + it }
}

private fun getBatchInputValues(
instruction: NadelBatchHydrationFieldInstruction,
sourceIds: List<JsonNode>,
hooks: NadelExecutionHooks,
userContext: Any?,
): List<Pair<NadelHydrationActorInputDef, NormalizedInputValue>> {
val batchSize = instruction.batchSize

val (batchInputDef, batchInputValueSource) = getBatchInputDef(instruction) ?: return emptyList()
val actorBatchArgDef = instruction.actorFieldDef.getArgument(batchInputDef.name)

val partitionArgumentList = hooks.partitionBatchHydrationArgumentList(
argumentValues = sourceIds.map { it.value },
instruction = instruction,
userContext = userContext,
)

return partitionArgumentList
.flatMap {
it.chunked(size = batchSize)
}
.map { chunk ->
batchInputDef to NormalizedInputValue(
GraphQLTypeUtil.simplePrint(actorBatchArgDef.type),
javaValueToAstValue(chunk),
)
}
}
}
Loading

0 comments on commit 8691951

Please sign in to comment.