NODES 2024 Online Developer Conference | November 7, 2024 | Register Today

Neo4j logo
Neo4j GraphQL Toolbox is Available Now - Read more!
Putting the Graph into GraphQL

Neo4j GraphQL Library

An extensible, low-code, open source library designed for building API-driven, intelligent applications faster with a Neo4j graph database.

Build Intelligent, Modern APIs with Ease

The Neo4j GraphQL Library is a highly flexible, low-code, open source JavaScript library that enables rapid API development for cross-platform and mobile applications by tapping into the power of connected data.

With Neo4j as the graph database, the GraphQL Library makes it simple for applications to have application data treated as a graph natively from the front-end all the way to storage, avoiding duplicate schema work and ensuring flawless integration between front-end and backend developers.

Written in TypeScript, the library's schema-first paradigm lets developers focus on the application data they need, while taking care of the heavy lifting involved in building the API.

Benefits of the Neo4j GraphQL Library

Powerful and fast

Low-Code and High Performance

Don't yet understand Cypher? Don't sweat it. The library translates GraphQL queries into a single Cypher query. Developers using this library can focus solely on building applications while just writing minimal backend code – the library handles the rest.

Secure, reliable, and data-rich

Modern and Intelligent

The library simplifies modernizing applications for exceptional performance, business results, and user engagement. Developers can create predictive, mission-critical, and context-aware apps with data-rich APIs that are more secure (using Auth) and reliable.

Built for developer flexibility

Developer-Friendly

Enabling full-stack developers with GraphQL-first development, the library generates Cypher from GraphQL, GraphQL CRUD API from type definitions, auto-generated resolvers, and custom logic with @cypher schema directive. It also comes with powerful authorization to secure your GraphQL API, nested mutations, and an OGM (Object-Graph-Mapper).

Choose your platform

Open and Flexible

Fast, open, and fun for developers, the library has a low learning curve, helping developers quickly build API-driven applications across open source frameworks (React, Vue, Angular and Others) and achieve exceptional flexibility to deploy across on-prem, cloud, or in a serverless setup.

How the Neo4j GraphQL Library Works

The Neo4j GraphQL Library enables developers to build GraphQL API layers using any JavaScript GraphQL implementation. This GraphQL API layer is commonly deployed as a Node.js web server, and the Neo4j data model is defined by a GraphQL schema.

Inside auto-generated resolver functions, GraphQL queries are translated to Cypher queries and sent to a Neo4j database by including a Neo4j driver instance in the context object of the GraphQL request. Resolvers can be overridden by a custom resolver function implementation to allow for custom logic.

Optionally, GraphQL fields can be resolved by a user-defined Cypher query through the use of the @cypher schema directive.

Neo4j GraphQL Library Features

Generate GraphQL CRUD API

The Neo4j GraphQL Library automatically creates Query and Mutation types from your type definitions that provide instant CRUD functionality. The GraphQL CRUD API can be configured and extended, and helps define the database schema and the GraphQL schema with the same type definitions.

type Movie {
  title: String!
  actors: [Person!]!
    @relationship(type: "ACTED_IN", direction: IN)
}

type Person {
  name: String!
}
{
  movies(where: { title: "River Runs Through It" }) {
    title
    actors {
      name
    }
  }
}
MATCH (m:Movie)<-[:ACTED_IN]-(a:Actor)
WHERE m.title = "River Runs Through It"
RETURN *

Generate Cypher from GraphQL

The Neo4j GraphQL Library generates a single Cypher query to resolve any arbitrary GraphQL request.

This means the resolvers are automatically implemented for your scenario. No need to write boilerplate data fetching code – just inject a Neo4j driver instance into the request context and the Neo4j GraphQL Library takes care of generating the database query and handling the database call. Additionally, since a single Cypher query is generated, this results in a huge performance boost and eliminates the N+1 query problem.

Extending GraphQL with Cypher

Since GraphQL is an API query language and not a database query language, it lacks semantics for operations such as aggregations, projections, and more complex graph traversals.

You can now extend the power of GraphQL through the use of @cypher GraphQL schema directives to bind the results of a Cypher query to a GraphQL field. This allows for the expression of complex logic using Cypher and can be used on Query and Mutation fields to define custom data fetching or mutation logic.

The Neo4j native graph database makes complex and multi-hop graph traversals highly performant to provide blazing-fast query response for your applications.

type Movie {
  title: String!
  actors: [Person!]!
    @relationship(type: "ACTED_IN", direction: IN)
  similar(first: Int = 3): [Movie!]!
    @cypher(
      statement: """
      MATCH (this)<-[:ACTED_IN|:DIRECTED]-(o)-[:ACTED_IN|:DIRECTED]->(rec:Movie)
      WITH rec, COUNT(*) AS score
      RETURN rec ORDER BY score DESC LIMIT $first
      """
    )
}

type Person {
  name: String!
}
type Movie {
  title: String!
  actors: [Person!]!
    @relationship(type: "ACTED_IN", direction: IN)
}

type Person {
  name: String!
}

extend type Movie
  @auth(
    rules: [{ operations: [UPDATE], allow: { actors: { name: "$jwt.sub" } } }]
  )

Secure APIs with Built-In Auth

Neo4j GraphQL Library comes with an Auth directive out-of-the-box for enabling authorization rules to GraphQL API using JSON Web Tokens (JWTs).

Having lots of different approaches to securing GraphQL API puts some beginners off. With the Auth directive, we have reduced a lot of complexity that comes with building a secure GraphQL API. Including Auth provides a working solution to users wanting to quickly and easily "spin up" secure services. Also, the power of Cypher effectively enables the library to translate complex authorization rules into a single Cypher query.

