# Step 1: Create a new Next.js 14+ app with TypeScript and App Router
npx create-next-app@latest graphql-codegen-demo --typescript --tailwind --eslint --app --src-dir --import-alias="@/*"
cd graphql-codegen-demo
# Generate types (runs automatically on postinstall)
npm run codegen
# Start dev server
npm run dev
How it works
GraphQL Code Generator fetches the schema from https://countries.trevorblades.com/ and scans your .tsx/.ts files for .graphql queries or inline queries.
It generates:
./src/gql/graphql.ts: Full TypeScript types for schema/operations.
./src/gql/ hooks like GetContinents (typed useQuery hook for Apollo).
Fully Typed: IntelliSense, no manual types needed. Errors at compile-time for invalid queries.
Server-Side Rendering: Uses Apollo's RSC support for getClient() in App Router.
Hot Reload: Add new .graphql files, run npm run codegen to regenerate.
Customization
Your own GraphQL server: Update schema in codegen.ts to your endpoint (e.g., http://localhost:4000/graphql).
Local schema: Use schema: './schema.graphql' and download schema with graphql-codegen --config schema=./schema.graphql.
More plugins: Add 'typescript-urql-hooks' if using urql instead.
Watch mode: graphql-codegen --watch for auto-regen.
This is a production-ready boilerplate! 🚀 Open http://localhost:3000 to see continents/countries with full typing.