Skip to content

Commit 39caa21

Browse files
authored
Allow Translator to return multiple Cypher queries (#266)
resolves #265
1 parent 6eac42f commit 39caa21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+904
-115
lines changed

.run/Run All tests including integration tests.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<component name="ProjectRunConfigurationManager">
22
<configuration default="false" name="Run All tests including integration tests" type="JUnit" factoryName="JUnit">
3-
<module name="neo4j-graphql-java" />
3+
<module name="neo4j-graphql-java (1)" />
44
<option name="PACKAGE_NAME" value="org.neo4j.graphql" />
55
<option name="MAIN_CLASS_NAME" value="" />
66
<option name="METHOD_NAME" value="" />

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import graphql.schema.GraphQLSchema
66

77
class Translator(val schema: GraphQLSchema) {
88

9-
class CypherHolder(var cypher: Cypher?)
9+
class CypherHolder(val cyphers :MutableList<Cypher> = mutableListOf())
1010

1111
private val gql: GraphQL = GraphQL.newGraphQL(schema).build()
1212

1313
@JvmOverloads
1414
@Throws(OptimizedQueryException::class)
1515
fun translate(query: String, params: Map<String, Any?> = emptyMap(), ctx: QueryContext = QueryContext()): List<Cypher> {
16-
val cypherHolder = CypherHolder(null)
16+
val cypherHolder = CypherHolder()
1717
val executionInput = ExecutionInput.newExecutionInput()
1818
.query(query)
1919
.variables(params)
@@ -36,6 +36,6 @@ class Translator(val schema: GraphQLSchema) {
3636
}
3737
}
3838

39-
return listOf(requireNotNull(cypherHolder.cypher))
39+
return cypherHolder.cyphers
4040
}
4141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class BaseDataFetcher(schemaConfig: SchemaConfig) : ProjectionBase(sche
3838
}
3939
return Cypher(query, params, env.fieldDefinition.type, variable = field.aliasOrName())
4040
.also {
41-
(env.getLocalContext() as? Translator.CypherHolder)?.apply { this.cypher = it }
41+
(env.getLocalContext() as? Translator.CypherHolder)?.apply { this.cyphers += it }
4242
}
4343
}
4444

