Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse when condition in blueprint #494

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import graphql.Scalars.GraphQLString
import graphql.language.EnumTypeDefinition
import graphql.language.FieldDefinition
import graphql.language.ImplementingTypeDefinition
import graphql.language.StringValue
import graphql.nadel.Service
import graphql.nadel.dsl.FieldMappingDefinition
import graphql.nadel.dsl.RemoteArgumentSource.SourceType.FieldArgument
Expand All @@ -20,6 +19,7 @@ import graphql.nadel.engine.blueprint.hydration.NadelBatchHydrationMatchStrategy
import graphql.nadel.engine.blueprint.hydration.NadelHydrationActorInputDef
import graphql.nadel.engine.blueprint.hydration.NadelHydrationActorInputDef.ValueSource.FieldResultValue
import graphql.nadel.engine.blueprint.hydration.NadelHydrationStrategy
import graphql.nadel.engine.blueprint.hydration.NadelHydrationWhenCondition
import graphql.nadel.engine.transform.query.NadelQueryPath
import graphql.nadel.engine.util.AnyImplementingTypeDefinition
import graphql.nadel.engine.util.AnyNamedNode
Expand Down Expand Up @@ -265,9 +265,35 @@ private class Factory(
is NadelHydrationActorInputDef.ValueSource.StaticValue -> null
}
},
condition = getHydrationCondition(hydration),
)
}

private fun getHydrationCondition(hydration: UnderlyingServiceHydration): NadelHydrationWhenCondition? {
if (hydration.conditionalHydration == null) {
return null
}
if (hydration.conditionalHydration.predicate.equals != null) {
return NadelHydrationWhenCondition.ResultEquals(
fieldPath = NadelQueryPath(hydration.conditionalHydration.sourceField),
value = hydration.conditionalHydration.predicate.equals
)
}
if (hydration.conditionalHydration.predicate.startsWith != null) {
return NadelHydrationWhenCondition.StringResultStartsWith(
fieldPath = NadelQueryPath(hydration.conditionalHydration.sourceField),
prefix = hydration.conditionalHydration.predicate.startsWith
)
}
if (hydration.conditionalHydration.predicate.matches != null) {
return NadelHydrationWhenCondition.StringResultMatches(
fieldPath = NadelQueryPath(hydration.conditionalHydration.sourceField),
regex = hydration.conditionalHydration.predicate.matches
)
}
error("a conditional hydration is defined but doesnt have any predicate")
}

private fun getHydrationStrategy(
hydratedFieldParentType: GraphQLObjectType,
hydratedFieldDef: GraphQLFieldDefinition,
Expand Down Expand Up @@ -352,6 +378,7 @@ private class Factory(
batchHydrationMatchStrategy = matchStrategy,
actorFieldDef = actorFieldDef,
actorFieldContainer = actorFieldContainer,
condition = getHydrationCondition(hydration),
sourceFields = Unit.let {
val paths = (when (matchStrategy) {
NadelBatchHydrationMatchStrategy.MatchIndex -> emptyList()
Expand Down Expand Up @@ -489,7 +516,7 @@ private class Factory(
}
StaticArgument -> {
NadelHydrationActorInputDef.ValueSource.StaticValue(
value = remoteArgDef.remoteArgumentSource.staticValue!!
value = remoteArgDef.remoteArgumentSource.staticValue!!
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just from reformatting

)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import graphql.nadel.Service
import graphql.nadel.engine.blueprint.hydration.NadelBatchHydrationMatchStrategy
import graphql.nadel.engine.blueprint.hydration.NadelHydrationActorInputDef
import graphql.nadel.engine.blueprint.hydration.NadelHydrationStrategy
import graphql.nadel.engine.blueprint.hydration.NadelHydrationWhenCondition
import graphql.nadel.engine.transform.query.NadelQueryPath
import graphql.schema.FieldCoordinates
import graphql.schema.GraphQLFieldDefinition
Expand Down Expand Up @@ -82,6 +83,11 @@ interface NadelGenericHydrationInstruction {
* The container of the actor field in the overall schema referenced by [queryPathToActorField].
*/
val actorFieldContainer: GraphQLFieldsContainer

/**
* The optional definition for conditional hydrations
*/
val condition: NadelHydrationWhenCondition?
}

data class NadelHydrationFieldInstruction(
Expand All @@ -95,6 +101,7 @@ data class NadelHydrationFieldInstruction(
override val sourceFields: List<NadelQueryPath>,
override val actorFieldDef: GraphQLFieldDefinition,
override val actorFieldContainer: GraphQLFieldsContainer,
override val condition: NadelHydrationWhenCondition?,
val hydrationStrategy: NadelHydrationStrategy,
) : NadelFieldInstruction(), NadelGenericHydrationInstruction

Expand All @@ -109,6 +116,7 @@ data class NadelBatchHydrationFieldInstruction(
override val sourceFields: List<NadelQueryPath>,
override val actorFieldDef: GraphQLFieldDefinition,
override val actorFieldContainer: GraphQLFieldsContainer,
override val condition: NadelHydrationWhenCondition?,
val batchSize: Int,
val batchHydrationMatchStrategy: NadelBatchHydrationMatchStrategy,
) : NadelFieldInstruction(), NadelGenericHydrationInstruction
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package graphql.nadel.engine.blueprint.hydration

import graphql.nadel.engine.transform.query.NadelQueryPath

sealed class NadelHydrationWhenCondition {
abstract fun evaluate(resultId: String): Boolean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resultId should be Any (maybe nullable too) probably. But we can fix that up in another PR.


data class ResultEquals(
val fieldPath: NadelQueryPath,
val value: Any,
) : NadelHydrationWhenCondition() {
override fun evaluate(resultId: String): Boolean {
TODO("Not yet implemented")
}
}

data class StringResultMatches(
val fieldPath: NadelQueryPath,
val regex: Regex,
) : NadelHydrationWhenCondition() {
override fun evaluate(resultId: String): Boolean {
TODO("Not yet implemented")
}
}

data class StringResultStartsWith(
val fieldPath: NadelQueryPath,
val prefix: String,
) : NadelHydrationWhenCondition() {
override fun evaluate(resultId: String): Boolean {
TODO("Not yet implemented")
}
}
}
Loading