This is a modern Next.js starter template integrated with Hygraph CMS, designed to be easily forkable and adjustable to your project needs. It features a complete setup with shadcn/ui, Hygraph live preview support, and GraphQL code generation.
- Next.js 15+: Utilizes the latest App Router architecture
- Hygraph CMS Integration: Ready-to-use GraphQL client and query setup
- TypeScript: Fully typed codebase with GraphQL type generation
- Live Preview: Real-time content preview with Draft Mode support
- shadcn/ui: Pre-configured component library with Tailwind CSS
- GraphQL Codegen: Automated type generation from GraphQL schemas
Before you start, make sure you have a Hygraph account and project set up
Create a .env file in the root of your project with the following variables:
# Required: Your Hygraph API endpoint
HYGRAPH_ENDPOINT="https://api-xx-xxx.hygraph.com/v2/your-project-id/master"
# Required for Preview Mode: Your Hygraph Content API token with appropriate permissions
HYGRAPH_PREVIEW_TOKEN="your-preview-token"
# Required for Preview Mode: A secret string used to secure preview requests
HYGRAPH_PREVIEW_SECRET="your-random-secret-string"# Install dependencies
npm install
# or
yarn
# or
pnpm install
# or
bun install
# Generate GraphQL types (requires environment variables)
npm run codegen
# or
yarn codegen
# or
pnpm codegen
# or
bun codegen
# Start the development server
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 with your browser to see the result.
├── codegen.ts # GraphQL code generation configuration
├── components.json # shadcn/ui configuration
├── src/
│ ├── app/ # Next.js App Router pages
│ │ ├── actions.ts # Server actions (for Draft Mode)
│ │ ├── api/
│ │ │ └── draft/ # Draft mode API route
│ │ │ └── route.ts
│ ├── components/
│ │ ├── draft-mode-toast.tsx # UI component for Draft Mode indication
│ │ └── ui/ # shadcn/ui components
│ └── lib/
│ ├── utils.ts # Utility functions
│ └── hygraph/ # Hygraph integration
│ ├── __generated/ # Auto-generated GraphQL types
│ ├── queries/ # GraphQL query files
│ └── index.ts # GraphQL client setup
The GraphQL server client is set up in src/lib/hygraph/index.ts and provides:
- Automatic environment-based configuration
- Draft mode support through preview tokens
- Type-safe SDK generation from GraphQL operations
import { getHygraphSdk } from "@/lib/hygraph";
...
const sdk = getHygraphSdk();
const { page } = await sdk.singlePage({ slug });
The client-side hook is provided in src/lib/hygraph/useHygraphSdk.ts and offers type-safe data fetching for client components using SWR:
'use client';
import { useHygraphSdk } from '@/lib/hygraph/useHygraphSdk';
function ClientComponent({ slug }: { slug: string }) {
const { data, error, isLoading } = useHygraphSdk('singlePage', {
slug,
});
if (isLoading) return <p>Loading...</p>;
if (error) return <p>Error loading page</p>;
return <h1>{data?.page?.title}</h1>;
}Features:
- Type Safety: The hook maintains full type safety with your GraphQL operations. TypeScript will enforce required variables and provide proper return types.
- Automatic Fetching: Uses SWR under the hood for data fetching with caching, revalidation, and focus tracking.
- Smart Variable Handling: The hook distinguishes between operations that require variables and those where variables are optional.
- Optimized for Development: Disables revalidation on focus during development for a smoother experience.
The hook calls the /api/hygraph endpoint which proxies requests to your Hygraph API, ensuring your API tokens remain secure on the server.
The project uses GraphQL Code Generator to create TypeScript types from your GraphQL schema and operations:
- Write your GraphQL queries in
.graphqlfiles undersrc/lib/hygraph/queries/ - Run
npm run codegento generate TypeScript types - Import and use the generated types and operations in your components
The codegen configuration (codegen.ts) is set up to:
- Read your schema from the Hygraph endpoint
- Process all
.graphqlfiles in the queries directory - Generate fully typed GraphQL operations and SDK
The starter includes a complete implementation of Hygraph's Live Preview feature:
- The
/api/draft/route.tsendpoint enables draft mode when requested from Hygraph - The
DraftModeToastcomponent shows when draft mode is active - The Hygraph client detects draft mode and sends the preview token when needed
This starter can be deployed on any platform that supports Next.js, such as Vercel, Netlify, or a custom server.
Make sure to add your environment variables to your deployment platform.