Using Redwood with Neo4j Database

Hi folks! I’ve been playing around with using Redwood with Neo4j (graph database). There is a GraphQL integration for Neo4j that makes it easy to build GraphQL APIs backed by Neo4j, and since Redwood relies heavily on GraphQL this combination seems like a great match to me.

So far I’ve converted the Redwood tutorial blog application to use neo4j-graphql.js for the GraphQL API. The benefit of neo4j-graphql.js is that we don’t have to write resolvers (database queries are auto-generated from arbitrary GraphQL requests) and we still have the ability to define custom logic using the @cypher schema directive in the GraphQL schema.

Getting the tutorial application to work involved changing api/src/functions/graphql.js to use the autogenerated Neo4j GraphQL schema instead of the one with explicitly defined resolvers using Prisma:

import importAll from '@redwoodjs/api/importAll.macro'
import { ApolloServer } from 'apollo-server-lambda'
import { makeAugmentedSchema } from 'neo4j-graphql-js'
import neo4j from 'neo4j-driver'

// Fetch all GraphQL SDL types defined in graphql/*.sdl.js
// and combine into a single typeDefs string
const sdls = importAll('api', 'graphql')
const typeDefs = Object.values(sdls).reduce((acc, v) => acc + v.schema, '')

// Create a connection to a Neo4j instance
// reading credentials from environment variables (can be set in .env)
const driver = neo4j.driver(
  process.env.NEO4J_URL,
  neo4j.auth.basic(process.env.NEO4J_USER, process.env.NEO4J_PASSWORD),
  { encrypted: true }
)

// Create an executable GraphQL schema with autogenerated
// resolvers from our type definitions
const schema = makeAugmentedSchema({ typeDefs })

// Create an ApolloServer instance, passing our GraphQL schema
// and injecting the Neo4j driver instance into the resolver context
const server = new ApolloServer({
  schema,
  context: { driver },
  playground: true,
  introspection: true,
})

exports.handler = server.createHandler()

The generated GraphQL API matches pretty closely to what the Redwood generators expect so once that GraphQL API is in place there were only a few tweaks to the GraphQL queries to get the blog application working.

The code so far is in this repo on Github (in the neo4j-graphql branch).

I’d love to hear any suggestions or feedback if anyone has any thoughts - especially around ideas for what to add next!

Cheers,
Will

5 Likes

Also, there’s a live version of this running here (deployed on Netlify of course), using a Neo4j Aura instance for the database.

@lyonwj Huge thanks for sharing! This is officially the first noSQL experiment with RedwoodJS I’ve seen :rocket:

And there’s been a lot of interest in general for examples of connections to other DB types without the use of Prisma. (For those new to Neo4j, it’s a graph database, so the structure and use cases are very different from relational/SQL DBs.)

I’ll try to dig in a bit deeper tomorrow. But just needed to say we’re really excited to see things like this.

(@crabasa this might be of interest to you as well…)

2 Likes

I was thinking along the same lines regarding integration between neo4j and redwood and how they would compliment each other.

Will, Would this be compatible using the newer neo4j/graphql instead of neo4j-graphql-js? Could you provide an example?

Thanks for sharing some ideas on doing neo4j with Redwood.
I am no experts on either, but it seems that a graph db (like neo4j) has some unique use cases and benefits over traditional rel. dbs, where you normally when building a startup with a db, you might not ahead of time know all your needs in terms of how you want to query and combine data, and you would ideally like to avoid later being in a situation where you have to do expensive and complex joins. At least that is what the marketing for graph dbs are telling you… if all this is true or not, I would like to know and hear other more experienced db admins or devs share their experiences on… Excited for RW v1.0, best of luck to you all!

I loved my time with Neo4j

I used the database for storing biathalon and triathalon data. I divided my data by whether the entities could have a date-timestamp.

For can-only-be-current data: (like people, and the races as repeating entities) then I used the on-linkage store for date-based info (the annual tirathalon and who to contact about it now, vs where it was held on a particular date, who was in charge on that date, and the runners and their scores, etc.)

If it was time-less data it was objects, each linkage between time-less entities had dated records - I could assemble all the history for any runner * race by gathering all their linkages and sorting on the date.

So much easier than that would have been in SQL I think.

I’m trying to bring together @neo4j/graphql and RedwoodJS. The above snippet from @lyonwj is not valid anymore and I’m wondering if anyone has ideas.

Even a standard workflow for adopting any non-prisma backed database would go a long way for me here!

Thank you for your work on Redwood, I’m stoked to learn more :smiley: