The easiest way to add GraphQL to any Nitro application
🚀 Auto-discovery • 📝 Type Generation • 🎮 Apollo Sandbox • 🔧 Zero Config
- Nuxt 4 Integration - Step-by-step Nuxt setup
- Standalone Nitro - Basic Nitro integration
- ⚡ 5-minute setup - From zero to GraphQL in minutes
- 🔍 Auto-discovery - Scans your files, builds your schema
- 📝 Type-safe - Full TypeScript support with auto-generated types
- 🎯 Universal - Works with Nuxt, Nitro, and any Nitro-based framework
- 🎮 Developer-friendly - Built-in Apollo Sandbox for testing
- 🔧 Zero config - Sensible defaults, customize when needed
GraphQL Yoga (recommended):
pnpm add nitro-graphql graphql-yoga graphql
Apollo Server:
pnpm add nitro-graphql @apollo/server @apollo/utils.withrequired @as-integrations/h3 graphql
🔧 Nitro Project
// nitro.config.ts
import { defineNitroConfig } from 'nitropack/config'
export default defineNitroConfig({
modules: ['nitro-graphql'],
graphql: {
framework: 'graphql-yoga', // or 'apollo-server'
},
})
🟢 Nuxt Project
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['nitro-graphql/nuxt'],
nitro: {
graphql: {
framework: 'graphql-yoga',
},
},
})
# server/graphql/schema.graphql
type Query {
hello: String!
greeting(name: String!): String!
}
type Mutation {
_empty: String
}
// server/graphql/hello.resolver.ts
export const helloResolver = defineResolver({
Query: {
hello: () => 'Hello from GraphQL!',
greeting: (_, { name }) => `Hello, ${name}!`,
},
})
pnpm dev
🎉 That's it! Your GraphQL server is ready at:
- Endpoint:
http://localhost:3000/api/graphql
- Playground:
http://localhost:3000/api/graphql
(browser) - Health:
http://localhost:3000/api/graphql/health
Try these working examples:
Example | Description | Demo |
---|---|---|
Nitro Basic | Standalone Nitro with GraphQL | pnpm playground:nitro |
Nuxt Integration | Full Nuxt app with client types | pnpm playground:nuxt |
Apollo Federation | Federated GraphQL services | pnpm playground:federation |
Let's create a complete user management system:
# server/graphql/users/user.graphql
type User {
id: ID!
name: String!
email: String!
createdAt: DateTime!
}
input CreateUserInput {
name: String!
email: String!
}
extend type Query {
users: [User!]!
user(id: ID!): User
}
extend type Mutation {
createUser(input: CreateUserInput!): User!
}
// server/graphql/users/user.resolver.ts
export const userQueries = defineQuery({
users: async (_, __, { storage }) => {
return await storage.getItem('users') || []
},
user: async (_, { id }, { storage }) => {
const users = await storage.getItem('users') || []
return users.find(user => user.id === id)
}
})
export const userMutations = defineMutation({
createUser: async (_, { input }, { storage }) => {
const users = await storage.getItem('users') || []
const user = {
id: Date.now().toString(),
...input,
createdAt: new Date()
}
users.push(user)
await storage.setItem('users', users)
return user
}
})
mutation {
createUser(input: {
name: "John Doe"
email: "john@example.com"
}) {
id
name
email
createdAt
}
}
query {
users {
id
name
email
}
}
🎭 Custom Directives
Create reusable GraphQL directives:
// server/graphql/directives/auth.directive.ts
export const authDirective = defineDirective({
name: 'auth',
locations: ['FIELD_DEFINITION'],
args: {
requires: { type: 'String', defaultValue: 'USER' }
},
transformer: (schema) => {
// Add authentication logic
}
})
Use in schema:
type Query {
users: [User!]! @auth(requires: "ADMIN")
profile: User! @auth
}
🌐 External GraphQL Services
Connect to multiple GraphQL APIs:
// nuxt.config.ts
export default defineNuxtConfig({
nitro: {
graphql: {
framework: 'graphql-yoga',
externalServices: [
{
name: 'github',
schema: 'https://api.github.com/graphql',
endpoint: 'https://api.github.com/graphql',
headers: () => ({
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
})
}
]
}
}
})
🔄 Apollo Federation
Build federated GraphQL services:
// nitro.config.ts
export default defineNitroConfig({
graphql: {
framework: 'apollo-server',
federation: {
enabled: true,
serviceName: 'users-service'
}
}
})
All utilities are auto-imported in resolver files:
Function | Purpose | Example |
---|---|---|
defineResolver |
Complete resolvers | defineResolver({ Query: {...}, Mutation: {...} }) |
defineQuery |
Query-only resolvers | defineQuery({ users: () => [...] }) |
defineMutation |
Mutation-only resolvers | defineMutation({ createUser: (...) => {...} }) |
defineType |
Custom type resolvers | defineType({ User: { posts: (parent) => [...] } }) |
defineDirective |
Custom directives | defineDirective({ name: 'auth', ... }) |
Automatic TypeScript types are generated:
- Server types:
#graphql/server
- Use in resolvers and server code - Client types:
#graphql/client
- Use in frontend components
// Server-side
import type { User, CreateUserInput } from '#graphql/server'
// Client-side
import type { GetUsersQuery, CreateUserMutation } from '#graphql/client'
server/
├── graphql/
│ ├── schema.graphql # Main schema
│ ├── hello.resolver.ts # Basic resolvers
│ ├── users/
│ │ ├── user.graphql # User schema
│ │ └── user.resolver.ts # User resolvers
│ ├── directives/ # Custom directives
│ └── config.ts # Optional GraphQL config
⚠️ Important: Use named exports for all resolvers:// ✅ Correct export const userQueries = defineQuery({...}) // ❌ Deprecated export default defineQuery({...})
Common Issues
GraphQL endpoint returns 404
- ✅ Check
nitro-graphql
is in modules - ✅ Set
graphql.framework
option - ✅ Create at least one
.graphql
file
Types not generating
- ✅ Restart dev server
- ✅ Check file naming:
*.graphql
,*.resolver.ts
- ✅ Verify exports are named exports
Import errors
- ✅ Use correct path:
nitro-graphql/utils/define
- ✅ Use named exports in resolvers
This package powers production applications:
- Nitroping - Self-hosted push notification service
# Install dependencies
pnpm install
# Build module
pnpm build
# Watch mode
pnpm dev
# Run playgrounds
pnpm playground:nitro
pnpm playground:nuxt
pnpm playground:federation
# Lint
pnpm lint
Tip
Want to contribute? We believe you can play a role in the growth of this project!
- 💡 Share ideas via GitHub Issues
- 🐛 Report bugs with detailed information
- 📖 Improve docs - README, examples, guides
- 🔧 Code contributions - Bug fixes and features
- 🌟 Star the project to show support
- Performance benchmarks
- Video tutorials
- Database adapter guides
- VS Code extension
MIT License © 2023 productdevbook