Skip to content

Commit 78dbd7a

Browse files
Andy2003jexp
andauthored
Update doc to reflect alternative filters (#81)
* Update doc to reflect alternative filters Co-authored-by: Michael Hunger <github@jexp.de>
1 parent eea6163 commit 78dbd7a

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

docs/Server.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@Grapes([
33
@Grab('com.sparkjava:spark-core:2.7.2'),
44
@Grab('org.neo4j.driver:neo4j-java-driver:1.7.2'),
5-
@Grab('org.neo4j:neo4j-graphql-java:1.0.0-M01'),
5+
@Grab('org.neo4j:neo4j-graphql-java:1.0.0'),
66
@Grab('com.google.code.gson:gson:2.8.5')
77
])
88

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

1515
schema = """
1616
type Person {
17-
name: String
17+
name: ID!
1818
born: Int
1919
actedIn: [Movie] @relation(name:"ACTED_IN")
2020
}
2121
type Movie {
22-
title: String
22+
title: ID!
2323
released: Int
2424
tagline: String
2525
}

readme.adoc

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
= JVM Library to translate GraphQL queries and mutations to Neo4j's Cypher
2+
:version: 1.0.0
23

34
This is a beta GraphQL transpiler written in Kotlin.
45

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

3940
== Usage
4041

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

4344
The basic usage should be:
4445

@@ -59,7 +60,10 @@ val schema =
5960
val query = """ { p:personByName(name:"Joe") { age } } """
6061
6162
val schema = SchemaBuilder.buildSchema(idl)
62-
val (cypher, params) = Translator(schema).translate(query, params)
63+
val ctx = QueryContext()
64+
// SETUP otimizer strategy
65+
// ctx.optimizedQuery = setOf(QueryContext.OptimizationStrategy.FILTER_AS_MATCH)
66+
val (cypher, params) = Translator(schema).translate(query, params, ctx)
6367
6468
// generated Cypher
6569
cypher == "MATCH (p:Person) WHERE p.name = $pName RETURN p {.age} as p"
@@ -74,6 +78,7 @@ You find more usage examples in the:
7478
* link:src/test/resources/translator-tests1.adoc[Translator 1 TCK]
7579
* link:src/test/resources/translator-tests2.adoc[Translator 2 TCK]
7680
* link:src/test/resources/translator-tests3.adoc[Translator 3 TCK]
81+
* link:src/test/resources/optimized-query-for-filter.adoc[Alternative Filter TCK]
7782

7883
== Demo
7984

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

89-
[source,groovy]
94+
[source,groovy,subs=attributes]
9095
----
9196
// Simplistic GraphQL Server using SparkJava
9297
@Grapes([
9398
@Grab('com.sparkjava:spark-core:2.7.2'),
9499
@Grab('org.neo4j.driver:neo4j-java-driver:1.7.2'),
95-
@Grab('org.neo4j:neo4j-graphql-java:1.0.0-M01'),
100+
@Grab('org.neo4j:neo4j-graphql-java:{version}'),
96101
@Grab('com.google.code.gson:gson:2.8.5')
97102
])
98103
@@ -518,6 +523,27 @@ You can also apply nested filter on relations, which use suffixes like `("",not,
518523

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

526+
==== Optimized Filters
527+
528+
If you encounter performance problems with the cypher queries generated for the filter,
529+
you can activate an alternative algorithm using:
530+
531+
[source,kotlin]
532+
----
533+
var query
534+
try {
535+
val ctx = QueryContext(optimizedQuery = setOf(QueryContext.OptimizationStrategy.FILTER_AS_MATCH))
536+
query = translator.translate(query, params, ctx)
537+
} catch (e: OptimizedQueryException) {
538+
query = translator.translate(query, params)
539+
}
540+
----
541+
542+
If no query can be generated by the alternative algorithm, an `OptimizedQueryException` is thrown,
543+
so that a fallback to the actual algorithm can used.
544+
545+
link:src/test/resources/optimized-query-for-filter.adoc[Examples of the alternative algorithm] can be seen in the tests.
546+
521547
=== Inline and Named Fragments
522548

523549
We support inline and named fragments according to the GraphQL spec.

src/test/kotlin/GraphQLServer.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import graphql.GraphQL
77
import org.neo4j.driver.v1.AuthTokens
88
import org.neo4j.driver.v1.GraphDatabase
99
import org.neo4j.driver.v1.Values
10-
import org.neo4j.graphql.Cypher
11-
import org.neo4j.graphql.SchemaBuilder
12-
import org.neo4j.graphql.Translator
13-
import org.neo4j.graphql.isList
10+
import org.neo4j.graphql.*
1411
import spark.Request
1512
import spark.Response
1613
import spark.Spark
@@ -49,7 +46,12 @@ fun main() {
4946
println(graphQLSchema)
5047
val schema = GraphQL.newGraphQL(graphQLSchema).build()
5148
val translator = Translator(graphQLSchema)
52-
fun translate(query: String, params: Map<String, Any?>) = translator.translate(query, params)
49+
fun translate(query: String, params: Map<String, Any?>) = try {
50+
val ctx = QueryContext(optimizedQuery = setOf(QueryContext.OptimizationStrategy.FILTER_AS_MATCH))
51+
translator.translate(query, params, ctx)
52+
} catch (e: OptimizedQueryException) {
53+
translator.translate(query, params)
54+
}
5355

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

0 commit comments

Comments
 (0)