@@ -26,6 +26,10 @@ class SchemaParserBuilder {
26
26
private val scalars = mutableListOf<GraphQLScalarType >()
27
27
private val runtimeWiringBuilder = RuntimeWiring .newRuntimeWiring()
28
28
private var options = SchemaParserOptions .defaultOptions()
29
+ private val parser = Parser ()
30
+ private val parserOptions = ParserOptions
31
+ .getDefaultParserOptions()
32
+ .transform { o -> o.maxTokens(MAX_VALUE ) }
29
33
30
34
/* *
31
35
* Add GraphQL schema files from the classpath.
@@ -167,24 +171,14 @@ class SchemaParserBuilder {
167
171
}
168
172
169
173
private fun parseDocuments (): List <Document > {
170
- val parser = Parser ()
171
- val documents = mutableListOf<Document >()
172
174
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()
182
176
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()))
187
179
}
180
+
181
+ return documents
188
182
} catch (pce: ParseCancellationException ) {
189
183
val cause = pce.cause
190
184
if (cause != null && cause is RecognitionException ) {
@@ -193,23 +187,34 @@ class SchemaParserBuilder {
193
187
throw pce
194
188
}
195
189
}
196
- return documents
197
190
}
198
191
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)
204
202
}
205
203
204
+ private fun readFile (filename : String ) =
205
+ this ::class .java.classLoader.getResource(filename)?.readText()
206
+ ? : throw java.io.FileNotFoundException (" classpath:$filename " )
207
+
206
208
/* *
207
209
* Build the parser with the supplied schema and dictionary.
208
210
*/
209
211
fun build () = SchemaParser (scan(), options, runtimeWiringBuilder.build())
210
212
}
211
213
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) {
213
218
override val message: String
214
219
get() = " Invalid schema provided (${recognitionException.javaClass.name} ) at: ${recognitionException.offendingToken} "
215
220
}
0 commit comments