Skip to content

Update dependency com.graphql-java:graphql-java to v21 #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor parseDocuments
  • Loading branch information
oryan-block committed Jul 28, 2023
commit c0e533db9912dbf8aaef687e2fa5735090d27138
49 changes: 27 additions & 22 deletions src/main/kotlin/graphql/kickstart/tools/SchemaParserBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class SchemaParserBuilder {
private val scalars = mutableListOf<GraphQLScalarType>()
private val runtimeWiringBuilder = RuntimeWiring.newRuntimeWiring()
private var options = SchemaParserOptions.defaultOptions()
private val parser = Parser()
private val parserOptions = ParserOptions
.getDefaultParserOptions()
.transform { o -> o.maxTokens(MAX_VALUE) }

/**
* Add GraphQL schema files from the classpath.
Expand Down Expand Up @@ -167,24 +171,14 @@ class SchemaParserBuilder {
}

private fun parseDocuments(): List<Document> {
val parser = Parser()
val documents = mutableListOf<Document>()
try {
val options = ParserOptions
.getDefaultParserOptions()
.transform { o -> o.maxTokens(MAX_VALUE) }

files.forEach {
val sourceReader = MultiSourceReader.newMultiSourceReader().string(readFile(it), it).trackData(true).build()
val environment = ParserEnvironment.newParserEnvironment().document(sourceReader).parserOptions(options).build()
documents.add(parser.parseDocument(environment))
}
val documents = files.map { parseDocument(readFile(it), it) }.toMutableList()

if (schemaString.isNotEmpty()) {
val sourceReader = MultiSourceReader.newMultiSourceReader().string(schemaString.toString(), null).trackData(true).build()
val environment = ParserEnvironment.newParserEnvironment().document(sourceReader).parserOptions(options).build()
documents.add(parser.parseDocument(environment))
if (schemaString.isNotBlank()) {
documents.add(parseDocument(schemaString.toString()))
}

return documents
} catch (pce: ParseCancellationException) {
val cause = pce.cause
if (cause != null && cause is RecognitionException) {
Expand All @@ -193,23 +187,34 @@ class SchemaParserBuilder {
throw pce
}
}
return documents
}

private fun readFile(filename: String): String {
return java.io.BufferedReader(java.io.InputStreamReader(
object : Any() {}.javaClass.classLoader.getResourceAsStream(filename)
?: throw java.io.FileNotFoundException("classpath:$filename")
)).readText()
private fun parseDocument(input: String, sourceName: String? = null): Document {
val sourceReader = MultiSourceReader
.newMultiSourceReader()
.string(input, sourceName)
.trackData(true).build()
val environment = ParserEnvironment
.newParserEnvironment()
.document(sourceReader)
.parserOptions(parserOptions).build()
return parser.parseDocument(environment)
}

private fun readFile(filename: String) =
this::class.java.classLoader.getResource(filename)?.readText()
?: throw java.io.FileNotFoundException("classpath:$filename")

/**
* Build the parser with the supplied schema and dictionary.
*/
fun build() = SchemaParser(scan(), options, runtimeWiringBuilder.build())
}

class InvalidSchemaError(pce: ParseCancellationException, private val recognitionException: RecognitionException) : RuntimeException(pce) {
class InvalidSchemaError(
pce: ParseCancellationException,
private val recognitionException: RecognitionException
) : RuntimeException(pce) {
override val message: String
get() = "Invalid schema provided (${recognitionException.javaClass.name}) at: ${recognitionException.offendingToken}"
}