@@ -34,10 +34,12 @@ import com.netflix.graphql.dgs.codegen.generators.shared.excludeSchemaTypeExtens
3434import com.squareup.javapoet.JavaFile
3535import com.squareup.kotlinpoet.FileSpec
3636import graphql.language.*
37+ import graphql.parser.InvalidSyntaxException
3738import graphql.parser.MultiSourceReader
3839import graphql.parser.Parser
3940import graphql.parser.ParserOptions
4041import java.io.File
42+ import java.io.Reader
4143import java.nio.file.Path
4244import java.nio.file.Paths
4345
@@ -97,28 +99,41 @@ class CodeGen(private val config: CodeGenConfig) {
9799 builder.maxTokens(SDL_MAX_ALLOWED_SCHEMA_TOKENS )
98100 }
99101 val parser = Parser ()
100- val readerBuilder = MultiSourceReader .newMultiSourceReader()
101-
102- val schemaFiles = config.schemaFiles.sorted()
103- .flatMap { it.walkTopDown() }
104- .filter { it.isFile }
105102
106- for (schemaFile in schemaFiles) {
107- schemaFile.appendText(" \n " )
108- readerBuilder.reader(schemaFile.reader(), schemaFile.name)
109- }
110-
111- for (schema in config.schemas) {
112- readerBuilder.string(schema, null )
113- }
103+ val readerBuilder = MultiSourceReader .newMultiSourceReader()
104+ val debugReaderBuilder = MultiSourceReader .newMultiSourceReader()
114105
106+ loadSchemaReaders(readerBuilder, debugReaderBuilder)
115107 val document = readerBuilder.build().use { reader ->
116- parser.parseDocument(reader, options)
108+ try {
109+ parser.parseDocument(reader, options)
110+ } catch (exception: InvalidSyntaxException ) {
111+ throw CodeGenSchemaParsingException (debugReaderBuilder.build(), exception)
112+ }
117113 }
118114
119115 return document
120116 }
121117
118+ /* *
119+ * Loads the given [MultiSourceReader.Builder] references with the sources that will be used to provide
120+ * the schema information for the parser.
121+ */
122+ private fun loadSchemaReaders (vararg readerBuilders : MultiSourceReader .Builder ) {
123+ readerBuilders.forEach { rb ->
124+ val schemaFiles = config.schemaFiles.sorted()
125+ .flatMap { it.walkTopDown() }
126+ .filter { it.isFile }
127+ for (schemaFile in schemaFiles) {
128+ rb.string(" \n " , " codegen" )
129+ rb.reader(schemaFile.reader(), schemaFile.name)
130+ }
131+ for (schema in config.schemas) {
132+ rb.string(schema, null )
133+ }
134+ }
135+ }
136+
122137 private fun generateJava (): CodeGenResult {
123138 val definitions = document.definitions
124139 // data types
@@ -618,3 +633,30 @@ fun Type<*>.findBaseTypeDefinition(): TypeDefinition<*>? {
618633 }
619634 }
620635}
636+
637+ class CodeGenSchemaParsingException (
638+ schemaReader : Reader ,
639+ invalidSyntaxException : InvalidSyntaxException
640+ ) : RuntimeException(buildMessage(schemaReader, invalidSyntaxException), invalidSyntaxException) {
641+ companion object {
642+ private fun buildMessage (
643+ schemaReader : Reader ,
644+ invalidSyntaxException : InvalidSyntaxException
645+ ): String {
646+ schemaReader.use { reader ->
647+ return """
648+ |Unable to parse the schema...
649+ |${invalidSyntaxException.message}
650+ |
651+ |Schema Section:
652+ |>>>
653+ |${invalidSyntaxException.sourcePreview}
654+ |<<<
655+ |
656+ |Full Schema:
657+ |${reader.readText()}
658+ """ .trimMargin()
659+ }
660+ }
661+ }
662+ }
0 commit comments