Building GraphQL APIs With The Neo4j GraphQL Library

Rate this content
Bookmark

This workshop will explore how to build GraphQL APIs backed Neo4j, a native graph database. The Neo4j GraphQL Library allows developers to quickly design and implement fully functional GraphQL APIs without writing any resolvers. This workshop will show how to use the Neo4j GraphQL Library to build a Node.js GraphQL API, including adding custom logic and authorization rules.

Table of contents:
- Overview of GraphQL and building GraphQL APIs
- Building Node.js GraphQL APIs backed a native graph database using the Neo4j GraphQL Library
- Adding custom logic to our GraphQL API using the @cypher schema directive and custom resolvers
- Adding authentication and authorization rules to our GraphQL API

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

FAQ

Neo4j Aura is a managed cloud service that provides hosted Neo4j database instances. It offers a free tier suitable for development and hobby projects, as well as professional and enterprise tiers for production applications.

To set up a Neo4j Aura instance, go to dev.neo4j.com/neo4j-aura, sign in with Google or create a username and password, and choose the AuraDB free instance. Be sure to copy and save the password provided during setup.

The Neo4j GraphQL Library is a Node.js JavaScript library that simplifies building GraphQL APIs backed by Neo4j. It supports GraphQL-first development, generates CRUD operations, and allows adding custom logic using Cypher or custom resolvers.

Custom logic can be added to a Neo4j GraphQL API using Cypher schema directives or custom resolvers. Cypher directives allow embedding Cypher queries directly in the GraphQL schema, while custom resolvers provide more flexibility for implementing complex logic.

Cypher schema directives are annotations in the GraphQL schema that specify custom Cypher queries to be executed. These directives can be used to add custom logic for fields, queries, and mutations, integrating seamlessly with the generated Cypher queries.

The auth schema directive in the Neo4j GraphQL Library is used to define authorization rules for types and fields in the GraphQL schema. These rules ensure that only authenticated users with specific roles or conditions can access or modify certain data.

JSON Web Tokens (JWTs) are a way to securely transmit information between parties as a JSON object. In Neo4j GraphQL, JWTs are used to verify user authentication and authorization. Claims in the JWT can be matched against database values to enforce access control rules defined in the auth schema directive.

Yes, you can project custom objects in a Neo4j GraphQL API using Cypher schema directives. These directives allow you to write Cypher queries that return custom objects, which can be mapped to types defined in the GraphQL schema.

GraphQL Playground is an in-browser tool for querying and introspecting GraphQL APIs. It provides features like autocompletion, documentation browsing, and the ability to execute queries and mutations, making it useful for exploring and testing GraphQL APIs.

Authorization in a Neo4j GraphQL API is handled using the auth schema directive. This directive allows defining rules based on JSON Web Tokens (JWTs) to protect types and fields. Rules can include checks for authentication, roles, and specific conditions matching JWT claims to database values.

William Lyon
William Lyon
175 min
03 Dec, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Today's workshop focused on building GraphQL APIs using Neo4j, a graph database. The Neo4j GraphQL library was used to generate the API, handle data fetching, and solve the N plus one query problem. Custom logic and authorization rules were added using Cypher statements and the auth schema directive. The workshop also covered filtering, sorting, and pagination options in GraphQL, as well as the integration of Neo4j with Code Sandbox and Neo4j Aura DB. Overall, the workshop provided a comprehensive overview of building GraphQL APIs with Neo4j and highlighted the benefits and challenges of using this technology.

1. Introduction to Neo4j and GraphQL

Short description:

Hello everyone! Today, we'll be building GraphQL APIs for sandboxing using Neo4j, a graph database. We'll start by discussing Neo4j and GraphQL, then explore the Neo4j GraphQL library for building APIs. We'll also cover adding custom logic and authorization rules to our API. Please note that we'll be focusing on the backend piece and not integrating the GraphQL API into a frontend application.

ReactJS, through the mouth of assistant projects and project managers. Well, hello everyone. Thanks for joining today. We are going to be building GraphQL APIs for sandboxing, which is a hosted database as a service. You don't need to set up local environments. We're going to do everything with these hosted sandbox environments. So don't worry about setting up a local environment, but we'll talk about that in a little bit.

The slides are linked at dev.neo4j.com slash GraphQL dash training, link in the chat here. I would recommend pulling up these slides as we work through this. There are several links and code snippets and things like that, that you may want to grab from the slides. So I'd recommend pulling up the slides as we go through this and just having them open in a tab. There's also the Discord, which Lara linked at the beginning. So I'll monitor both the Zoom chat and Discord as well. So that's either one. If you have questions, please just drop them in the chat, either in Zoom or Discord, and I'll try to monitor both and share links in both.

