Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meistermeier/spring-graphql-neo4j
Example project using Spring Data Neo4j and Spring GraphQL
https://github.com/meistermeier/spring-graphql-neo4j
java spring-boot spring-graphql
Last synced: 2 months ago
JSON representation
Example project using Spring Data Neo4j and Spring GraphQL
- Host: GitHub
- URL: https://github.com/meistermeier/spring-graphql-neo4j
- Owner: meistermeier
- Created: 2021-07-30T13:27:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-24T07:32:44.000Z (8 months ago)
- Last Synced: 2024-10-04T16:17:13.766Z (3 months ago)
- Topics: java, spring-boot, spring-graphql
- Language: Java
- Homepage:
- Size: 315 KB
- Stars: 13
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
Awesome Lists containing this project
README
== Spring GraphQL with Spring Data Neo4j
This example project shows how to combine https://docs.spring.io/spring-graphql/docs/1.2.0/reference/html/[Spring GraphQL 1.2.0] with https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#reference[Spring Data Neo4j 7.1.0].
The project itself is based on Spring Boot 3.1 to make use of some very nice improvements.=== Planned features / tasks for this example
* Support for Scroll/Pagination https://docs.spring.io/spring-graphql/docs/current/reference/html/#data.pagination.scroll
** Need to https://github.com/spring-projects/spring-data-neo4j/issues/2726[implement some functionality] in Spring Data Neo4j first
* Native image support without additional definitions
** Spring Data Neo4j https://github.com/spring-projects/spring-data-neo4j/issues/2725[misses some hints] to compile out-of-the-box with Spring GraphQL=== Some features and libraries used in this example
==== New Testcontainers support features
* `@ServiceConnection`: https://docs.spring.io/spring-boot/docs/3.1.x/reference/htmlsingle/#features.testing.testcontainers.service-connections
* Testcontainers at development time: https://docs.spring.io/spring-boot/docs/3.1.x/reference/htmlsingle/#features.testing.testcontainers.at-development-time
* `@ImportTestcontainers`: https://docs.spring.io/spring-boot/docs/3.1.x/reference/htmlsingle/#features.testing.testcontainers.at-development-time.importing-container-declarations==== Neo4j-Migrations for test data creation
Starting the application from the tests or running integration tests, the project will use https://github.com/michael-simons/neo4j-migrations[Neo4j-Migrations] to create the initial dataset.
==== Neo4j Java driver metrics / health
===== Micrometer metrics
Although the current version of the Neo4j Java driver is 5.7.,
the Spring Boot autoconfiguration is backwards compatible to the whole 4.x versions.
As a consequence, it cannot configure the metrics provider automatically and we need to provide a `ConfigBuilderCustomizer`
to set `Micrometer` as the adapter.[source,java]
----
@Bean
ConfigBuilderCustomizer configBuilderCustomizer() {
return configBuilder -> configBuilder.withMetricsAdapter(MetricsAdapter.MICROMETER);
}
----This will provide following metrics to consume.
Accessible via http://localhost:8080/actuator/metrics/[source,text]
.Overview of available Neo4j driver metrics
----
neo4j.driver.connections.acquiring
neo4j.driver.connections.acquisition
neo4j.driver.connections.acquisition.timeout
neo4j.driver.connections.closed
neo4j.driver.connections.creating
neo4j.driver.connections.creation
neo4j.driver.connections.failed
neo4j.driver.connections.idle
neo4j.driver.connections.in.use
neo4j.driver.connections.usage
----===== Health
The health endpoint is enriched with the information about the connection to the running Neo4j instance
and can be queried at http://localhost:8080/actuator/health/neo4j.Please be aware that both, metrics and health endpoints, are configured without any security mechanisms in this project's configuration.
Always check the information you are exposing before deploying an application to the public.=== Try it out
==== With a temporary test database
The project provides a Neo4j testcontainer instance if it gets started from the tests. (Docker needed)
[source,shell]
----
./mvnw spring-boot:test-run
----==== With a "production" database
If you want to connect to a running instance, you have to start the application with the matching environment variables
[source,shell]
----
SPRING_NEO4J_AUTHENTICATION_PASSWORD= \\
SPRING_NEO4j_URI= \\
./mvnw spring-boot:run
----Please keep in mind that the application is focused on using the movie dataset.
If not happen yet, please run the `:play movies` guide and the Cypher query to populate the database.==== Compile native
This requires you to have GraalVM (Java 17 based) installed and included your _$PATH_ on your machine.
There is a pre-configured profile (_native_) that will invoke the GraalVM's native maven plugin.[source,shell]
.invoke native compile
----
./mvnw -Pnative clean package
----After it has successfully compiled, you can invoke the executable similar to the command above.
[source,shell]
.start the application
----
SPRING_NEO4J_AUTHENTICATION_PASSWORD= \\
SPRING_NEO4j_URI= \\
target/spring-neo4j-graphql
----=== What to explore
The current schema looks like this:
[source,graphql endpoint]
.schema.graphqls
----
type Query {
movies : [Movie]
movie(title : String!) : Movie
}type Movie {
id: ID!
title: String!
description : String!
"Random number 0 - 100 as an example for aggregation of data"
rating: Int
actors: [Person]
directors: [Person]
}"A person is either an actor or a director"
type Person {
id: ID!
name: String!
yearOfBirth: Int
}
----Example queries as you can see above are:
[source,graphql]
.Query all movies
----
{movies {title, actors {name, yearOfBirth}}}
----
will return:[source, json]
----
{
"data": {
"movies": [
{
"title": "The Matrix",
"actors": [
{
"name": "Gloria Foster",
"yearOfBirth": null
},
{
"name": "Hugo Weaving",
"yearOfBirth": 1960
},
{
"name": "Keanu Reeves",
"yearOfBirth": 1964
},
{
"name": "Emil Eifrem",
"yearOfBirth": 1978
}, ...
]},
{
"title": "The Matrix Reloaded",
"actors": [
{
"name": "Gloria Foster",
"yearOfBirth": null
}, ....
]}
]}
}
----[source,graphql]
.Query one particular movie
----
{movie (title: "The Matrix") {title, description}}
----will return:
[source, json]
----
{
"data": {
"movie": {
"title": "The Matrix",
"description": "Welcome to the Real World"
}
}
}
----==== Multiple sources
It is possible to aggregate the data from different sources.
For example the `rating` field of the `Movie` will be a random generated number between 0 and 100.[source,graphql]
.Query movie with field from other source
----
{movie (title: "The Matrix") {title, rating}}
----returns
[source, json]
----
{
"data": {
"movie": {
"title": "The Matrix",
"rating": 99
}
}
}
----