core/src/test/kotlin/org/neo4j/graphql/utils/AsciiDocTestSuite.kt

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ open class AsciiDocTestSuite(
6767
private var currentDocumentLevel: DocumentLevel? = null
6868
private var currentDepth = 0
6969

70-
private val globalCodeBlocks = mutableMapOf<String, ParsedBlock>()
71-
private var codeBlocksOfTest = mutableMapOf<String, ParsedBlock>()
70+
private val globalCodeBlocks = mutableMapOf<String, MutableList<ParsedBlock>>()
71+
private var codeBlocksOfTest = mutableMapOf<String, MutableList<ParsedBlock>>()
7272

7373
fun parse(): Stream<DynamicNode> {
74-
val file = File(AsciiDocTestSuite::class.java.getResource("/$fileName").toURI())
74+
val file = File(AsciiDocTestSuite::class.java.getResource("/$fileName")?.toURI()!!)
7575
val lines = file.readLines()
76-
val terminatorElement = testCaseMarkers.lastOrNull()
7776

7877
var title: String? = null
7978
var currentBlock: ParsedBlock? = null
@@ -92,11 +91,17 @@ open class AsciiDocTestSuite(
9291
val headlineMatcher = HEADLINE_PATTERN.matcher(line)
9392

9493
when {
95-
!globalDone && globalMarkers.contains(line) -> currentBlock = startBlock(line, lineNr, globalCodeBlocks)
94+
!globalDone && globalMarkers.contains(line) -> currentBlock =
95+
startBlock(line, lineNr, globalCodeBlocks)
9696
testCaseMarkers.contains(line) -> {
9797
globalDone = true
9898
currentBlock = startBlock(line, lineNr, codeBlocksOfTest)
9999
}
100+
line == "'''" -> {
101+
createTests(title, lineNr, ignore)
102+
currentBlock = null
103+
ignore = false
104+
}
100105
line == "----" -> {
101106
inside = !inside
102107
if (inside) {
@@ -111,16 +116,10 @@ open class AsciiDocTestSuite(
111116
SCHEMA_MARKER -> {
112117
val schemaTests = schemaTestFactory(currentBlock.code())
113118
currentDocumentLevel?.tests?.add(schemaTests)
114-
if (terminatorElement == null) {
119+
if (testCaseMarkers.isEmpty()){
115120
break@loop
116121
}
117122
}
118-
119-
terminatorElement -> {
120-
createTests(title, lineNr, ignore)
121-
currentBlock = null
122-
ignore = false
123-
}
124123
}
125124

126125
}
@@ -143,15 +142,22 @@ open class AsciiDocTestSuite(
143142

144143
if (UPDATE_TEST_FILE) {
145144
// this test prints out the adjusted test file
146-
root?.afterTests?.add(DynamicTest.dynamicTest("Write updated Testfile", srcLocation, this@AsciiDocTestSuite::writeAdjustedTestFile))
145+
root?.afterTests?.add(
146+
DynamicTest.dynamicTest("Write updated Testfile", srcLocation, this@AsciiDocTestSuite::writeAdjustedTestFile)
147+
)
147148
} else if (GENERATE_TEST_FILE_DIFF) {
148149
// this test prints out the adjusted test file
149-
root?.afterTests?.add(DynamicTest.dynamicTest("Adjusted Tests", srcLocation, this@AsciiDocTestSuite::printAdjustedTestFile))
150+
root?.afterTests?.add(
151+
DynamicTest.dynamicTest("Adjusted Tests", srcLocation, this@AsciiDocTestSuite::printAdjustedTestFile)
152+
)
150153
}
151154
return root?.generateTests() ?: Stream.empty()
152155
}
153156

154157
private fun createTests(title: String?, lineNr: Int, ignore: Boolean) {
158+
if (codeBlocksOfTest.isEmpty()) {
159+
throw IllegalStateException("no code blocks for tests (line $lineNr)")
160+
}
155161
val tests = testFactory(
156162
title ?: throw IllegalStateException("Title should be defined (line $lineNr)"),
157163
globalCodeBlocks,
@@ -211,33 +217,34 @@ open class AsciiDocTestSuite(
211217
return rebuildTest.toString()
212218
}
213219

214-
fun startBlock(marker: String, lineIndex: Int, blocks: MutableMap<String, ParsedBlock>): ParsedBlock {
220+
fun startBlock(marker: String, lineIndex: Int, blocks: MutableMap<String, MutableList<ParsedBlock>>): ParsedBlock {
215221
val uri = UriBuilder.fromUri(srcLocation).queryParam("line", lineIndex + 1).build()
216222
val block = ParsedBlock(marker, uri)
217223
knownBlocks += block
218-
blocks[marker] = block
224+
blocks.computeIfAbsent(marker) { mutableListOf() }.add(block)
219225
return block
220226
}
221227

222-
protected open fun testFactory(title: String, globalBlocks: Map<String, ParsedBlock>, codeBlocks: Map<String, ParsedBlock>, ignore: Boolean): List<DynamicNode> {
228+
protected open fun testFactory(title: String, globalBlocks: Map<String, List<ParsedBlock>>, codeBlocks: Map<String, List<ParsedBlock>>, ignore: Boolean): List<DynamicNode> {
223229
return emptyList()
224230
}
225231

226232
protected open fun schemaTestFactory(schema: String): List<DynamicNode> {
227233
return emptyList()
228234
}
229235

230-
protected fun getOrCreateBlock(codeBlocks: Map<String, ParsedBlock>, marker: String, headline: String): ParsedBlock? {
231-
var block = codeBlocks[marker]
232-
if (block == null && (GENERATE_TEST_FILE_DIFF || UPDATE_TEST_FILE)) {
236+
protected fun getOrCreateBlocks(codeBlocks: Map<String, List<ParsedBlock>>, marker: String, headline: String): List<ParsedBlock> {
237+
val blocks = codeBlocks[marker]?.toMutableList() ?: mutableListOf()
238+
if (blocks.isEmpty() && (GENERATE_TEST_FILE_DIFF || UPDATE_TEST_FILE)) {
233239
val insertPoints = testCaseMarkers.indexOf(marker).let { testCaseMarkers.subList(0, it).asReversed() }
234-
val insertPoint = insertPoints.mapNotNull { codeBlocks[it] }.firstOrNull()
235-
?: throw IllegalArgumentException("none of the insert points $insertPoints found")
236-
block = ParsedBlock(marker, insertPoint.uri, headline)
240+
val insertPoint = insertPoints.mapNotNull { codeBlocks[it]?.firstOrNull() }.firstOrNull()
241+
?: throw IllegalArgumentException("none of the insert points $insertPoints found in $fileName")
242+
val block = ParsedBlock(marker, insertPoint.uri, headline)
237243
block.start = (insertPoint.end ?: throw IllegalStateException("no start for block defined")) + 6
238-
knownBlocks += block
244+
knownBlocks += blocks
245+
blocks += block
239246
}
240-
return block
247+
return blocks
241248
}
242249

243250
companion object {

0 commit comments

Comments
 (0)