Skip to content

Commit

Permalink
Merge pull request #62 from aPureBase/new-parser
Browse files Browse the repository at this point in the history
New parser
  • Loading branch information
jeggy authored Nov 9, 2019
2 parents 7f21eb7 + e337ac7 commit 6f705f3
Show file tree
Hide file tree
Showing 140 changed files with 5,951 additions and 2,390 deletions.
6 changes: 3 additions & 3 deletions docs/content/docs/Getting Started/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ And make sure that you use the right Kotlin version:

```groovy
buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_version = '1.3.50'
}
```

Expand Down Expand Up @@ -55,6 +55,6 @@ Add dependency:

```xml
<properties>
<kotlin.version>1.3.0</kotlin.version>
<kotlin.version>1.3.50</kotlin.version>
</properties>
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ open class FunctionExecutionBenchmark {
setup()
println(benchmarkFunctionExecution())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.apurebase.kgraphql

import com.apurebase.kgraphql.schema.Schema
import org.junit.Test
import org.openjdk.jmh.annotations.*
import java.util.concurrent.ThreadLocalRandom
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.*
import org.junit.Test

@State(Scope.Benchmark)
@Warmup(iterations = 5)
Expand Down Expand Up @@ -45,7 +45,7 @@ open class ParallelExecutionBenchmark {
}

@Benchmark @BenchmarkMode(Mode.AverageTime)
fun queryBenchmark(): String = schema.execute( "{ " + (0..999).map { "automated-${it}" }.joinToString(", ") + " }" )
fun queryBenchmark(): String = schema.executeBlocking( "{ " + (0..999).map { "automated-${it}" }.joinToString(", ") + " }" )

@Test
fun benchmarkWithThreads(){
Expand All @@ -59,4 +59,4 @@ open class ParallelExecutionBenchmark {
setup()
println(queryBenchmark())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ open class SimpleExecutionOverheadBenchmark {
@Benchmark
fun benchmark(): String {
if(withKGraphQL){
return schema.execute("{one{name, quantity, active}, two(name : \"FELLA\"){range{start, endInclusive}}, three{id}}")
return schema.executeBlocking("{one{name, quantity, active}, two(name : \"FELLA\"){range{start, endInclusive}}, three{id}}")
} else {
return runBlocking {
": ${objectMapper.writeValueAsString(oneResolver())} " +
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/com/apurebase/kgraphql/ExecutionException.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
package com.apurebase.kgraphql

import com.apurebase.kgraphql.schema.execution.Execution
import com.apurebase.kgraphql.schema.model.ast.ASTNode

class ExecutionException(message: String, cause: Throwable? = null) : Exception(message, cause)
class ExecutionException(
message: String,
node: ASTNode? = null,
cause: Throwable? = null
) : GraphQLError(
message,
nodes = node?.let(::listOf),
originalError = cause
) {
constructor(message: String, node: Execution, cause: Throwable? = null): this(message, node.selectionNode, cause)
}
22 changes: 12 additions & 10 deletions src/main/kotlin/com/apurebase/kgraphql/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.jvmErasure

fun <T : Any> KClass<T>.defaultKQLTypeName() = this.simpleName!!
internal fun <T : Any> KClass<T>.defaultKQLTypeName() = this.simpleName!!

fun KType.defaultKQLTypeName() = this.jvmErasure.defaultKQLTypeName()
internal fun KType.defaultKQLTypeName() = this.jvmErasure.defaultKQLTypeName()

fun String.dropQuotes() : String = if(isLiteral()) drop(1).dropLast(1) else this
internal fun String.dropQuotes() : String = if(isLiteral()) drop(1).dropLast(1) else this

fun String.isLiteral() : Boolean = startsWith('\"') && endsWith('\"')
internal fun String.isLiteral() : Boolean = startsWith('\"') && endsWith('\"')

fun KParameter.isNullable() = type.isMarkedNullable
internal fun KParameter.isNullable() = type.isMarkedNullable

fun KParameter.isNotNullable() = !type.isMarkedNullable
internal fun KParameter.isNotNullable() = !type.isMarkedNullable

fun KClass<*>.isIterable() = isSubclassOf(Iterable::class)
internal fun KClass<*>.isIterable() = isSubclassOf(Iterable::class)

fun KType.isIterable() = jvmErasure.isIterable() || toString().startsWith("kotlin.Array")
internal fun KType.isIterable() = jvmErasure.isIterable() || toString().startsWith("kotlin.Array")

fun KType.getIterableElementType(): KType? {
internal fun KType.getIterableElementType(): KType? {
require(isIterable()) { "KType $this is not collection type" }
return arguments.firstOrNull()?.type ?: throw NoSuchElementException("KType $this has no type arguments")
}

fun not(boolean: Boolean) = !boolean

internal fun not(boolean: Boolean) = !boolean

74 changes: 74 additions & 0 deletions src/main/kotlin/com/apurebase/kgraphql/GraphQLError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.apurebase.kgraphql

import com.apurebase.kgraphql.schema.model.ast.ASTNode
import com.apurebase.kgraphql.schema.model.ast.Location.Companion.getLocation
import com.apurebase.kgraphql.schema.model.ast.Source

open class GraphQLError(

/**
* A message describing the Error for debugging purposes.
*/
message: String,

/**
* An array of GraphQL AST Nodes corresponding to this error.
*/
val nodes: List<ASTNode>? = null,

/**
* The source GraphQL document for the first location of this error.
*
* Note that if this Error represents more than one node, the source may not
* represent nodes after the first node.
*/
val source: Source? = null,

/**
* An array of character offsets within the source GraphQL document
* which correspond to this error.
*/
val positions: List<Int>? = null,

/**
* The original error thrown from a field resolver during execution.
*/
val originalError: Throwable? = null
) : Exception(message) {

constructor(message: String, node: ASTNode?) : this(message, nodes = node?.let(::listOf))

/**
* An array of { line, column } locations within the source GraphQL document
* that correspond to this error.
*
* Errors during validation often contain multiple locations, for example to
* point out two things with the same name. Errors during execution include a
* single location, the field that produced the error.
*/
val locations: List<Source.LocationSource>? by lazy {
if (positions != null && source != null) {
positions.map { pos -> getLocation(source, pos) }
} else nodes?.mapNotNull { node ->
node.loc?.let { getLocation(it.source, it.start) }
}
}

fun prettyPrint(): String {
var output = message ?: ""

if (nodes != null) {
for (node in nodes) {
if (node.loc != null) {
output += "\n\n" + node.loc!!.printLocation()
}
}
} else if (source != null && locations != null) {
for (location in locations!!) {
output += "\n\n" + source.print(location)
}
}

return output
}
}
4 changes: 0 additions & 4 deletions src/main/kotlin/com/apurebase/kgraphql/RequestException.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.apurebase.kgraphql

import com.apurebase.kgraphql.schema.model.ast.ASTNode

class ValidationException(message: String, cause: Throwable? = null) : RequestException(message, cause)
class ValidationException(message: String, nodes: List<ASTNode>? = null): GraphQLError(message, nodes = nodes)
11 changes: 0 additions & 11 deletions src/main/kotlin/com/apurebase/kgraphql/request/Arguments.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package com.apurebase.kgraphql.request

import com.apurebase.kgraphql.schema.model.ast.DocumentNode
import com.github.benmanes.caffeine.cache.Caffeine


class CachingDocumentParser(cacheMaximumSize : Long = 1000L) : DocumentParser() {
class CachingDocumentParser(cacheMaximumSize : Long = 1000L) {

sealed class Result {
class Success(val operations :List<Operation>) : Result()
class Success(val document: DocumentNode) : Result()
class Exception(val exception: kotlin.Exception) : Result()
}

val cache = Caffeine.newBuilder().maximumSize(cacheMaximumSize).build<String, Result>()

override fun parseDocument(input: String): List<Operation> {
val result = cache.get(input, {
fun parseDocument(input: String): DocumentNode {
val result = cache.get(input) {
val parser = Parser(input)
try {
Result.Success(super.parseDocument(input))
Result.Success(parser.parseDocument())
} catch ( e : Exception){
Result.Exception(e)
}
})
}

when (result) {
is Result.Success -> return result.operations
is Result.Success -> return result.document
is Result.Exception -> throw result.exception
else -> {
cache.invalidateAll()
throw IllegalStateException("Internal error of CachingDocumentParser")
}
}
}
}
}
46 changes: 0 additions & 46 deletions src/main/kotlin/com/apurebase/kgraphql/request/Document.kt

This file was deleted.

Loading

0 comments on commit 6f705f3

Please sign in to comment.