nitro-graphql 2.0.0-beta.54 → 2.0.0-beta.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +18 -813
  2. package/dist/core/constants.d.mts +2 -2
  3. package/dist/core/constants.mjs +2 -2
  4. package/dist/core/index.d.mts +2 -2
  5. package/dist/core/index.mjs +2 -2
  6. package/dist/core/manifest.mjs +3 -3
  7. package/dist/core/pubsub/index.d.mts +109 -0
  8. package/dist/core/pubsub/index.mjs +137 -0
  9. package/dist/core/scanning/common.mjs +1 -1
  10. package/dist/core/scanning/documents.d.mts +1 -1
  11. package/dist/core/scanning/documents.mjs +1 -1
  12. package/dist/core/scanning/schemas.d.mts +1 -1
  13. package/dist/core/scanning/schemas.mjs +1 -1
  14. package/dist/core/schema/builder.d.mts +2 -2
  15. package/dist/core/schema/builder.mjs +3 -3
  16. package/dist/core/schema/index.d.mts +2 -2
  17. package/dist/core/schema/index.mjs +2 -2
  18. package/dist/define.d.mts +2 -1
  19. package/dist/define.mjs +3 -1
  20. package/dist/nitro/codegen.mjs +9 -8
  21. package/dist/nitro/index.d.mts +2 -2
  22. package/dist/nitro/routes/apollo-server-ws.d.mts +16 -0
  23. package/dist/nitro/routes/apollo-server-ws.mjs +53 -0
  24. package/dist/nitro/routes/apollo-server.d.mts +2 -2
  25. package/dist/nitro/routes/debug.d.mts +2 -2
  26. package/dist/nitro/routes/graphql-yoga-ws.d.mts +13 -0
  27. package/dist/nitro/routes/graphql-yoga-ws.mjs +53 -0
  28. package/dist/nitro/routes/graphql-yoga.d.mts +2 -2
  29. package/dist/nitro/routes/health.d.mts +2 -2
  30. package/dist/nitro/setup/rollup-integration.mjs +5 -1
  31. package/dist/nitro/setup/routes.mjs +30 -10
  32. package/dist/nitro/setup/scanner.d.mts +2 -1
  33. package/dist/nitro/setup.mjs +4 -0
  34. package/dist/nitro/types.d.mts +81 -1
  35. package/dist/nitro/virtual/generators.d.mts +5 -1
  36. package/dist/nitro/virtual/generators.mjs +18 -2
  37. package/dist/nitro/virtual/stubs.d.mts +2 -1
  38. package/dist/nitro/virtual/stubs.mjs +2 -1
  39. package/native/index.js +52 -52
  40. package/package.json +19 -6
package/README.md CHANGED
@@ -13,846 +13,51 @@
13
13
 
14
14
  **The easiest way to add GraphQL to any Nitro application**
15
15
 