So my name is Will. I work for a company called Neo4j, which is a graph database that we'll be using today. I also co-host a podcast called graphstuff.fm. So if you're interested in graph technology and you like a podcast format, definitely encourage you to check that out. So here's our rough outline for today. We'll talk a little bit about what is Neo4j. I think since that was pretty much completely new for most folks, we will spend a little bit of time there but really we'll be focused on GraphQL and building GraphQL APIs backed by Neo4j. So we'll talk a little bit about Neo4j, then we'll talk a bit about GraphQL and how GraphQL APIs are typically built. Then we'll take a look at the Neo4j GraphQL library which is a Node.js JavaScript library for building GraphQL APIs backed by Neo4j. Then we'll take a look at adding custom logic to our API. And then in the final module, we will take a look at adding authorization rules to our API. We're gonna be building like a bookstore API. So we'll focus on the backend piece of this, not so much on what do I actually do with my GraphQL API once I have it built. Instead, we're gonna be focused on just the backend piece. So we won't cover integrating, for example, a GraphQL API into like a React application or something like that. We will be focusing just on the backend piece.

2. Neo4j Aura DB and Code Sandbox

Short description:

Today, we'll be using Neo4j Aura DB and Code Sandbox for our GraphQL API development. Neo4j Aura offers a free tier for hosting Neo4j instances in the cloud, and Code Sandbox allows us to run code in the browser without dealing with local development issues. We'll start with a Skeleton Starter Code Sandbox and work through hands-on exercises. Feel free to ask questions in the chat.

So I said earlier that we were gonna be building I said earlier that we don't need to worry about setting up a local development environment. We're gonna be using these Sandbox Cloud Services today. So there's two things that we're gonna use. One is Neo4j Aura DB. There's a free tier of Neo4j Aura. So Neo4j Aura is hosted Neo4j instances in the cloud that are private to us. There's a free tier. So you don't have to think about putting in a credit card or anything like that, which is quite nice. So we'll be using that for our database. Everyone will have the opportunity to spin up an Aura instance. And then for running the code, so we're gonna be building a GraphQL API using JavaScript. So Node.js GraphQL API application. And for that, we're gonna use Code Sandbox. So Code Sandbox allows us to run code in the browser, including Node code that runs on someone's container somewhere that they're hosting for us, which is quite nice. So we don't have to worry about dealing with local environments development issues. Each one of these modules, I should say I guess the last three modules have a link for a Skeleton Starter Code Sandbox with some starter code. So we'll start that way and then work through exercises, editing some code, writing GraphQL queries, that sort of thing. So you'll see this hands-on exercise icon on some slides. When you have that, we'll stop for a few minutes and we will dig into some exercises. So I'd like for this to be as hands-on as possible. And as folks have questions, please just drop them in the chat, either in Zoom or Discord, and we will dig into your questions.

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.
How to Solve Real-World Problems with Remix
Remix Conf Europe 2022Remix Conf Europe 2022
195 min
How to Solve Real-World Problems with Remix
Featured Workshop
Michael Carter
Michael Carter
- Errors? How to render and log your server and client errorsa - When to return errors vs throwb - Setup logging service like Sentry, LogRocket, and Bugsnag- Forms? How to validate and handle multi-page formsa - Use zod to validate form data in your actionb - Step through multi-page forms without losing data- Stuck? How to patch bugs or missing features in Remix so you can move ona - Use patch-package to quickly fix your Remix installb - Show tool for managing multiple patches and cherry-pick open PRs- Users? How to handle multi-tenant apps with Prismaa - Determine tenant by host or by userb - Multiple database or single database/multiple schemasc - Ensures tenant data always separate from others
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced 2022React Advanced 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.

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.
Get rid of your API schemas with tRPC
React Day Berlin 2022React Day Berlin 2022
29 min
Get rid of your API schemas with tRPC
Today's Talk introduces TRPC, a library that eliminates the need for code generation and provides type safety and better collaboration between front-end and back-end. TRPC is demonstrated in a Next JS application integrated with Prisma, allowing for easy implementation and interaction with the database. The library allows for seamless usage in the client, with automatic procedure renaming and the ability to call methods without generating types. TRPC's client-server interaction is based on HTTP requests and allows for easy debugging and tracing. The library also provides runtime type check and validation using Zod.
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.