nitro-graphql 2.0.0-beta.25 → 2.0.0-beta.27
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.
- package/README.md +99 -532
- package/dist/rollup.mjs +2 -2
- package/dist/routes/apollo-server.d.mts +2 -2
- package/dist/routes/debug.d.mts +2 -2
- package/dist/routes/graphql-yoga.d.mts +2 -2
- package/dist/setup.mjs +45 -49
- package/dist/utils/apollo.d.mts +1 -1
- package/dist/utils/client-codegen.d.mts +4 -1
- package/dist/utils/client-codegen.mjs +2 -2
- package/dist/utils/index.mjs +14 -4
- package/dist/utils/type-generation.d.mts +7 -2
- package/dist/utils/type-generation.mjs +43 -21
- package/package.json +58 -70
package/README.md
CHANGED
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
|
|
57
57
|
**GraphQL Yoga (recommended):**
|
|
58
58
|
```bash
|
|
59
|
-
pnpm add nitro-graphql graphql-yoga graphql graphql-config
|
|
59
|
+
pnpm add nitro-graphql@beta graphql-yoga graphql graphql-config
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
**Apollo Server:**
|
|
63
63
|
```bash
|
|
64
|
-
pnpm add nitro-graphql @apollo/server
|
|
64
|
+
pnpm add nitro-graphql@beta @apollo/server graphql graphql-config
|
|
65
65
|
```
|
|
66
66
|
|
|
67
67
|
### 2. Configure
|
|
@@ -71,13 +71,15 @@ pnpm add nitro-graphql @apollo/server @apollo/utils.withrequired graphql graphql
|
|
|
71
71
|
|
|
72
72
|
```ts
|
|
73
73
|
// nitro.config.ts
|
|
74
|
+
import graphql from 'nitro-graphql'
|
|
74
75
|
import { defineNitroConfig } from 'nitro/config'
|
|
75
76
|
|
|
76
77
|
export default defineNitroConfig({
|
|
77
|
-
modules: [
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
modules: [
|
|
79
|
+
graphql({
|
|
80
|
+
framework: 'graphql-yoga', // or 'apollo-server'
|
|
81
|
+
}),
|
|
82
|
+
],
|
|
81
83
|
})
|
|
82
84
|
```
|
|
83
85
|
|
|
@@ -90,18 +92,20 @@ export default defineNitroConfig({
|
|
|
90
92
|
// vite.config.ts
|
|
91
93
|
import { defineConfig } from 'vite'
|
|
92
94
|
import { nitro } from 'nitro/vite'
|
|
93
|
-
import
|
|
95
|
+
import graphql from 'nitro-graphql'
|
|
96
|
+
import { graphql as graphqlVite } from 'nitro-graphql/vite'
|
|
94
97
|
|
|
95
98
|
export default defineConfig({
|
|
96
99
|
plugins: [
|
|
97
|
-
|
|
100
|
+
graphqlVite(), // ⚠️ Must be before nitro()
|
|
98
101
|
nitro(),
|
|
99
102
|
],
|
|
100
103
|
nitro: {
|
|
101
|
-
modules: [
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
modules: [
|
|
105
|
+
graphql({
|
|
106
|
+
framework: 'graphql-yoga',
|
|
107
|
+
}),
|
|
108
|
+
],
|
|
105
109
|
},
|
|
106
110
|
})
|
|
107
111
|
```
|
|
@@ -281,12 +285,17 @@ Disable all scaffold files for library/module development:
|
|
|
281
285
|
|
|
282
286
|
```ts
|
|
283
287
|
// nitro.config.ts
|
|
288
|
+
import graphql from 'nitro-graphql'
|
|
289
|
+
import { defineNitroConfig } from 'nitro/config'
|
|
290
|
+
|
|
284
291
|
export default defineNitroConfig({
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
292
|
+
modules: [
|
|
293
|
+
graphql({
|
|
294
|
+
framework: 'graphql-yoga',
|
|
295
|
+
scaffold: false, // Disable all scaffold files
|
|
296
|
+
clientUtils: false, // Disable client utilities
|
|
297
|
+
}),
|
|
298
|
+
],
|
|
290
299
|
})
|
|
291
300
|
```
|
|
292
301
|
|
|
@@ -295,37 +304,42 @@ export default defineNitroConfig({
|
|
|
295
304
|
Control each file individually:
|
|
296
305
|
|
|
297
306
|
```ts
|
|
307
|
+
import graphql from 'nitro-graphql'
|
|
308
|
+
import { defineNitroConfig } from 'nitro/config'
|
|
309
|
+
|
|
298
310
|
export default defineNitroConfig({
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
// Scaffold files
|
|
303
|
-
scaffold: {
|
|
304
|
-
graphqlConfig: false, // Don't generate graphql.config.ts
|
|
305
|
-
serverSchema: true, // Generate server/graphql/schema.ts
|
|
306
|
-
serverConfig: true, // Generate server/graphql/config.ts
|
|
307
|
-
serverContext: false, // Don't generate server/graphql/context.ts
|
|
308
|
-
},
|
|
311
|
+
modules: [
|
|
312
|
+
graphql({
|
|
313
|
+
framework: 'graphql-yoga',
|
|
309
314
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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
|
+
},
|
|
315
322
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
323
|
+
// Client utilities (Nuxt only)
|
|
324
|
+
clientUtils: {
|
|
325
|
+
index: true, // Generate app/graphql/index.ts
|
|
326
|
+
ofetch: false, // Don't generate ofetch wrappers
|
|
327
|
+
},
|
|
321
328
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
+
],
|
|
329
343
|
})
|
|
330
344
|
```
|
|
331
345
|
|
|
@@ -334,34 +348,39 @@ export default defineNitroConfig({
|
|
|
334
348
|
Customize where files are generated:
|
|
335
349
|
|
|
336
350
|
```ts
|
|
351
|
+
import graphql from 'nitro-graphql'
|
|
352
|
+
import { defineNitroConfig } from 'nitro/config'
|
|
353
|
+
|
|
337
354
|
export default defineNitroConfig({
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
// Method 1: Global paths (affects all files)
|
|
342
|
-
paths: {
|
|
343
|
-
serverGraphql: 'src/server/graphql',
|
|
344
|
-
clientGraphql: 'src/client/graphql',
|
|
345
|
-
buildDir: '.build',
|
|
346
|
-
typesDir: '.build/types',
|
|
347
|
-
},
|
|
355
|
+
modules: [
|
|
356
|
+
graphql({
|
|
357
|
+
framework: 'graphql-yoga',
|
|
348
358
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
359
|
+
// Method 1: Global paths (affects all files)
|
|
360
|
+
paths: {
|
|
361
|
+
serverGraphql: 'src/server/graphql',
|
|
362
|
+
clientGraphql: 'src/client/graphql',
|
|
363
|
+
buildDir: '.build',
|
|
364
|
+
typesDir: '.build/types',
|
|
365
|
+
},
|
|
354
366
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
367
|
+
// Method 2: Specific file paths
|
|
368
|
+
scaffold: {
|
|
369
|
+
serverSchema: 'lib/graphql/schema.ts',
|
|
370
|
+
serverConfig: 'lib/graphql/config.ts',
|
|
371
|
+
},
|
|
359
372
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
373
|
+
sdk: {
|
|
374
|
+
main: 'app/graphql/organization/sdk.ts',
|
|
375
|
+
external: 'app/graphql/{serviceName}/client-sdk.ts',
|
|
376
|
+
},
|
|
377
|
+
|
|
378
|
+
types: {
|
|
379
|
+
server: 'types/graphql-server.d.ts',
|
|
380
|
+
client: 'types/graphql-client.d.ts',
|
|
381
|
+
},
|
|
382
|
+
}),
|
|
383
|
+
],
|
|
365
384
|
})
|
|
366
385
|
```
|
|
367
386
|
|
|
@@ -576,14 +595,19 @@ Build federated GraphQL services:
|
|
|
576
595
|
|
|
577
596
|
```ts
|
|
578
597
|
// nitro.config.ts
|
|
598
|
+
import graphql from 'nitro-graphql'
|
|
599
|
+
import { defineNitroConfig } from 'nitro/config'
|
|
600
|
+
|
|
579
601
|
export default defineNitroConfig({
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
602
|
+
modules: [
|
|
603
|
+
graphql({
|
|
604
|
+
framework: 'apollo-server',
|
|
605
|
+
federation: {
|
|
606
|
+
enabled: true,
|
|
607
|
+
serviceName: 'users-service',
|
|
608
|
+
},
|
|
609
|
+
}),
|
|
610
|
+
],
|
|
587
611
|
})
|
|
588
612
|
```
|
|
589
613
|
|
|
@@ -773,463 +797,6 @@ This package powers production applications:
|
|
|
773
797
|
|
|
774
798
|
- [**Nitroping**](https://github.com/productdevbook/nitroping) - Self-hosted push notification service
|
|
775
799
|
|
|
776
|
-
## 🤖 Using Claude Code
|
|
777
|
-
|
|
778
|
-
Speed up development with [Claude Code](https://claude.ai/code) — AI-powered assistance for setting up and building with nitro-graphql.
|
|
779
|
-
|
|
780
|
-
### Quick Setup Prompts
|
|
781
|
-
|
|
782
|
-
Copy and paste these prompts into Claude Code to scaffold a complete GraphQL API.
|
|
783
|
-
|
|
784
|
-
**💡 Tip**: After pasting, Claude Code will execute step-by-step and validate each action.
|
|
785
|
-
|
|
786
|
-
<details>
|
|
787
|
-
<summary>🟢 <strong>Nuxt Project</strong></summary>
|
|
788
|
-
|
|
789
|
-
```
|
|
790
|
-
## GOAL
|
|
791
|
-
Set up nitro-graphql in this Nuxt project with a User management GraphQL API.
|
|
792
|
-
|
|
793
|
-
## PREREQUISITES
|
|
794
|
-
Check if this is a Nuxt project by looking for nuxt.config.ts in the root.
|
|
795
|
-
|
|
796
|
-
## STEP 1: INSTALL DEPENDENCIES
|
|
797
|
-
Action: Run this command
|
|
798
|
-
Command: pnpm add nitro-graphql graphql-yoga graphql
|
|
799
|
-
Validation: Check package.json contains these packages
|
|
800
|
-
|
|
801
|
-
## STEP 2: CONFIGURE NUXT
|
|
802
|
-
File: nuxt.config.ts
|
|
803
|
-
Action: EDIT (add to existing config, don't replace)
|
|
804
|
-
Add these properties:
|
|
805
|
-
|
|
806
|
-
export default defineNuxtConfig({
|
|
807
|
-
modules: ['nitro-graphql/nuxt'], // Add this module
|
|
808
|
-
nitro: {
|
|
809
|
-
graphql: {
|
|
810
|
-
framework: 'graphql-yoga',
|
|
811
|
-
},
|
|
812
|
-
},
|
|
813
|
-
})
|
|
814
|
-
|
|
815
|
-
Validation: Check the file has modules array and nitro.graphql config
|
|
816
|
-
|
|
817
|
-
## STEP 3: CREATE SCHEMA
|
|
818
|
-
File: server/graphql/schema.graphql
|
|
819
|
-
Action: CREATE NEW FILE (create server/graphql/ directory if needed)
|
|
820
|
-
Content:
|
|
821
|
-
|
|
822
|
-
type User {
|
|
823
|
-
id: ID!
|
|
824
|
-
name: String!
|
|
825
|
-
email: String!
|
|
826
|
-
}
|
|
827
|
-
|
|
828
|
-
type Query {
|
|
829
|
-
users: [User!]!
|
|
830
|
-
user(id: ID!): User
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
type Mutation {
|
|
834
|
-
_empty: String
|
|
835
|
-
}
|
|
836
|
-
|
|
837
|
-
Validation: File should be in server/graphql/ directory
|
|
838
|
-
|
|
839
|
-
## STEP 4: CREATE CONTEXT (Optional but recommended)
|
|
840
|
-
File: server/graphql/context.ts
|
|
841
|
-
Action: CREATE NEW FILE (auto-generated on first run, but create manually for clarity)
|
|
842
|
-
Content:
|
|
843
|
-
|
|
844
|
-
// Extend H3 event context with custom properties
|
|
845
|
-
declare module 'h3' {
|
|
846
|
-
interface H3EventContext {
|
|
847
|
-
// Add your custom context properties here
|
|
848
|
-
// Example:
|
|
849
|
-
// db?: Database
|
|
850
|
-
// auth?: { userId: string }
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
Note: This file lets you add custom properties to resolver context
|
|
855
|
-
Validation: File exists in server/graphql/
|
|
856
|
-
|
|
857
|
-
## STEP 5: CREATE CONFIG (Optional)
|
|
858
|
-
File: server/graphql/config.ts
|
|
859
|
-
Action: CREATE NEW FILE (auto-generated, customize if needed)
|
|
860
|
-
Content:
|
|
861
|
-
|
|
862
|
-
// Custom GraphQL Yoga configuration
|
|
863
|
-
export default defineGraphQLConfig({
|
|
864
|
-
// Custom context enhancer, plugins, etc.
|
|
865
|
-
// See: https://the-guild.dev/graphql/yoga-server/docs
|
|
866
|
-
})
|
|
867
|
-
|
|
868
|
-
Note: Use this to customize GraphQL Yoga options
|
|
869
|
-
Validation: File exists in server/graphql/
|
|
870
|
-
|
|
871
|
-
## STEP 6: CREATE RESOLVERS
|
|
872
|
-
File: server/graphql/users.resolver.ts
|
|
873
|
-
Action: CREATE NEW FILE
|
|
874
|
-
Content:
|
|
875
|
-
|
|
876
|
-
// Must explicitly import (NOT auto-imported)
|
|
877
|
-
import { defineQuery } from 'nitro-graphql/define'
|
|
878
|
-
|
|
879
|
-
// ⚠️ CRITICAL: Use NAMED EXPORTS (not default export)
|
|
880
|
-
export const userQueries = defineQuery({
|
|
881
|
-
users: async (_, __, context) => {
|
|
882
|
-
// context is H3EventContext - access event, storage, etc.
|
|
883
|
-
return [
|
|
884
|
-
{ id: '1', name: 'John Doe', email: 'john@example.com' },
|
|
885
|
-
{ id: '2', name: 'Jane Smith', email: 'jane@example.com' }
|
|
886
|
-
]
|
|
887
|
-
},
|
|
888
|
-
user: async (_, { id }, context) => {
|
|
889
|
-
// Third parameter is context (H3EventContext)
|
|
890
|
-
const users = [
|
|
891
|
-
{ id: '1', name: 'John Doe', email: 'john@example.com' },
|
|
892
|
-
{ id: '2', name: 'Jane Smith', email: 'jane@example.com' }
|
|
893
|
-
]
|
|
894
|
-
return users.find(u => u.id === id) || null
|
|
895
|
-
}
|
|
896
|
-
})
|
|
897
|
-
|
|
898
|
-
Validation: File ends with .resolver.ts, has import statement, and uses named export
|
|
899
|
-
|
|
900
|
-
## STEP 7: START DEV SERVER
|
|
901
|
-
Command: pnpm dev
|
|
902
|
-
Expected Output: Server starts on http://localhost:3000
|
|
903
|
-
Wait for: "Nitro built in X ms" message
|
|
904
|
-
Note: context.ts and config.ts will auto-generate if you skipped steps 4-5
|
|
905
|
-
|
|
906
|
-
## VALIDATION CHECKLIST
|
|
907
|
-
- [ ] Navigate to http://localhost:3000/api/graphql - should show GraphQL playground
|
|
908
|
-
- [ ] Health check: http://localhost:3000/api/graphql/health - should return OK
|
|
909
|
-
- [ ] Run this query in playground:
|
|
910
|
-
```graphql
|
|
911
|
-
query {
|
|
912
|
-
users {
|
|
913
|
-
id
|
|
914
|
-
name
|
|
915
|
-
email
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
```
|
|
919
|
-
Expected: Returns 2 users
|
|
920
|
-
- [ ] Check .nuxt/types/nitro-graphql-server.d.ts exists (types auto-generated)
|
|
921
|
-
|
|
922
|
-
## FILE STRUCTURE CREATED
|
|
923
|
-
```
|
|
924
|
-
server/
|
|
925
|
-
graphql/
|
|
926
|
-
schema.graphql ← GraphQL type definitions
|
|
927
|
-
context.ts ← H3 event context augmentation (optional)
|
|
928
|
-
config.ts ← GraphQL Yoga config (optional)
|
|
929
|
-
users.resolver.ts ← Query resolvers
|
|
930
|
-
.nuxt/
|
|
931
|
-
types/
|
|
932
|
-
nitro-graphql-server.d.ts ← Auto-generated types
|
|
933
|
-
graphql.config.ts ← Auto-generated (for IDE tooling)
|
|
934
|
-
```
|
|
935
|
-
|
|
936
|
-
## CRITICAL RULES (MUST FOLLOW)
|
|
937
|
-
❌ DO NOT use default exports in resolvers
|
|
938
|
-
Wrong: export default defineQuery({...})
|
|
939
|
-
Right: export const userQueries = defineQuery({...})
|
|
940
|
-
|
|
941
|
-
❌ DO NOT name files without .resolver.ts extension
|
|
942
|
-
Wrong: users.ts or user-resolver.ts
|
|
943
|
-
Right: users.resolver.ts or user.resolver.ts
|
|
944
|
-
|
|
945
|
-
✅ DO use named exports for all resolvers
|
|
946
|
-
✅ DO place files in server/graphql/ directory
|
|
947
|
-
✅ DO restart dev server if types don't generate
|
|
948
|
-
|
|
949
|
-
## TROUBLESHOOTING
|
|
950
|
-
Issue: "GraphQL endpoint returns 404"
|
|
951
|
-
Fix: Ensure 'nitro-graphql/nuxt' is in modules array (not just 'nitro-graphql')
|
|
952
|
-
|
|
953
|
-
Issue: "defineQuery is not defined"
|
|
954
|
-
Fix: Add import statement at top of resolver file:
|
|
955
|
-
import { defineQuery } from 'nitro-graphql/define'
|
|
956
|
-
|
|
957
|
-
Issue: "Types not generating"
|
|
958
|
-
Fix: Check .nuxt/types/nitro-graphql-server.d.ts exists, if not restart dev server
|
|
959
|
-
|
|
960
|
-
Issue: "Module not found: nitro-graphql"
|
|
961
|
-
Fix: Run pnpm install again, check package.json has the package
|
|
962
|
-
|
|
963
|
-
## NEXT STEPS (After Setup Works)
|
|
964
|
-
1. Add mutations: "Add createUser and deleteUser mutations with H3 storage"
|
|
965
|
-
2. Extend context: "Add database connection to context.ts and use it in resolvers"
|
|
966
|
-
3. Use types: "Import and use TypeScript types from #graphql/server in resolvers"
|
|
967
|
-
4. Add auth: "Add authentication middleware using context in resolvers"
|
|
968
|
-
5. Custom config: "Configure GraphQL Yoga plugins in config.ts"
|
|
969
|
-
|
|
970
|
-
Now implement this setup step-by-step.
|
|
971
|
-
```
|
|
972
|
-
|
|
973
|
-
</details>
|
|
974
|
-
|
|
975
|
-
<details>
|
|
976
|
-
<summary>⚡ <strong>Nitro Project</strong></summary>
|
|
977
|
-
|
|
978
|
-
```
|
|
979
|
-
Set up nitro-graphql in this Nitro project following these exact specifications:
|
|
980
|
-
|
|
981
|
-
INSTALLATION:
|
|
982
|
-
1. Run: pnpm add nitro-graphql graphql-yoga graphql
|
|
983
|
-
|
|
984
|
-
CONFIGURATION (nitro.config.ts):
|
|
985
|
-
import { defineNitroConfig } from 'nitro/config'
|
|
986
|
-
|
|
987
|
-
export default defineNitroConfig({
|
|
988
|
-
modules: ['nitro-graphql'],
|
|
989
|
-
graphql: {
|
|
990
|
-
framework: 'graphql-yoga',
|
|
991
|
-
},
|
|
992
|
-
})
|
|
993
|
-
|
|
994
|
-
SCHEMA (server/graphql/schema.graphql):
|
|
995
|
-
type Product {
|
|
996
|
-
id: ID!
|
|
997
|
-
name: String!
|
|
998
|
-
price: Float!
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
input CreateProductInput {
|
|
1002
|
-
name: String!
|
|
1003
|
-
price: Float!
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
type Query {
|
|
1007
|
-
products: [Product!]!
|
|
1008
|
-
product(id: ID!): Product
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
type Mutation {
|
|
1012
|
-
createProduct(input: CreateProductInput!): Product!
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
RESOLVERS (server/graphql/products.resolver.ts):
|
|
1016
|
-
// Must explicitly import (NOT auto-imported)
|
|
1017
|
-
import { defineQuery, defineMutation } from 'nitro-graphql/define'
|
|
1018
|
-
|
|
1019
|
-
// Use NAMED EXPORTS only
|
|
1020
|
-
export const productQueries = defineQuery({
|
|
1021
|
-
products: async (_, __, context) => {
|
|
1022
|
-
// Access H3 event context
|
|
1023
|
-
const products = await context.storage?.getItem('products') || []
|
|
1024
|
-
return products
|
|
1025
|
-
},
|
|
1026
|
-
product: async (_, { id }, context) => {
|
|
1027
|
-
const products = await context.storage?.getItem('products') || []
|
|
1028
|
-
return products.find(p => p.id === id)
|
|
1029
|
-
}
|
|
1030
|
-
})
|
|
1031
|
-
|
|
1032
|
-
export const productMutations = defineMutation({
|
|
1033
|
-
createProduct: async (_, { input }, context) => {
|
|
1034
|
-
const products = await context.storage?.getItem('products') || []
|
|
1035
|
-
const product = {
|
|
1036
|
-
id: Date.now().toString(),
|
|
1037
|
-
...input
|
|
1038
|
-
}
|
|
1039
|
-
products.push(product)
|
|
1040
|
-
await context.storage?.setItem('products', products)
|
|
1041
|
-
return product
|
|
1042
|
-
}
|
|
1043
|
-
})
|
|
1044
|
-
|
|
1045
|
-
KEY RULES:
|
|
1046
|
-
- Files: *.graphql for schemas, *.resolver.ts for resolvers
|
|
1047
|
-
- MUST use named exports (not default export)
|
|
1048
|
-
- MUST explicitly import: import { defineQuery } from 'nitro-graphql/define'
|
|
1049
|
-
- Context is the third parameter (access H3 event context)
|
|
1050
|
-
- Endpoint: http://localhost:3000/api/graphql
|
|
1051
|
-
|
|
1052
|
-
Now implement this setup.
|
|
1053
|
-
```
|
|
1054
|
-
|
|
1055
|
-
</details>
|
|
1056
|
-
|
|
1057
|
-
<details>
|
|
1058
|
-
<summary>🎮 <strong>Apollo Server Setup</strong></summary>
|
|
1059
|
-
|
|
1060
|
-
```
|
|
1061
|
-
Set up nitro-graphql with Apollo Server following these exact specifications:
|
|
1062
|
-
|
|
1063
|
-
INSTALLATION:
|
|
1064
|
-
1. Run: pnpm add nitro-graphql @apollo/server @apollo/utils.withrequired graphql
|
|
1065
|
-
|
|
1066
|
-
CONFIGURATION (nitro.config.ts):
|
|
1067
|
-
import { defineNitroConfig } from 'nitro/config'
|
|
1068
|
-
|
|
1069
|
-
export default defineNitroConfig({
|
|
1070
|
-
modules: ['nitro-graphql'],
|
|
1071
|
-
graphql: {
|
|
1072
|
-
framework: 'apollo-server',
|
|
1073
|
-
},
|
|
1074
|
-
})
|
|
1075
|
-
|
|
1076
|
-
SCHEMA (server/graphql/schema.graphql):
|
|
1077
|
-
type Book {
|
|
1078
|
-
id: ID!
|
|
1079
|
-
title: String!
|
|
1080
|
-
author: String!
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
type Query {
|
|
1084
|
-
books: [Book!]!
|
|
1085
|
-
book(id: ID!): Book
|
|
1086
|
-
}
|
|
1087
|
-
|
|
1088
|
-
type Mutation {
|
|
1089
|
-
addBook(title: String!, author: String!): Book!
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
RESOLVERS (server/graphql/books.resolver.ts):
|
|
1093
|
-
// Must explicitly import (NOT auto-imported)
|
|
1094
|
-
import { defineResolver } from 'nitro-graphql/define'
|
|
1095
|
-
|
|
1096
|
-
// IMPORTANT: Use NAMED EXPORTS
|
|
1097
|
-
export const bookResolver = defineResolver({
|
|
1098
|
-
Query: {
|
|
1099
|
-
books: async () => {
|
|
1100
|
-
return [
|
|
1101
|
-
{ id: '1', title: '1984', author: 'George Orwell' }
|
|
1102
|
-
]
|
|
1103
|
-
},
|
|
1104
|
-
book: async (_, { id }) => {
|
|
1105
|
-
return { id, title: '1984', author: 'George Orwell' }
|
|
1106
|
-
}
|
|
1107
|
-
},
|
|
1108
|
-
Mutation: {
|
|
1109
|
-
addBook: async (_, { title, author }) => {
|
|
1110
|
-
return {
|
|
1111
|
-
id: Date.now().toString(),
|
|
1112
|
-
title,
|
|
1113
|
-
author
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
})
|
|
1118
|
-
|
|
1119
|
-
KEY RULES:
|
|
1120
|
-
- framework: 'apollo-server' in config
|
|
1121
|
-
- MUST explicitly import: import { defineResolver } from 'nitro-graphql/define'
|
|
1122
|
-
- defineResolver for complete resolver maps
|
|
1123
|
-
- Named exports required (export const name = ...)
|
|
1124
|
-
- Apollo Sandbox: http://localhost:3000/api/graphql
|
|
1125
|
-
- Supports Apollo Federation with federation: { enabled: true }
|
|
1126
|
-
|
|
1127
|
-
Now implement this setup.
|
|
1128
|
-
```
|
|
1129
|
-
|
|
1130
|
-
</details>
|
|
1131
|
-
|
|
1132
|
-
<details>
|
|
1133
|
-
<summary>🔄 <strong>Add Feature to Existing Setup</strong></summary>
|
|
1134
|
-
|
|
1135
|
-
```
|
|
1136
|
-
Add a complete blog posts feature to my nitro-graphql API following these specifications:
|
|
1137
|
-
|
|
1138
|
-
SCHEMA (server/graphql/posts/post.graphql):
|
|
1139
|
-
type Post {
|
|
1140
|
-
id: ID!
|
|
1141
|
-
title: String!
|
|
1142
|
-
content: String!
|
|
1143
|
-
authorId: ID!
|
|
1144
|
-
createdAt: String!
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
input CreatePostInput {
|
|
1148
|
-
title: String!
|
|
1149
|
-
content: String!
|
|
1150
|
-
authorId: ID!
|
|
1151
|
-
}
|
|
1152
|
-
|
|
1153
|
-
input UpdatePostInput {
|
|
1154
|
-
title: String
|
|
1155
|
-
content: String
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
extend type Query {
|
|
1159
|
-
posts(limit: Int = 10, offset: Int = 0): [Post!]!
|
|
1160
|
-
post(id: ID!): Post
|
|
1161
|
-
}
|
|
1162
|
-
|
|
1163
|
-
extend type Mutation {
|
|
1164
|
-
createPost(input: CreatePostInput!): Post!
|
|
1165
|
-
updatePost(id: ID!, input: UpdatePostInput!): Post
|
|
1166
|
-
deletePost(id: ID!): Boolean!
|
|
1167
|
-
}
|
|
1168
|
-
|
|
1169
|
-
RESOLVERS (server/graphql/posts/post.resolver.ts):
|
|
1170
|
-
// Must explicitly import (NOT auto-imported)
|
|
1171
|
-
import { defineQuery, defineMutation } from 'nitro-graphql/define'
|
|
1172
|
-
|
|
1173
|
-
// Use NAMED EXPORTS
|
|
1174
|
-
export const postQueries = defineQuery({
|
|
1175
|
-
posts: async (_, { limit, offset }, context) => {
|
|
1176
|
-
const posts = await context.storage?.getItem('posts') || []
|
|
1177
|
-
return posts.slice(offset, offset + limit)
|
|
1178
|
-
},
|
|
1179
|
-
post: async (_, { id }, context) => {
|
|
1180
|
-
const posts = await context.storage?.getItem('posts') || []
|
|
1181
|
-
return posts.find(p => p.id === id) || null
|
|
1182
|
-
}
|
|
1183
|
-
})
|
|
1184
|
-
|
|
1185
|
-
export const postMutations = defineMutation({
|
|
1186
|
-
createPost: async (_, { input }, context) => {
|
|
1187
|
-
const posts = await context.storage?.getItem('posts') || []
|
|
1188
|
-
const post = {
|
|
1189
|
-
id: Date.now().toString(),
|
|
1190
|
-
...input,
|
|
1191
|
-
createdAt: new Date().toISOString()
|
|
1192
|
-
}
|
|
1193
|
-
posts.push(post)
|
|
1194
|
-
await context.storage?.setItem('posts', posts)
|
|
1195
|
-
return post
|
|
1196
|
-
},
|
|
1197
|
-
updatePost: async (_, { id, input }, context) => {
|
|
1198
|
-
const posts = await context.storage?.getItem('posts') || []
|
|
1199
|
-
const index = posts.findIndex(p => p.id === id)
|
|
1200
|
-
if (index === -1) return null
|
|
1201
|
-
posts[index] = { ...posts[index], ...input }
|
|
1202
|
-
await context.storage?.setItem('posts', posts)
|
|
1203
|
-
return posts[index]
|
|
1204
|
-
},
|
|
1205
|
-
deletePost: async (_, { id }, context) => {
|
|
1206
|
-
const posts = await context.storage?.getItem('posts') || []
|
|
1207
|
-
const filtered = posts.filter(p => p.id !== id)
|
|
1208
|
-
await context.storage?.setItem('posts', filtered)
|
|
1209
|
-
return filtered.length < posts.length
|
|
1210
|
-
}
|
|
1211
|
-
})
|
|
1212
|
-
|
|
1213
|
-
TYPE USAGE:
|
|
1214
|
-
After dev server restarts, types are auto-generated in:
|
|
1215
|
-
- .nitro/types/nitro-graphql-server.d.ts (server types)
|
|
1216
|
-
- .nuxt/types/nitro-graphql-server.d.ts (for Nuxt)
|
|
1217
|
-
|
|
1218
|
-
Import types:
|
|
1219
|
-
import type { Post, CreatePostInput } from '#graphql/server'
|
|
1220
|
-
|
|
1221
|
-
KEY RULES:
|
|
1222
|
-
- MUST explicitly import: import { defineQuery, defineMutation } from 'nitro-graphql/define'
|
|
1223
|
-
- Use "extend type" to add to existing Query/Mutation
|
|
1224
|
-
- Named exports required
|
|
1225
|
-
- Context has H3 event properties
|
|
1226
|
-
- Types auto-generate on file changes
|
|
1227
|
-
|
|
1228
|
-
Now implement this feature.
|
|
1229
|
-
```
|
|
1230
|
-
|
|
1231
|
-
</details>
|
|
1232
|
-
|
|
1233
800
|
### Working with Your GraphQL API
|
|
1234
801
|
|
|
1235
802
|
Once set up, you can ask Claude Code for help with:
|