JVM Library to translate GraphQL queries and mutations to Neo4j's Cypher
This is a beta GraphQL transpiler written in Kotlin.
License: Apache 2.
How does it work
- parses a GraphQL schema and
- uses the information of the annotated schema to translate GraphQL queries and parameters into Cypher queries and parameters.
Those Cypher queries can then executed, e.g via the Neo4j-Java-Driver (or other JVM drivers) against the graph database and the results can be returned directly to the caller.
The request, result and error handling is not part of this library, but we provide demo programs on how to use it in different languages.
NOTE: All the supported features are listed and explained below, more detailed docs will be added in time.
How does this relate to the other neo4j graphql libraries?
neo4j-graphql-js this library focuses on query translation, just for the JVM instead of Node.js.
It does not provide a server (except as examples) or other facilities but is meant to be used as a dependency included for a single purpose.
If this library is feature complete we plan to replace the code in the current Neo4j server plugin
neo4j-graphql with a single call to this library.
The server plugin would still handle request-response and error-handling, and perhaps some schema management but be slimmed down to a tiny piece.
How does this related to graphql-java
This library uses
graphql-java under the hood for parsing of schema and queries, and managing the GraphQL state and context.
But not for nested field resolvers or data fetching.
If you wanted, you could combine
graphql-java resolvers with this library to have a part of your schema handled by Neo4j.
Thanks a lot to the maintainers of
graphql-java for the awesome library.
You can use the library as dependency:
org.neo4j:neo4j-graphql-java:1.0.0-M03 in any JVM program.
The basic usage should be:
You find more usage examples in the TCK scripts.
Here is a minimalistic example in Groovy using the Neo4j-Java driver and Spark-Java as webserver.
It is running against a Neo4j instance at
password) containing the
:play movies graph.
(You can also use a Kotlin based server example.)
Run the example with:
and use http://localhost:4567/graphql as your GraphQL URL.
It uses a schema of:
And can run queries like:
You can also test it with
This example doesn't handle introspection queries but the one in the test directory does.
Filter, Sorting, Paging support
- parse SDL schema
- resolve query fields via result types
- handle arguments as equality comparisons for top level and nested fields
- handle relationships via @relation directive on schema fields
- @relation directive on types for rich relationships (from, to fields for start & end node)
- handle first, offset arguments
- argument types: string, int, float, array
- request parameter support
- parametrization for cypher query
- inline and named fragments
- auto-generate query fields for all objects
- @cypher directive for fields to compute field values, support arguments
- auto-generate mutation fields for all objects to create, update, delete
- @cypher directive for top level queries and mutations, supports arguments
- skip limit params
- sorting (nested)
- input types
- date(time), spatial
Parse SDL schema
Currently schemas with object types, enums, fragments and Query types are parsed and handled. We support @relation directives on fields and types for rich relationships We support @cypher directives on fields and top-level query and mutation fields. The configurable augmentation auto-generates queries and mutations (create,update,delete) for all types. It supports the built-in scalars for GraphQL. For arguments we support input types in many places and filters as known from GraphCool/Prisma.
Resolve query Fields via Result Types
For query fields that result in object types (even if wrapped in list/non-null), the appropriate object type is found in the schema and used to translate the query.
Handle Arguments as Equality Comparisons for Top Level and Nested Fields
If you add a simple argument to your top-level query or nested related fields, those will be translated to direct equality comparisons.
Only that the literal values are turned into parameters.
Handle Relationships via @relation Directive on Schema Fields
If you want to represent a relationship from the graph in GraphQL you have to add an
@relation directive that contains the relationship-type and the direction.
Default relationship-type is 'OUT'.
So you can use different domain names in your GraphQL fields that are independent of your graph model.
NOTE: We use Neo4j's pattern comprehensions to represent nested graph patterns in Cypher.
Handle first, offset Arguments
To support pagination
first is translated to
LIMIT in Cypher and
For nested queries these are converted into slices for arrays.
Argument Types: string, int, float, array
The default Neo4j types are handled both as argument types as well as field types.
NOTE: Datetime and spatial not yet.
We handle passed in GraphQL parameters, these are resolved correctly when used within the GraphQL query.
As we don't want to have literal values in our Cypher queries, all of them are translated into parameters.
Those parameters are returned as part of the
Cypher type that's returned from the
We support query aliases, they are used as Cypher aliases too, so you get them back as keys in your result records.
Inline and Named Fragments
This is more of a technical feature, both types of fragments are resolved internally.
We support sorting via an
orderBy argument, which takes an Enum or String value of
NOTE: Those enums are not yet automatically generated. And we don't support ordering yet on nested, related fields.
@relationship on Types
To represent rich relationship types with properties, a
@relation directive is supported on an object type.
In our example it would be the
Filters are a powerful way of selecting a subset of data. Inspired by the graph.cool/Prisma filter approach, our filters work the same way.
NOTE: we'll create more detailed docs, for now the prisma docs on that topic are pretty good.
We use nested input types for arbitrary filtering on query types and fields
You can also apply nested filter on relations, which use suffixes like
("",not,some, none, single, every)
NOTE: Those nested input types are not yet generated, we use leniency in the parser.
Inline and Named Fragments
We support inline and named fragments according to the GraphQL spec. Most of this is resolved on the parser/query side.
@cypher directives you can add the power of Cypher to your GraphQL API.
It allows you, without code to compute field values using complex queries.
You can also write your own, custom top-level queries and mutations using Cypher.
Arguments on the field are passed to the Cypher statement as parameters.
Input types are supported, they appear as
Map type in your Cypher statement.
NOTE: Those Cypher directive queries are only included in the generated Cypher statement if the field or query is included in the GraphQL query.
@cypher directive on a field
this variable is bound to the current movie and you can use it to navigate the graph and collect data.
limit variable is passed to the query as parameter.
Similarly you can use the
@cypher directive with a top level query.
@cypher directive on query
Of course you can also return arrays from your query, the statements on query fields should be read-only queries.
You can do the same for mutations, just with updating Cypher statements.
@cypher directive on mutation
You can use more complex statements for creating these entities or even subgraphs.
NOTE: The common CRUD mutations and queries are auto-generated, see below.
Auto Generate Queries and Mutations
To reduce the amount of boilerplate code a user has to write we auto-generate top-level CRUD queries and mutations for all types.
This is configurable via the API, you can:
- disable auto-generation (for mutations/queries)
- disable it per type
- disable mutations per operation (create,delete,update)
For a schema like this:
It would auto-generate quite a lot of things:
- a query:
person(id:ID, name:String , age: Int, _id: Int, filter:_PersonFilter, orderBy:_PersonOrdering, first:Int, offset:Int) : [Person]
_PersonOrderingenum, for the
orderByargument with all fields for
_PersonInputfor creating Person objects
filterargument, which is a deeply nested input object
- mutations for:
createPerson(id:ID!, name:String, age: Int) : PersonmergePerson:
mergePerson(id:ID!, name:String, age:Int) : PersonupdatePerson:
updatePerson(id:ID!, name:String, age:Int) : PersondeletePerson:
deletePerson(id:ID!) : PersonaddPersonMovies:
addPersonMovies(id:ID!,movies:[ID!]!) : PersondeletePersonMovies:
deletePersonMovies(id:ID!,movies:[ID!]!) : Person
You can then use those in your GraphQL queries like this: