Building GraphQL APIs With The Neo4j GraphQL Library & Neo4j AuraDB

Rate this content
Bookmark

Learn how to use the Neo4j GraphQL library to build Node.js GraphQL APIs backed by the Neo4j graph database. This course covers building GraphQL APIs using the Neo4j GraphQL Library and the Neo4j AuraDB cloud-native database to build an e-comerce GraphQL API backed by a native graph database in the cloud.


Table of contents:

- Introduction To GraphQL & Neo4j

- The Neo4j GraphQL Library: modeling a graph with GraphQL type definitions; creating and querying a GraphQL API using the Neo4j GraphQL Library

- Adding Custom Logic With Cypher And Custom Resolvers: using the @cypher GraphQL schema directive; adding custom resolver functions with the Neo4j GraphQL Library

- Authorization With The Neo4j GraphQL Library: working with JSON Web Tokens (JWTs) to authenticate your API users; using the @auth GraphQL schema directive to attach authorization rules to your GraphQL API


Prerequisites:

No local setup is required for the workshop. We will make use of Codesandbox (a browser-based tool for editing and running code in the browser) and Neo4j AuraDB, a free managed database service. Some familiarity with GraphQL and JavaScript is helpful, however not strictly required. No experience with Neo4j is necessary.

This workshop has been presented at GraphQL Galaxy 2022, check out the latest edition of this Tech Conference.

FAQ

Neo4j is an open-source graph database that uses the property graph data model, where data is stored as nodes and relationships. It is optimized for traversing and querying graph structures.

GraphQL is an API query language that uses a strict type system to define data structures. It allows clients to request exactly the data they need and receive responses that match the shape of their queries.

The Neo4j GraphQL library is a tool for building Node.js GraphQL APIs backed by Neo4j. It generates GraphQL CRUD operations, supports complex filtering, pagination, and custom logic using Cypher schema directives.

Custom logic in the Neo4j GraphQL library can be added using the @cypher schema directive, which allows custom Cypher queries to be attached to fields, queries, and mutations. Alternatively, custom resolvers can be implemented.

Using GraphQL with Neo4j reduces boilerplate code, addresses performance issues like the N+1 query problem, and leverages the graph database's optimized traversal capabilities. It also boosts developer productivity by auto-generating CRUD operations.

Cypher is the query language used in Neo4j. It is designed for querying and updating graph structures and is similar to SQL but optimized for graph databases.

The Neo4j GraphQL library supports authorization using the @auth schema directive. It uses JSON Web Tokens (JWTs) to define and enforce access control rules at the field and type levels.

To get started with Neo4j Aura, visit the Neo4j Aura website, sign up for an account, and select the free tier. You can create a new instance and load a dataset to begin working with the database.

Tools used include Neo4j Aura for hosted databases, Neo4j GraphQL Sandbox and CodeSandbox for running and testing code, and Neo4j GraphQL Toolbox for low-code development and testing.

Common use cases for graph databases include recommendation systems, fraud detection, network management, identity and access management, and data lineage tracking.

William Lyon
William Lyon
146 min
06 Dec, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's workshop focused on Neo4j and GraphQL, covering topics such as graph pattern matching in Cypher, use cases for graphs in Neo4j, and the basics of GraphQL. The workshop also explored resolver functions and performance optimization in GraphQL, as well as writing queries and setting up a Neo4j Aura database. The Neo4j GraphQL library was introduced, highlighting its features and the ability to add custom logic and authorization. The workshop concluded with a discussion on connecting Neo4j Aura to a Node.js GraphQL server and implementing custom resolvers and authorization rules.

1. Introduction to Neo4j and GraphQL

Short description:

I work for Neo4j, an open-source graph database company. Today, we'll focus on the back-end piece of a full stack application, working with the database, building a GraphQL server, and using cloud services. We'll use Neo4j Aura as our database and the Neo4j GraphQL library to build GraphQL APIs backed by Neo4j. We'll also explore adding custom logic and authorization to our GraphQL API. No need to set up a local development environment; we'll use hosted services like Neo4j Aura and the Neo4j GraphQL Sandbox.

So just a little bit about me. My name's Will. I work for a database company called Neo4j. Neo4j is an open-source graph database. We'll talk about what that means and we'll do some hands-on exercises with Neo4j as we our GraphQL API. So I work on the developer relations team at Neo4j, and so roughly that means helping people build applications with graphs and Neo4j. I also work on integration tooling, so making sure that you can use Neo4j with different tools in the ecosystem. GraphQL is one of those. I also recently wrote a book published by Manning called Full Stack GraphQL Applications, which you can actually download for free. This link dev.neo4j.com slash GraphQL dash book.

What we're going to talk about today is largely the back-end piece of a full stack application, right? So we're going to work with the database. We're going to build a GraphQL server, and we'll use some cloud services to host that in the cloud. But we're not going to talk about how do we integrate GraphQL into our front-end application. The book goes into a lot more detail on that.

