@@ -4,6 +4,7 @@ import graphql.language.Definition
44import graphql.language.Document
55import graphql.parser.MultiSourceReader
66import graphql.parser.Parser
7+ import graphql.parser.ParserEnvironment
78import graphql.parser.ParserOptions
89import graphql.schema.GraphQLScalarType
910import graphql.schema.idl.RuntimeWiring
@@ -25,6 +26,10 @@ class SchemaParserBuilder {
2526 private val scalars = mutableListOf<GraphQLScalarType >()
2627 private val runtimeWiringBuilder = RuntimeWiring .newRuntimeWiring()
2728 private var options = SchemaParserOptions .defaultOptions()
29+ private val parser = Parser ()
30+ private val parserOptions = ParserOptions
31+ .getDefaultParserOptions()
32+ .transform { o -> o.maxTokens(MAX_VALUE ) }
2833
2934 /* *
3035 * Add GraphQL schema files from the classpath.
@@ -166,21 +171,14 @@ class SchemaParserBuilder {
166171 }
167172
168173 private fun parseDocuments (): List <Document > {
169- val parser = Parser ()
170- val documents = mutableListOf<Document >()
171174 try {
172- val options = ParserOptions
173- .getDefaultParserOptions()
174- .transform { o -> o.maxTokens(MAX_VALUE ) }
175+ val documents = files.map { parseDocument(readFile(it), it) }.toMutableList()
175176
176- files.forEach {
177- val sourceReader = MultiSourceReader .newMultiSourceReader().string(readFile(it), it).trackData(true ).build()
178- documents.add(parser.parseDocument(sourceReader, options))
177+ if (schemaString.isNotBlank()) {
178+ documents.add(parseDocument(schemaString.toString()))
179179 }
180180
181- if (schemaString.isNotEmpty()) {
182- documents.add(parser.parseDocument(schemaString.toString(), options))
183- }
181+ return documents
184182 } catch (pce: ParseCancellationException ) {
185183 val cause = pce.cause
186184 if (cause != null && cause is RecognitionException ) {
@@ -189,23 +187,34 @@ class SchemaParserBuilder {
189187 throw pce
190188 }
191189 }
192- return documents
193190 }
194191
195- private fun readFile (filename : String ): String {
196- return java.io.BufferedReader (java.io.InputStreamReader (
197- object : Any () {}.javaClass.classLoader.getResourceAsStream(filename)
198- ? : throw java.io.FileNotFoundException (" classpath:$filename " )
199- )).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)
200202 }
201203
204+ private fun readFile (filename : String ) =
205+ this ::class .java.classLoader.getResource(filename)?.readText()
206+ ? : throw java.io.FileNotFoundException (" classpath:$filename " )
207+
202208 /* *
203209 * Build the parser with the supplied schema and dictionary.
204210 */
205211 fun build () = SchemaParser (scan(), options, runtimeWiringBuilder.build())
206212}
207213
208- class InvalidSchemaError (pce : ParseCancellationException , private val recognitionException : RecognitionException ) : RuntimeException(pce) {
209- override val message: String?
214+ class InvalidSchemaError (
215+ pce : ParseCancellationException ,
216+ private val recognitionException : RecognitionException
217+ ) : RuntimeException(pce) {
218+ override val message: String
210219 get() = " Invalid schema provided (${recognitionException.javaClass.name} ) at: ${recognitionException.offendingToken} "
211220}
0 commit comments