This is Part 4 in the Full-Stack Next.js Development with Nhost series. This part focuses on GraphQL operations, database management, and permission-based data access control using Next.js App Router with server/client component separation.
Full-Stack Next.js Development with Nhost
1. Create Project
Set up your Nhost project
2. Protected Routes
Route protection basics
3. User Authentication
Complete auth flow
4. GraphQL Operations
Current - CRUD operations with GraphQL
5. File Uploads
File upload and management
Prerequisites
- Complete the User Authentication part first
- The project from the previous part set up and running
What You’ll Build
By the end of this part, you’ll have:- GraphQL queries and mutations for complete CRUD operations
- Database schema with proper relationships and constraints
- User permissions for secure data access control
- Next.js components using server/client patterns that interact with GraphQL endpoint
- Server actions for secure data mutations
- Server components for efficient data fetching
Step-by-Step Guide
1
Create the To-Dos Table
First, we’ll perform the database changes to set up the todos table with proper schema and relationships to users.In your Nhost project dashboard:- Navigate to Database
- Click on the SQL Editor
Please make sure to enable Track this so that the new table todos is available through the auto-generated APIs
2
Set Up Permissions
It’s now time to set permission rules for the table you just created. With the tabletodos
selected, click on …, followed by Edit Permissions.
You will set permissions for the user role and actions insert, select, update, and delete.When inserting permissions we are only allowing users to set the 
title
, details
, and completed
columns as the rest of the columns are set automatically by the backend. The user_id
column is configured as a preset to the currently authenticated user’s ID using the X-Hasura-User-Id
session variable. This ensures that each todo is associated with the user who created it.
3
Create the Todos Page System
Now let’s implement the Next.js page system that uses the database we just configured. We’ll create a server component for the main page, a client component for the interactive todos interface, and server actions for secure data mutations.The main todos page is a server component that fetches initial data server-side and renders the todos interface. This component runs on the server and provides the initial state to the client component.
src/app/todos/page.tsx
4
Update Navigation Component
Add the todos page to your application navigation by updating the Navigation component to include a link to the todos page.src/components/Navigation.tsx
5
Test Your Complete Application
Run your Next.js application and test all the functionality:- Server-Side Rendering: Notice how the todos are loaded server-side on initial page load, providing faster initial rendering
- Authentication Integration: Try signing in and out and see how the Todos page is only available when authenticated through middleware protection
- CRUD Operations: Create, view, edit, complete, and delete todos. Notice how server actions handle mutations while maintaining type safety
- Multi-User Isolation: Open the application in another browser or incognito window, sign in with a different account and verify that you cannot see or modify todos from the first account
- Real-time Updates: Unlike client-only React apps, changes will be persisted immediately through server actions and reflected in the optimistic UI updates
Key Features Implemented
Database Schema
Database Schema
Properly designed todos table with constraints, indexes, and automatic timestamp updates for optimal performance.
GraphQL API
GraphQL API
Auto-generated GraphQL API with queries and mutations for full CRUD operations on todos.
Row-Level Security
Row-Level Security
Comprehensive permissions ensuring users can only access their own todos through all GraphQL operations.
Server/Client Architecture
Server/Client Architecture
Next.js App Router patterns with server components for data fetching, client components for interactivity, and server actions for mutations.
Server-Side Data Fetching
Server-Side Data Fetching
Initial todos loaded server-side for improved performance and SEO, with client-side state management for optimal user experience.
Type-Safe Server Actions
Type-Safe Server Actions
Secure server-side mutations with comprehensive error handling and type safety throughout the data flow.
Rich Interface
Rich Interface
Expandable todo items, inline editing, completion status, and detailed timestamps with responsive design.