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.
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 dev
Open 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:
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:
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:
.graphql
files under src/lib/hygraph/queries/
npm run codegen
to generate TypeScript typesThe codegen configuration (codegen.ts
) is set up to:
.graphql
files in the queries directoryThe starter includes a complete implementation of Hygraph's Live Preview feature:
/api/draft/route.ts
endpoint enables draft mode when requested from HygraphDraftModeToast
component shows when draft mode is activeThis 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.