Java query builder
The Java query builder library can be used to programmatically construct TypeQL queries with native Java code.
To use TypeQL, we first add it as a dependency to our pom.xml
.
Don’t forget to replace the |
The latest version of typeql-lang
can be found in the
Vaticle’s public Maven repository.
<repositories>
<repository>
<id>repo.vaticle.com</id>
<url>https://repo.vaticle.com/repository/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.vaticle.typeql</groupId>
<artifactId>typeql-lang</artifactId>
<version>{version}</version>
</dependency>
</dependencies>
Then we import TypeQL
.
import com.vaticle.typeql.lang.TypeQL;
We are now ready to construct TypeQL queries, using the methods available on the TypeQL
class.
Examples
Using the Java query builder library is quite simple as it produces a TypeQL string.
See the examples below.
Example 1: A get query
TypeQLMatch.Filtered getQuery = TypeQL.match(
cVar("u").isa("user").has("full-name", "Kevin Morrison"),
cVar("p").rel(cVar("u")).rel(cVar("pa")).isa("permission"),
cVar("o").isa("object").has("path", cVar("fp")),
cVar("pa").rel(cVar("o")).rel(cVar("va")).isa("access")
).get(cVar("fp"));
As the result of the above example we should get a TypeQL query in a getQuery
variable that can be used for a
get query like in the following line.
Since version In version prior to |
readTransaction.query().match(getQuery)
The result should be the same as if we set getQuery
variable as a TypeQL string.
String getQuery = "match $u isa user, has full-name 'Kevin Morrison'; $p($u, $pa) isa permission; " +
"$o isa object, has path $fp; $pa($o, $va) isa access; get $fp;";
Example 2: A get query with additional parameters
The following example showcases the usage of sorting, offsetting and limiting a get query.
TypeQLMatch.Limited getQuery = TypeQL.match(
cVar("u").isa("user").has("full-name", "Kevin Morrison"),
cVar("p").rel(cVar("u")).rel(cVar("pa")).isa("permission"),
cVar("o").isa("object").has("path", cVar("fp")),
cVar("pa").rel(cVar("o")).rel(cVar("va")).isa("access"),
cVar("va").isa("action").has("name", "view_file")
).get(cVar("fp")).sort(cVar("fp")).offset(0).limit(5);