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