Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions docs/Server.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Grapes([
@Grab('com.sparkjava:spark-core:2.7.2'),
@Grab('org.neo4j.driver:neo4j-java-driver:1.7.2'),
@Grab('org.neo4j:neo4j-graphql-java:1.0.0-M01'),
@Grab('org.neo4j:neo4j-graphql-java:1.0.0'),
@Grab('com.google.code.gson:gson:2.8.5')
])

Expand All @@ -14,12 +14,12 @@ import org.neo4j.driver.v1.*

schema = """
type Person {
name: String
name: ID!
born: Int
actedIn: [Movie] @relation(name:"ACTED_IN")
}
type Movie {
title: String
title: ID!
released: Int
tagline: String
}
Expand Down
34 changes: 30 additions & 4 deletions readme.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
= JVM Library to translate GraphQL queries and mutations to Neo4j's Cypher
:version: 1.0.0

This is a beta GraphQL transpiler written in Kotlin.

Expand Down Expand Up @@ -38,7 +39,7 @@ Thanks a lot to the maintainers of `graphql-java` for the awesome library.

== Usage

You can use the library as dependency: `org.neo4j:neo4j-graphql-java:1.0.0-M02` in any JVM program.
You can use the library as dependency: `org.neo4j:neo4j-graphql-java:{version}` in any JVM program.

The basic usage should be:

Expand All @@ -59,7 +60,10 @@ val schema =
val query = """ { p:personByName(name:"Joe") { age } } """

val schema = SchemaBuilder.buildSchema(idl)
val (cypher, params) = Translator(schema).translate(query, params)
val ctx = QueryContext()
// SETUP otimizer strategy
// ctx.optimizedQuery = setOf(QueryContext.OptimizationStrategy.FILTER_AS_MATCH)
val (cypher, params) = Translator(schema).translate(query, params, ctx)

// generated Cypher
cypher == "MATCH (p:Person) WHERE p.name = $pName RETURN p {.age} as p"
Expand All @@ -74,6 +78,7 @@ You find more usage examples in the:
* link:src/test/resources/translator-tests1.adoc[Translator 1 TCK]
* link:src/test/resources/translator-tests2.adoc[Translator 2 TCK]
* link:src/test/resources/translator-tests3.adoc[Translator 3 TCK]
* link:src/test/resources/optimized-query-for-filter.adoc[Alternative Filter TCK]

== Demo

Expand All @@ -86,13 +91,13 @@ In case you wand to bind the neo4j driver directly to the graphql schema you can
link:src/test/kotlin/DataFetcherInterceptorDemo.kt[use the DataFetchingInterceptor to
intercept the cypher queries].

[source,groovy]
[source,groovy,subs=attributes]
----
// Simplistic GraphQL Server using SparkJava
@Grapes([
@Grab('com.sparkjava:spark-core:2.7.2'),
@Grab('org.neo4j.driver:neo4j-java-driver:1.7.2'),
@Grab('org.neo4j:neo4j-graphql-java:1.0.0-M01'),
@Grab('org.neo4j:neo4j-graphql-java:{version}'),
@Grab('com.google.code.gson:gson:2.8.5')
])

Expand Down Expand Up @@ -518,6 +523,27 @@ You can also apply nested filter on relations, which use suffixes like `("",not,

NOTE: Those nested input types are not yet generated, we use leniency in the parser.

==== Optimized Filters

If you encounter performance problems with the cypher queries generated for the filter,
you can activate an alternative algorithm using:

[source,kotlin]
----
var query
try {
val ctx = QueryContext(optimizedQuery = setOf(QueryContext.OptimizationStrategy.FILTER_AS_MATCH))
query = translator.translate(query, params, ctx)
} catch (e: OptimizedQueryException) {
query = translator.translate(query, params)
}
----

If no query can be generated by the alternative algorithm, an `OptimizedQueryException` is thrown,
so that a fallback to the actual algorithm can used.

link:src/test/resources/optimized-query-for-filter.adoc[Examples of the alternative algorithm] can be seen in the tests.

=== Inline and Named Fragments

We support inline and named fragments according to the GraphQL spec.
Expand Down
12 changes: 7 additions & 5 deletions src/test/kotlin/GraphQLServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import graphql.GraphQL
import org.neo4j.driver.v1.AuthTokens
import org.neo4j.driver.v1.GraphDatabase
import org.neo4j.driver.v1.Values
import org.neo4j.graphql.Cypher
import org.neo4j.graphql.SchemaBuilder
import org.neo4j.graphql.Translator
import org.neo4j.graphql.isList
import org.neo4j.graphql.*
import spark.Request
import spark.Response
import spark.Spark
Expand Down Expand Up @@ -49,7 +46,12 @@ fun main() {
println(graphQLSchema)
val schema = GraphQL.newGraphQL(graphQLSchema).build()
val translator = Translator(graphQLSchema)
fun translate(query: String, params: Map<String, Any?>) = translator.translate(query, params)
fun translate(query: String, params: Map<String, Any?>) = try {
val ctx = QueryContext(optimizedQuery = setOf(QueryContext.OptimizationStrategy.FILTER_AS_MATCH))
translator.translate(query, params, ctx)
} catch (e: OptimizedQueryException) {
translator.translate(query, params)
}

val driver = GraphDatabase.driver("bolt://localhost", AuthTokens.basic("neo4j", "test"))
fun run(cypher: Cypher) = driver.session().use {
Expand Down