Skip to content

Commit 02dd7a7

Browse files
authored
let the datafetcher resolve variable references (#205)
1 parent 0112bab commit 02dd7a7

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

core/src/main/kotlin/org/neo4j/graphql/Translator.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ class Translator(val schema: GraphQLSchema) {
2727
.filterIsInstance<Field>() // FragmentSpread, InlineFragment
2828
.map { field ->
2929
val cypher = toQuery(operationDefinition.operation, field, fragments, params, ctx)
30-
val resolvedParams = cypher.params.mapValues { toBoltValue(it.value, params) }
31-
cypher.with(resolvedParams) // was cypher.with(params)
30+
val resolvedParams = cypher.params.mapValues { toBoltValue(it.value) }
31+
cypher.with(resolvedParams)
3232
}
3333
}
3434
}
3535

36-
private fun toBoltValue(value: Any?, params: Map<String, Any?>) = when (value) {
37-
is VariableReference -> params[value.name]
36+
private fun toBoltValue(value: Any?) = when (value) {
3837
is BigInteger -> value.longValueExact()
3938
is BigDecimal -> value.toDouble()
4039
else -> value

core/src/main/kotlin/org/neo4j/graphql/handler/BaseDataFetcher.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.neo4j.graphql.handler
22

33
import graphql.language.Field
4+
import graphql.language.VariableReference
45
import graphql.schema.DataFetcher
56
import graphql.schema.DataFetchingEnvironment
67
import graphql.schema.GraphQLFieldDefinition
@@ -29,7 +30,12 @@ abstract class BaseDataFetcher(val fieldDefinition: GraphQLFieldDefinition) : Pr
2930
.withPrettyPrint(true)
3031
.build()
3132
).render(statement)
32-
return Cypher(query, statement.parameters, fieldDefinition.type, variable = field.aliasOrName())
33+
34+
val params = statement.parameters.mapValues { (_, value) ->
35+
(value as? VariableReference)?.let { env.variables[it.name] } ?: value
36+
}
37+
38+
return Cypher(query, params, fieldDefinition.type, variable = field.aliasOrName())
3339
}
3440

3541
protected abstract fun generateCypher(variable: String, field: Field, env: DataFetchingEnvironment): Statement

0 commit comments

Comments
 (0)