16
- 🚀 **Auto-discovery** 📝 **Type Generation** • 🎮 **Apollo Sandbox** • 🔧 **Zero Config**
17
-
18
- [📚 Documentation](https://nitro-graphql.pages.dev) • [Quick Start](#-quick-start) • [Examples](#-examples) • [Community](#-community)
16
+ [Documentation](https://nitro-graphql.pages.dev) | [Examples](./examples/) | [Playgrounds](./playgrounds/)
19
17
 
20
18
  </div>
21
19
 
22
20
  > [!IMPORTANT]
23
- > **v2.0 Beta (Current - Main Branch)**
24
- > This is the **v2.0** beta branch with Nitro v3 and H3 v2 support. Includes Rolldown optimization, improved chunking, and enhanced Vite integration.
25
- >
26
- > **Looking for v1.x?**
27
- > For the stable v1 version (Nitro v2), see the [`v1` branch](https://github.com/productdevbook/nitro-graphql/tree/v1).
28
-
29
- ---
30
-
31
- ## 🎥 Watch & Learn
32
-
33
- - [**Nuxt 4 Integration**](https://x.com/productdevbook/status/1947314569531076633) - Step-by-step Nuxt setup
34
- - [**Standalone Nitro**](https://x.com/productdevbook/status/1945759751393976348) - Basic Nitro integration
35
-
36
- ## ✨ Why Nitro GraphQL?
37
-
38
- - ⚡ **5-minute setup** - From zero to GraphQL in minutes
39
- - 🔍 **Auto-discovery** - Scans your files, builds your schema
40
- - 📝 **Type-safe** - Full TypeScript support with auto-generated types
41
- - 🎯 **Universal** - Works with Nuxt, Nitro, and any Nitro-based framework
42
- - 🎮 **Developer-friendly** - Built-in Apollo Sandbox for testing
43
- - 🔧 **Zero config** - Sensible defaults, customize when needed
44
-
45
- ### 🆕 What's New in v2.0 Beta
46
-
47
- - 🚀 **Nitro v3 & H3 v2** - Full compatibility with the latest Nitro and H3
48
- - ⚙️ **Rolldown Support** - Optimized for both Rolldown (Vite 7+) and Rollup
49
- - 📦 **Smart Chunking** - GraphQL code split into separate chunks (~98% size reduction)
50
- - 🔍 **Debug Dashboard** - Built-in diagnostics at `/_nitro/graphql/debug` (dev only)
51
- - 🎨 **Enhanced Vite Integration** - Better custom path support and virtual module resolution
21
+ > **v2.0 Beta** - Nitro v3 / H3 v2 support. For v1.x (Nitro v2), see the [`v1` branch](https://github.com/productdevbook/nitro-graphql/tree/v1).
52
22
 
53
- ## 🚀 Quick Start
23
+ ## Quick Start
54
24
 
55
- ### 1. Install
56
-
57
- **GraphQL Yoga (recommended):**
58
- ```bash
59
- pnpm add nitro-graphql@beta graphql-yoga graphql graphql-config
60
- ```
61
-
62
- **Apollo Server:**
63
25
  ```bash
64
- pnpm add nitro-graphql@beta @apollo/server graphql graphql-config
26
+ npx nitro-graphql@beta init my-app -t nitro
65
27
  ```
66
28
 
67
- ### 2. Configure
29
+ Available templates: `nitro`, `vite`, `vite-react`, `vite-vue`, `drizzle-orm`, `better-auth`
68
30
 
69
- <details>
70
- <summary>🔧 <strong>Nitro Project</strong></summary>
71
-
72
- ```ts
73
- // nitro.config.ts
74
- import graphql from 'nitro-graphql'
75
- import { defineNitroConfig } from 'nitro/config'
76
-
77
- export default defineNitroConfig({
78
- modules: [
79
- graphql({
80
- framework: 'graphql-yoga', // or 'apollo-server'
81
- }),
82
- ],
83
- })
84
- ```
85
-
86
- </details>
87
-
88
- <details>
89
- <summary>⚡ <strong>Vite + Nitro Project</strong></summary>
90
-
91
- ```ts
92
- // vite.config.ts
93
- import { defineConfig } from 'vite'
94
- import { nitro } from 'nitro/vite'
95
- import graphql from 'nitro-graphql'
96
- import { graphql as graphqlVite } from 'nitro-graphql/vite'
97
-
98
- export default defineConfig({
99
- plugins: [
100
- graphqlVite(), // ⚠️ Must be before nitro()
101
- nitro(),
102
- ],
103
- nitro: {
104
- modules: [
105
- graphql({
106
- framework: 'graphql-yoga',
107
- }),
108
- ],
109
- },
110
- })
111
- ```
112
-
113
- > **⚠️ Important**: The `graphql()` plugin must be placed **before** `nitro()` to prevent Vite from trying to parse GraphQL files as JavaScript.
114
-
115
- </details>
116
-
117
- <details>
118
- <summary>🟢 <strong>Nuxt Project</strong></summary>
119
-
120
- ```ts
121
- // nuxt.config.ts
122
- export default defineNuxtConfig({
123
- modules: ['nitro-graphql/nuxt'],
124
- nitro: {
125
- graphql: {
126
- framework: 'graphql-yoga',
127
- },
128
- },
129
- })
130
- ```
131
-
132
- </details>
133
-
134
- ### 3. Create Your Schema
135
-
136
- ```graphql
137
- # server/graphql/schema.graphql
138
- type Query {
139
- hello: String!
140
- greeting(name: String!): String!
141
- }
142
-
143
- type Mutation {
144
- _empty: String
145
- }
146
- ```
147
-
148
- ### 4. Add Resolvers
149
-
150
- ```ts
151
- // server/graphql/hello.resolver.ts
152
- import { defineResolver } from 'nitro-graphql/define'
153
-
154
- export const helloResolver = defineResolver({
155
- Query: {
156
- hello: () => 'Hello from GraphQL!',
157
- greeting: (_, { name }) => `Hello, ${name}!`,
158
- },
159
- })
160
- ```
161
-
162
- ### 5. Start Development
31
+ ## Manual Installation
163
32
 
164
33
  ```bash
165
- pnpm dev
166
- ```
167
-
168
- 🎉 **That's it!** Your GraphQL server is ready at:
169
- - **Endpoint**: `http://localhost:3000/api/graphql`
170
- - **Playground**: `http://localhost:3000/api/graphql` (browser)
171
- - **Health**: `http://localhost:3000/api/graphql/health`
172
- - **Debug Dashboard**: `http://localhost:3000/_nitro/graphql/debug` (dev mode only)
173
-
174
- ## 🎮 Examples
175
-
176
- Try these working examples:
177
-
178
- | Example | Description | Demo |
179
- |---------|-------------|------|
180
- | [**Nitro Basic**](./playgrounds/nitro/) | Standalone Nitro with GraphQL | `pnpm playground:nitro` |
181
- | [**Vite + Nitro**](./playgrounds/vite/) | Vite with Nitro GraphQL integration | `cd playgrounds/vite && pnpm dev` |
182
- | [**Nuxt Integration**](./playgrounds/nuxt/) | Full Nuxt app with client types | `pnpm playground:nuxt` |
183
- | [**Apollo Federation**](./playgrounds/federation/) | Federated GraphQL services | `pnpm playground:federation` |
184
- | [**Drizzle ORM**](./examples/drizzle-orm/) | Drizzle ORM + Zod validation integration | `cd examples/drizzle-orm && pnpm dev` |
185
-
186
- ### 🧪 Test Projects
187
-
188
- Real-world test projects using nitro-graphql v2:
189
-
190
- - [**Vite + Nitro + Rolldown**](https://github.com/productdevbook/vite-nitro-graphql/tree/rolldown) - Testing with Rolldown bundler (Vite 7+)
191
- - [**Vite + Nitro (Main)**](https://github.com/productdevbook/vite-nitro-graphql) - Standard Vite integration tests
192
-
193
- ## 🏗️ Building Your First Feature
194
-
195
- Let's create a complete user management system:
196
-
197
- ### 1. Define Schema
198
- ```graphql
199
- # server/graphql/users/user.graphql
200
- type User {
201
- id: ID!
202
- name: String!
203
- email: String!
204
- createdAt: DateTime!
205
- }
206
-
207
- input CreateUserInput {
208
- name: String!
209
- email: String!
210
- }
211
-
212
- extend type Query {
213
- users: [User!]!
214
- user(id: ID!): User
215
- }
216
-
217
- extend type Mutation {
218
- createUser(input: CreateUserInput!): User!
219
- }
220
- ```
221
-
222
- ### 2. Create Resolvers
223
- ```ts
224
- // server/graphql/users/user.resolver.ts
225
- import { defineQuery, defineMutation } from 'nitro-graphql/define'
226
-
227
- export const userQueries = defineQuery({
228
- users: async (_, __, { storage }) => {
229
- return await storage.getItem('users') || []
230
- },
231
- user: async (_, { id }, { storage }) => {
232
- const users = await storage.getItem('users') || []
233
- return users.find(user => user.id === id)
234
- }
235
- })
236
-
237
- export const userMutations = defineMutation({
238
- createUser: async (_, { input }, { storage }) => {
239
- const users = await storage.getItem('users') || []
240
- const user = {
241
- id: Date.now().toString(),
242
- ...input,
243
- createdAt: new Date()
244
- }
245
- users.push(user)
246
- await storage.setItem('users', users)
247
- return user
248
- }
249
- })
250
- ```
251
-
252
- ### 3. Test in Apollo Sandbox
253
- ```graphql
254
- mutation {
255
- createUser(input: {
256
- name: "John Doe"
257
- email: "john@example.com"
258
- }) {
259
- id
260
- name
261
- email
262
- createdAt
263
- }
264
- }
265
-
266
- query {
267
- users {
268
- id
269
- name
270
- email
271
- }
272
- }
34
+ pnpm add nitro-graphql@beta graphql-yoga graphql graphql-config
273
35
  ```
274
36
 
275
- ## 🚀 Advanced Features
276
-
277
- <details>
278
- <summary><strong>🎛️ Custom File Generation & Paths</strong></summary>
279
-
280
- Control which files are auto-generated and customize their output paths. Perfect for library development, monorepos, or custom project structures.
281
-
282
- ### Library Mode
283
-
284
- Disable all scaffold files for library/module development:
285
-
286
37
  ```ts
287
38
  // nitro.config.ts
39
+ import { defineConfig } from 'nitro'
288
40
  import graphql from 'nitro-graphql'
289
- import { defineNitroConfig } from 'nitro/config'
290
-
291
- export default defineNitroConfig({
292
- modules: [
293
- graphql({
294
- framework: 'graphql-yoga',
295
- scaffold: false, // Disable all scaffold files
296
- clientUtils: false, // Disable client utilities
297
- }),
298
- ],
299
- })
300
- ```
301
41
 
302
- ### Fine-Grained Control
303
-
304
- Control each file individually:
305
-
306
- ```ts
307
- import graphql from 'nitro-graphql'
308
- import { defineNitroConfig } from 'nitro/config'
309
-
310
- export default defineNitroConfig({
311
- modules: [
312
- graphql({
313
- framework: 'graphql-yoga',
314
-
315
- // Scaffold files
316
- scaffold: {
317
- graphqlConfig: false, // Don't generate graphql.config.ts
318
- serverSchema: true, // Generate server/graphql/schema.ts
319
- serverConfig: true, // Generate server/graphql/config.ts
320
- serverContext: false, // Don't generate server/graphql/context.ts
321
- },
322
-
323
- // Client utilities (Nuxt only)
324
- clientUtils: {
325
- index: true, // Generate app/graphql/index.ts
326
- ofetch: false, // Don't generate ofetch wrappers
327
- },
328
-
329
- // SDK files
330
- sdk: {
331
- main: true, // Generate default SDK
332
- external: true, // Generate external service SDKs
333
- },
334
-
335
- // Type files
336
- types: {
337
- server: true, // Generate server types
338
- client: true, // Generate client types
339
- external: true, // Generate external service types
340
- },
341
- }),
342
- ],
343
- })
344
- ```
345
-
346
- ### Custom Paths
347
-
348
- Customize where files are generated:
349
-
350
- ```ts
351
- import graphql from 'nitro-graphql'
352
- import { defineNitroConfig } from 'nitro/config'
353
-
354
- export default defineNitroConfig({
42
+ export default defineConfig({
43
+ serverDir: './',
355
44
  modules: [
356
45
  graphql({
357
46
  framework: 'graphql-yoga',
358
-
359
- // Method 1: Global paths (affects all files)
360
- serverDir: 'src/server/graphql',
361
- clientDir: 'src/client/graphql',
362
- typesDir: '.build/types',
363
-
364
- // Method 2: Specific file paths
365
- scaffold: {
366
- serverSchema: 'lib/graphql/schema.ts',
367
- serverConfig: 'lib/graphql/config.ts',
368
- },
369
-
370
- sdk: {
371
- main: 'app/graphql/organization/sdk.ts',
372
- external: 'app/graphql/{serviceName}/client-sdk.ts',
373
- },
374
-
375
- types: {
376
- server: 'types/graphql-server.d.ts',
377
- client: 'types/graphql-client.d.ts',
378
- },
379
- }),
380
- ],
381
- })
382
- ```
383
-
384
- ### Path Placeholders
385
-
386
- Use placeholders in custom paths:
387
-
388
- | Placeholder | Description | Example |
389
- |------------|-------------|---------|
390
- | `{serviceName}` | External service name | `github`, `stripe` |
391
- | `{buildDir}` | Build directory | `.nitro` or `.nuxt` |
392
- | `{rootDir}` | Root directory | `/Users/you/project` |
393
- | `{framework}` | Framework name | `nuxt` or `nitro` |
394
- | `{typesDir}` | Types directory | `.nitro/types` |
395
- | `{serverDir}` | Server GraphQL dir | `server/graphql` |
396
- | `{clientDir}` | Client GraphQL dir | `app/graphql` |
397
-
398
- Example:
399
- ```ts
400
- sdk: {
401
- external: '{clientDir}/{serviceName}/sdk.ts'
402
- }
403
- // → app/graphql/github/sdk.ts
404
- // → app/graphql/stripe/sdk.ts
405
- ```
406
-
407
- ### Service-Specific Paths
408
-
409
- Customize paths for individual external services:
410
-
411
- ```ts
412
- export default defineNuxtConfig({
413
- nitro: {
414
- graphql: {
415
- framework: 'graphql-yoga',
416
-
417
- // Global default for all external services
418
- sdk: {
419
- external: 'app/graphql/{serviceName}/sdk.ts'
420
- },
421
-
422
- externalServices: [
423
- {
424
- name: 'github',
425
- endpoint: 'https://api.github.com/graphql',
426
- schema: 'https://api.github.com/graphql',
427
-
428
- // GitHub-specific paths (override global config)
429
- paths: {
430
- sdk: 'app/graphql/organization/github-sdk.ts',
431
- types: 'types/github.d.ts',
432
- ofetch: 'app/graphql/organization/github-client.ts'
433
- }
434
- },
435
- {
436
- name: 'stripe',
437
- endpoint: 'https://api.stripe.com/graphql',
438
- schema: 'https://api.stripe.com/graphql',
439
-
440
- // Stripe-specific paths
441
- paths: {
442
- sdk: 'app/graphql/payments/stripe-sdk.ts',
443
- types: 'types/payments/stripe.d.ts',
444
- // ofetch uses global config
445
- }
446
- },
447
- {
448
- name: 'shopify',
449
- endpoint: 'https://api.shopify.com/graphql',
450
- // No paths → uses global config
451
- // → app/graphql/shopify/sdk.ts
452
- }
453
- ]
454
- }
455
- }
456
- })
457
- ```
458
-
459
- ### Path Resolution Priority
460
-
461
- When resolving file paths, the system follows this priority order:
462
-
463
- 1. **Service-specific path** (for external services): `service.paths.sdk`
464
- 2. **Category config**: `sdk.external` or `sdk.main`
465
- 3. **Global paths**: `clientDir`
466
- 4. **Framework defaults**: Nuxt vs Nitro defaults
467
-
468
- Example:
469
- ```ts
470
- // Given this config:
471
- {
472
- clientDir: 'custom/graphql',
473
- sdk: { external: '{clientDir}/{serviceName}/sdk.ts' },
474
- externalServices: [
475
- {
476
- name: 'github',
477
- paths: { sdk: 'app/org/github-sdk.ts' } // ← Wins (priority 1)
478
- },
479
- {
480
- name: 'stripe',
481
- // Uses sdk.external (priority 2)
482
- // → custom/graphql/stripe/sdk.ts
483
- }
484
- ]
485
- }
486
- ```
487
-
488
- ### Use Cases
489
-
490
- **Monorepo structure:**
491
- ```ts
492
- {
493
- serverDir: 'packages/api/src/graphql',
494
- clientDir: 'packages/web/src/graphql',
495
- typesDir: 'packages/types/src/generated',
496
- }
497
- ```
498
-
499
- **Multiple external service organizations:**
500
- ```ts
501
- externalServices: [
502
- {
503
- name: 'github',
504
- paths: { sdk: 'app/graphql/vcs/github-sdk.ts' }
505
- },
506
- {
507
- name: 'gitlab',
508
- paths: { sdk: 'app/graphql/vcs/gitlab-sdk.ts' }
509
- },
510
- {
511
- name: 'stripe',
512
- paths: { sdk: 'app/graphql/billing/stripe-sdk.ts' }
513
- }
514
- ]
515
- ```
516
-
517
- **Library development (no scaffolding):**
518
- ```ts
519
- {
520
- scaffold: false,
521
- clientUtils: false,
522
- sdk: { enabled: true }, // Only generate SDKs
523
- types: { enabled: true }, // Only generate types
524
- }
525
- ```
526
-
527
- </details>
528
-
529
- <details>
530
- <summary><strong>🎭 Custom Directives</strong></summary>
531
-
532
- Create reusable GraphQL directives:
533
-
534
- ```ts
535
- // server/graphql/directives/auth.directive.ts
536
- import { defineDirective } from 'nitro-graphql/define'
537
-
538
- export const authDirective = defineDirective({
539
- name: 'auth',
540
- locations: ['FIELD_DEFINITION'],
541
- args: {
542
- requires: { type: 'String', defaultValue: 'USER' }
543
- },
544
- transformer: (schema) => {
545
- // Add authentication logic
546
- }
547
- })
548
- ```
549
-
550
- Use in schema:
551
- ```graphql
552
- type Query {
553
- users: [User!]! @auth(requires: "ADMIN")
554
- profile: User! @auth
555
- }
556
- ```
557
-
558
- </details>
559
-
560
- <details>
561
- <summary><strong>🌐 External GraphQL Services</strong></summary>
562
-
563
- Connect to multiple GraphQL APIs:
564
-
565
- ```ts
566
- // nuxt.config.ts
567
- export default defineNuxtConfig({
568
- nitro: {
569
- graphql: {
570
- framework: 'graphql-yoga',
571
- externalServices: [
572
- {
573
- name: 'github',
574
- schema: 'https://api.github.com/graphql',
575
- endpoint: 'https://api.github.com/graphql',
576
- headers: () => ({
577
- Authorization: `Bearer ${process.env.GITHUB_TOKEN}`
578
- })
579
- }
580
- ]
581
- }
582
- }
583
- })
584
- ```
585
-
586
- </details>
587
-
588
- <details>
589
- <summary><strong>🔄 Apollo Federation</strong></summary>
590
-
591
- Build federated GraphQL services:
592
-
593
- ```ts
594
- // nitro.config.ts
595
- import graphql from 'nitro-graphql'
596
- import { defineNitroConfig } from 'nitro/config'
597
-
598
- export default defineNitroConfig({
599
- modules: [
600
- graphql({
601
- framework: 'apollo-server',
602
- federation: {
603
- enabled: true,
604
- serviceName: 'users-service',
605
- },
47
+ serverDir: './',
606
48
  }),
607
49
  ],
608
50
  })
609
51
  ```
610
52
 
611
- </details>
612
-
613
- ## 📖 Documentation
614
-
615
- ### Core Utilities
616
-
617
- > **⚠️ Breaking Change**: Utilities are **NOT auto-imported**. You must explicitly import them from `nitro-graphql/define`:
618
-
619
- ```typescript
620
- import { defineResolver, defineQuery, defineMutation, defineField, defineDirective } from 'nitro-graphql/define'
621
- ```
622
-
623
- | Function | Purpose | Example |
624
- |----------|---------|---------|
625
- | `defineResolver` | Complete resolvers | `defineResolver({ Query: {...}, Mutation: {...} })` |
626
- | `defineQuery` | Query-only resolvers | `defineQuery({ users: () => [...] })` |
627
- | `defineMutation` | Mutation-only resolvers | `defineMutation({ createUser: (...) => {...} })` |
628
- | `defineField` | Custom type resolvers | `defineField({ User: { posts: (parent) => [...] } })` |
629
- | `defineDirective` | Custom directives | `defineDirective({ name: 'auth', ... })` |
630
- | `defineGraphQLConfig` | GraphQL server config | `defineGraphQLConfig({ maskedErrors: {...} })` |
631
- | `defineSchema` | Schema with Zod integration | `defineSchema({ Book: selectBookSchema })` |
632
-
633
- **Additional Utilities** from `nitro-graphql/core`:
634
- - `createDefaultMaskError()` - Error handler for ZodError and HTTPError (use in `defineGraphQLConfig`)
635
-
636
- ### Type Generation
637
-
638
- Automatic TypeScript types are generated:
639
- - **Server types**: `#graphql/server` - Use in resolvers and server code
640
- - **Client types**: `#graphql/client` - Use in frontend components
641
-
642
- ```ts
643
- // Server-side
644
- import type { User, CreateUserInput } from '#graphql/server'
645
-
646
- // Client-side
647
- import type { GetUsersQuery, CreateUserMutation } from '#graphql/client'
648
- ```
649
-
650
- ### Project Structure
651
-
652
- ```
653
- server/
654
- ├── graphql/
655
- │ ├── schema.graphql # Main schema
656
- │ ├── hello.resolver.ts # Basic resolvers
657
- │ ├── users/
658
- │ │ ├── user.graphql # User schema
659
- │ │ └── user.resolver.ts # User resolvers
660
- │ ├── directives/ # Custom directives
661
- │ └── config.ts # Optional GraphQL config
662
- ```
663
-
664
- > **⚠️ Important**: Use **named exports** for all resolvers:
665
- > ```ts
666
- > // ✅ Correct
667
- > export const userQueries = defineQuery({...})
668
- >
669
- > // ❌ Deprecated
670
- > export default defineQuery({...})
671
- > ```
672
-
673
- ## 🚨 Troubleshooting
674
-
675
- <details>
676
- <summary><strong>Common Issues</strong></summary>
677
-
678
- **GraphQL endpoint returns 404**
679
- - ✅ Check `nitro-graphql` is in modules
680
- - ✅ Set `graphql.framework` option
681
- - ✅ Create at least one `.graphql` file
682
-
683
- **Types not generating**
684
- - ✅ Restart dev server
685
- - ✅ Check file naming: `*.graphql`, `*.resolver.ts`
686
- - ✅ Verify exports are named exports
687
-
688
- **Import errors / "defineQuery is not defined"**
689
- - ✅ **Requires explicit imports**: Add `import { defineQuery } from 'nitro-graphql/define'` to resolver files
690
- - ✅ Use correct import path: `nitro-graphql/define` (not `nitro-graphql`)
691
- - ✅ Use named exports in resolvers
692
-
693
- Example fix:
694
- ```typescript
695
- // Add this to the top of your resolver file
696
- import { defineQuery, defineMutation } from 'nitro-graphql/define'
697
-
698
- export const myQueries = defineQuery({ ... })
699
- ```
700
-
701
- **Vite: "Parse failure: Expected ';', '}' or <eof>" on GraphQL files**
702
- - ✅ Add `graphql()` plugin from `nitro-graphql/vite`
703
- - ✅ Ensure `graphql()` is placed **before** `nitro()` in plugins array
704
- - ✅ Example:
705
- ```ts
706
- import { graphql } from 'nitro-graphql/vite'
707
-
708
- export default defineConfig({
709
- plugins: [
710
- graphql(), // ← Must be first
711
- nitro(),
712
- ]
713
- })
714
- ```
715
-
716
- **RollupError: "[exportName]" is not exported by "[file].resolver.ts"**
717
-
718
- This error occurs when the resolver scanner can't find the expected export in your resolver file. Common causes:
719
-
720
- 1. **Using default export instead of named export** ❌
721
- ```ts
722
- // ❌ WRONG - Will not be detected
723
- export default defineQuery({
724
- users: () => [...]
725
- })
726
- ```
727
-
728
- ```ts
729
- // ✅ CORRECT - Use named export
730
- export const userQueries = defineQuery({
731
- users: () => [...]
732
- })
733
- ```
734
-
735
- 2. **Not using a define function** ❌
736
- ```ts
737
- // ❌ WRONG - Plain object won't be detected
738
- export const resolvers = {
739
- Query: {
740
- users: () => [...]
741
- }
742
- }
743
- ```
744
-
745
- ```ts
746
- // ✅ CORRECT - Use defineResolver, defineQuery, etc.
747
- export const userResolver = defineResolver({
748
- Query: {
749
- users: () => [...]
750
- }
751
- })
752
- ```
753
-
754
- 3. **File naming doesn't match export** ❌
755
- ```ts
756
- // ❌ File: uploadFile.resolver.ts but export is named differently
757
- export const fileUploader = defineMutation({...})
758
- ```
759
-
760
- ```ts
761
- // ✅ CORRECT - Export name can be anything, as long as it uses a define function
762
- export const uploadFile = defineMutation({...})
763
- export const fileUploader = defineMutation({...}) // Both work!
764
- ```
765
-
766
- 4. **Syntax errors preventing parsing**
767
- - Check for TypeScript compilation errors in the file
768
- - Ensure imports are valid
769
- - Verify no missing brackets or syntax issues
770
-
771
- **How resolver scanning works:**
772
- - The module uses `oxc-parser` to scan `.resolver.ts` files
773
- - It looks for **named exports** using these functions:
774
- - `defineResolver` - Complete resolver with Query, Mutation, etc.
775
- - `defineQuery` - Query-only resolvers
776
- - `defineMutation` - Mutation-only resolvers
777
- - `defineField` - Custom type resolvers
778
- - `defineSubscription` - Subscription resolvers
779
- - `defineDirective` - Directive resolvers
780
- - Only exports using these functions are included in the virtual module
781
-
782
- **Debugging steps:**
783
- 1. Check your resolver file uses named exports: `export const name = defineQuery({...})`
784
- 2. Verify you're using one of the define functions listed above
785
- 3. Look for TypeScript/syntax errors in the file
786
- 4. Restart the dev server after fixing
787
- 5. If issues persist, simplify the resolver to test (single query)
788
-
789
- </details>
790
-
791
- ## 🌟 Production Usage
792
-
793
- This package powers production applications:
794
-
795
- - [**Nitroping**](https://github.com/productdevbook/nitroping) - Self-hosted push notification service
796
-
797
- ### Working with Your GraphQL API
798
-
799
- Once set up, you can ask Claude Code for help with:
800
-
801
- ```
802
- "Add authentication to my GraphQL resolvers"
803
- "Create a custom @auth directive for field-level permissions"
804
- "Set up type generation for client-side queries"
805
- "Add pagination to my users query"
806
- "Connect to an external GitHub GraphQL API"
807
- "Debug: my types aren't generating in .nitro/types/"
808
- "Optimize resolver performance using DataLoader"
809
- ```
810
-
811
- ### Tips for Better Results
812
-
813
- - **Start specific**: Include your framework (Nuxt/Nitro), version, and goal
814
- - **Reference docs**: Mention "following nitro-graphql conventions" to align with best practices
815
- - **Show errors**: Paste error messages for faster debugging
816
- - **Test iteratively**: Run `pnpm dev` after each change to verify
817
-
818
- ## 🛠️ Development
819
-
820
- ```bash
821
- # Install dependencies
822
- pnpm install
823
-
824
- # Build module
825
- pnpm build
826
-
827
- # Watch mode
828
- pnpm dev
829
-
830
- # Run playgrounds
831
- pnpm playground:nitro
832
- pnpm playground:nuxt
833
- pnpm playground:federation
834
-
835
- # Lint
836
- pnpm lint
837
- ```
838
-
839
- ## 💬 Community
840
-
841
- > [!TIP]
842
- > **Want to contribute?** We believe you can play a role in the growth of this project!
53
+ See the [documentation](https://nitro-graphql.pages.dev) for Nuxt and Vite setup.
843
54
 
844
- ### Ways to Contribute
845
- - 💡 **Share ideas** via [GitHub Issues](https://github.com/productdevbook/nitro-graphql/issues)
846
- - 🐛 **Report bugs** with detailed information
847
- - 📖 **Improve docs** - README, examples, guides
848
- - 🔧 **Code contributions** - Bug fixes and features
849
- - 🌟 **Star the project** to show support
55
+ ## Resources
850
56
 
851
- ### Help Wanted
852
- - [ ] Performance benchmarks
853
- - [ ] Video tutorials
854
- - [ ] Database adapter guides
855
- - [ ] VS Code extension
57
+ - [Documentation](https://nitro-graphql.pages.dev)
58
+ - [Examples](./examples/) - Real-world integrations (Drizzle ORM, etc.)
59
+ - [Playgrounds](./playgrounds/) - Development environments
60
+ - [GitHub Issues](https://github.com/productdevbook/nitro-graphql/issues)
856
61
 
857
62
  ## Sponsors
858
63
 
@@ -878,4 +83,4 @@ pnpm lint
878
83
  [beta-src]: https://img.shields.io/npm/v/nitro-graphql/beta?style=flat&logo=rocket&logoColor=white&label=beta&color=7c3aed&colorA=080f12
879
84
  [beta-href]: https://github.com/productdevbook/nitro-graphql/releases
880
85
  [coverage-src]: https://img.shields.io/badge/coverage-63%25-green?style=flat&colorA=080f12
881
- [coverage-href]: https://github.com/productdevbook/nitro-graphql/actions/workflows/ci.yml
86
+ [coverage-href]: https://github.com/productdevbook/nitro-graphql/actions/workflows/ci.yml