Power REST APIs and More with OGM

If you don't know what OGM (Object Graph Mapper) is, no worries. It's an ORM (Object Relational Mapper) for your graph database and provides a type-safe wrapper around the CRUD API. You can use the OGM, just as you would ORM, to power REST APIs, custom scripts, migrations, authentication, and anywhere you interact with Neo4j.

When using the Neo4j GraphQL Library, there may be a time when you need to define a custom query or mutation – you can use the OGM here. This means, you don't need to write a custom Cypher in your application. Cool, right?!

const { OGM } = require("@neo4j/graphql-ogm");
const neo4j = require("neo4j-driver");

const typeDefs = `
  type Movie {
    title: String!
    actors: [Person!]! @relationship(type: "ACTED_IN", direction: IN)
  }
  type Person {
    name: String
  }
`;

const driver = neo4j.driver(
  "bolt://localhost:7687",
  neo4j.auth.basic("admin", "password")
);

const ogm = new OGM({
  typeDefs,
  driver,
});
const Movie = ogm.model("Movie");

ogm.init().then(() => {
  Movie.create({
    input: [
      {
        title: "Forrest Gump",
        actors: {
          create: [{ node: { name: "Tom Hanks" } }],
        },
      },
    ],
  }).then(() => console.log("Movie Created"));
});
type Movie {
  title: String!
  actors: [Person!]!
    @relationship(type: "ACTED_IN", direction: IN, properties: "ActedIn")
}

type Person {
  name: String!
}

interface ActedIn {
  roles: [String!]!
}

Easily Add Relationship Properties

Adding relationships into your data model gives your data the context it needs to run complex queries across wide sections of your graph.

With GraphQL Library, you are able to define an interface that represents your relationship properties. This can be used with multiple relationships simply by adding the properties key to your existing relationship directives. The result is a great developer experience when incrementally growing applications.

Cursor-Based Pagination

Cursor-based pagination is the preferred method for delivering an infinite scroll on the frontend. The biggest advantage of cursor-based pagination is its ability to handle real-time data changes effectively. This is because cursors do not require the data to remain static – when records are added or removed, each new page still loads correctly.

With GraphQL Library, you get cursor-based pagination for all relationship fields, with the great filtering and sorting you’re already familiar with.

{
  movies(where: { title: "River Runs Through It" }) {
    title
    actorsConnection(first: 10, after: "opaqueCursor") {
      edges {
        roles
        node {
          name
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
}

See What Our Customers Are Saying

  • TypeScript + @neo4j + @neo4j/graphql + Apollo + AWS Lambda + Pulumi makes for an awesome API prototyping stack, and gives you tons of power up front so you don’t have to write as much cypher."

    Noël Jackson, Developer, noeljackson.com
  • “I am absolutely blown away how easy it is to spin up a GraphQL API on top of my Neo4j database. Incorporating user authentication was a breeze; freeing me up to explore user roles and permissions. The authentication/authorization is straightforward and easy to grok. So easy a drummer can do it!”

    Rob Brennan, Consulting Architect, GenUI
  • "I'm so excited! I have the feeling all my requirements are taken into account by the new API. I really, really love the new library and the efforts that Neo4j is taking to provide better integration of the database into the application layer, especially for a GraphQL API."

    Robert Schäfer, Full-Stack Engineer
  • “Spent the weekend and a couple days this week and completely refactored our app to use alpha. I ditched SO MUCH code with the new auth and “connect” functionality. Thanks guys!”

    Thomas Lester, Founder / Developer
  • "You have all done such a great job addressing the exact pain points we've been experiencing. thank you!”

    Oren Goldberg, Lead Engineer at United for Respect
  • “Hi there! Just discovered this new alpha release. It looks super nice, and seems to solve a lot of the small issues we have faced in the current js library! Great work Do you have any release plan / ETA for when this would be released in a stable version? Would love to start migrating as soon as possible”

    Neo4j GraphQL Library User
RESOURCES

Featured Content

Overview

GraphQL Library Product Brief

Learn

Putting The Graph In GraphQL With The Neo4j GraphQL Library

Learn

Full Stack GraphQL With Next.js, Neo4j AuraDB And Vercel

Learn

Why You Should Consider Graphs For Your Next GraphQL Project

Learn

10 Things We Learned In Full Stack GraphQL Book Club

Library

The Neo4j GraphQL Library

Videos

Full Stack GraphQL Book Club - Chapter 1: What Is Full Stack GraphQL?

Full Stack GraphQL Book Club - Chapter 2: Graph Thinking With GraphQL

Full Stack GraphQL Book Club - Chapter 3: Graphs In The Database

Full Stack GraphQL Book Club - Chapter 4: The Neo4j GraphQL Library

Full Stack GraphQL Book Club - Chapter 5: Building User Interfaces With React

Full Stack GraphQL Book Club - Chapter 6: Client-Side GraphQL With React and Apollo Client

GraphQL Book Club - Chapter 7: Adding Authorization And Authentication

GraphQL Book Club - Chapter 8: Deploying Our Full Stack GraphQL Application

GraphQL Book Club - Chapter 9: Advanced GraphQL Considerations

Full Stack GraphQL Applications: Leveraging the Power of GraphQL

Ready to Get Started?

Sign up for our GraphAcademy course, Building GraphQL APIs Using the Neo4j GraphQL Library, to dig deeper into the Graph QL Library.

Register Now