Skip to content

Commit c0e533d

Browse files
committed
Refactor parseDocuments
1 parent 4deb417 commit c0e533d

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/main/kotlin/graphql/kickstart/tools/SchemaParserBuilder.kt

+27-22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class SchemaParserBuilder {
2626
private val scalars = mutableListOf<GraphQLScalarType>()
2727
private val runtimeWiringBuilder = RuntimeWiring.newRuntimeWiring()
2828
private var options = SchemaParserOptions.defaultOptions()
29+
private val parser = Parser()
30+
private val parserOptions = ParserOptions
31+
.getDefaultParserOptions()
32+
.transform { o -> o.maxTokens(MAX_VALUE) }
2933

3034
/**
3135
* Add GraphQL schema files from the classpath.
@@ -167,24 +171,14 @@ class SchemaParserBuilder {
167171
}
168172

169173
private fun parseDocuments(): List<Document> {
170-
val parser = Parser()
171-
val documents = mutableListOf<Document>()
172174
try {
173-
val options = ParserOptions
174-
.getDefaultParserOptions()
175-
.transform { o -> o.maxTokens(MAX_VALUE) }
176-
177-
files.forEach {
178-
val sourceReader = MultiSourceReader.newMultiSourceReader().string(readFile(it), it).trackData(true).build()
179-
val environment = ParserEnvironment.newParserEnvironment().document(sourceReader).parserOptions(options).build()
180-
documents.add(parser.parseDocument(environment))
181-
}
175+
val documents = files.map { parseDocument(readFile(it), it) }.toMutableList()
182176

183-
if (schemaString.isNotEmpty()) {
184-
val sourceReader = MultiSourceReader.newMultiSourceReader().string(schemaString.toString(), null).trackData(true).build()
185-
val environment = ParserEnvironment.newParserEnvironment().document(sourceReader).parserOptions(options).build()
186-
documents.add(parser.parseDocument(environment))
177+
if (schemaString.isNotBlank()) {
178+
documents.add(parseDocument(schemaString.toString()))
187179
}
180+
181+
return documents
188182
} catch (pce: ParseCancellationException) {
189183
val cause = pce.cause
190184
if (cause != null && cause is RecognitionException) {
@@ -193,23 +187,34 @@ class SchemaParserBuilder {
193187
throw pce
194188
}
195189
}
196-
return documents
197190
}
198191

199-
private fun readFile(filename: String): String {
200-
return java.io.BufferedReader(java.io.InputStreamReader(
201-
object : Any() {}.javaClass.classLoader.getResourceAsStream(filename)
202-
?: throw java.io.FileNotFoundException("classpath:$filename")
203-
)).readText()
192+
private fun parseDocument(input: String, sourceName: String? = null): Document {
193+
val sourceReader = MultiSourceReader
194+
.newMultiSourceReader()
195+
.string(input, sourceName)
196+
.trackData(true).build()
197+
val environment = ParserEnvironment
198+
.newParserEnvironment()
199+
.document(sourceReader)
200+
.parserOptions(parserOptions).build()
201+
return parser.parseDocument(environment)
204202
}
205203

204+
private fun readFile(filename: String) =
205+
this::class.java.classLoader.getResource(filename)?.readText()
206+
?: throw java.io.FileNotFoundException("classpath:$filename")
207+
206208
/**
207209
* Build the parser with the supplied schema and dictionary.
208210
*/
209211
fun build() = SchemaParser(scan(), options, runtimeWiringBuilder.build())
210212
}
211213

212-
class InvalidSchemaError(pce: ParseCancellationException, private val recognitionException: RecognitionException) : RuntimeException(pce) {
214+
class InvalidSchemaError(
215+
pce: ParseCancellationException,
216+
private val recognitionException: RecognitionException
217+
) : RuntimeException(pce) {
213218
override val message: String
214219
get() = "Invalid schema provided (${recognitionException.javaClass.name}) at: ${recognitionException.offendingToken}"
215220
}

0 commit comments

Comments
 (0)