So the rough outline for today. We have a few different modules to work through so I'll have a mix of some slides and examples. And then we have at least one or two hands-on exercises for each one of these modules that we're going to go through. So we're going to start off with what I think for some folks will be a recap of an intro to GraphQL but just make sure that we're all roughly on the same page. Starting out here. Then we'll take a look at Neo4j. We're going to use a hosted database as a service called Neo4j Aura. There's a free tier. We can just click a couple buttons and spin one up. So that's going to be our database for today. And then we're going to look at using the Neo4j GraphQL library which is a database integration for GraphQL and Neo4j that makes it easier to build GraphQL APIs backed by Neo4j. So we'll look at a couple of different ways to get started using the Neo4j GraphQL library. We'll look at how we can add custom logic to our GraphQL API using the Cypher query language. Cypher is the database query language that we use with Neo4j. But there's a very interesting way that we can combine that with GraphQL to add custom logic. And then if we have time, which I'm not sure if we'll get to this but we'll at least talk about this, is how do we add authorization to our GraphQL API using some of the features in the grew up to library to define authorization rules in the GraphQL schema and JSON web tokens.

So you don't need to worry about setting up a local development environment or cloning a GitHub repo or anything like that. We're just going to use posted services. So I mentioned we're using Neo4j or a DB, the free tier of that. We're going to use a tool called the Neo4j GraphQL Sandbox, which is a sort of in browser tool for working with GraphQL and Neo4j. And then we're going to use code sandbox to run some JavaScript code. So we do have a bunch of links to slides here and various resources, documentations. But I'll talk about these as we go through them. Cool.

So let's talk a little bit about Neo4j. I think some folks said they're not familiar with Neo4j. So Neo4j is a graph database. So unlike other databases that use tables or documents as the data model, Neo4j and other graph databases use what's called the property graph data model. So a graph, entities, those are nodes and relationships that connect nodes. That's the basic data model that we work with in a graph database like Neo4j. We use the Cypher query language primarily to work with Neo4j. There are other ways of interacting with the database, which we'll see today. Primarily, you can think of Cypher as kind of like SQL, but for graphs. There's an example here in the, kind of the upper right, talking about addresses. Something in New York, registered address, connected to officers and entities. And there's this sort of ASCII art graph pattern drawn here.

2. Introduction to Cypher and GraphQL

Short description:

Graph pattern matching is a core part of Cypher. Patterns in Cypher are defined using an ASCII art representation. We use parentheses to indicate nodes, arrows to represent relationships, and shorthand notation for outgoing relationships. An example query from the Panama Papers Dataset is used to illustrate the use of Cypher in graph analysis. We will discuss Cypher further after exploring GraphQL.

So what is this about? Well, graph pattern matching is kind of a core part of Cypher. So the way that we define patterns in Cypher is to use this ASCII art representation. So the parentheses, first around address, that's indicating a node, so we're looking for address nodes where the address contains New York. So find addresses in New York, and then this arrow that we've drawn here with the registered address, that's representing a relationship connected to officer nodes. And then we have the shorthand for an outgoing relationship now to these entity nodes. So this is saying, find addresses in New York, find officer nodes connected to those addresses through this registered address relationship. And then find any entities connected to those officers with an address in New York. I have used this query as an example, because this comes from the Panama Papers Dataset, which was a data journalism investigation a few years ago, where the data journalists at the ICIJ, used Neo4j to make sense of these leaked documents about offshore companies, because that was a very graphy problem with, sort of, nested structures of offshore companies and things like that. So we'll talk a bit more about Cypher after we sort of, dig into GraphQL a bit.

Watch more workshops on topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
The Talk discusses the challenges and advancements in using GraphQL and React together. It introduces RedwoodJS, a framework that simplifies frontend-backend integration and provides features like code generation, scaffolding, and authentication. The Talk demonstrates how to set up a Redwood project, generate layouts and models, and perform CRUD operations. Redwood automates many GraphQL parts and provides an easy way for developers to get started with GraphQL. It also highlights the benefits of Redwood and suggests checking out RedwoodJS.com for more information.
Adopting GraphQL in an Enterprise
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
Adopting GraphQL in an Enterprise
Today's Talk is about adopting GraphQL in an enterprise. It discusses the challenges of using REST APIs and the benefits of GraphQL. The Talk explores different approaches to adopting GraphQL, including coexistence with REST APIs. It emphasizes the power of GraphQL and provides tips for successful adoption. Overall, the Talk highlights the advantages of GraphQL in terms of efficiency, collaboration, and control over APIs.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
GraphQL has made a huge impact in the way we build client applications, websites, and mobile apps. Despite the dominance of resolvers, the GraphQL specification does not mandate their use. Introducing Graphast, a new project that compiles GraphQL operations into execution and output plans, providing advanced optimizations. In GraphFast, instead of resolvers, we have plan resolvers that deal with future data. Graphfast plan resolvers are short and efficient, supporting all features of modern GraphQL.