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.

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
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
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.

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
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
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
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
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
In this talk, we'll look at some of the modern options for building a full-stack React and GraphQL app with strong conventions and how this can be of enormous benefit to you and your team. We'll focus specifically on RedwoodJS, a full stack React framework that is often called 'Ruby on Rails for React'.
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
Though GraphQL is declarative, resolvers operate field-by-field, layer-by-layer, often resulting in unnecessary work for your business logic even when using techniques such as DataLoader. In this talk, Benjie will introduce his vision for a new general-purpose GraphQL execution strategy whose holistic approach could lead to significant efficiency and scalability gains for all GraphQL APIs.