nitro-graphql 2.0.0-beta.21 → 2.0.0-beta.22

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 CHANGED
@@ -649,7 +649,7 @@ server/
649
649
  - ✅ Verify exports are named exports
650
650
 
651
651
  **Import errors**
652
- - ✅ Use correct path: `nitro-graphql/utils/define`
652
+ - ✅ Use correct path: `nitro-graphql/define`
653
653
  - ✅ Use named exports in resolvers
654
654
 
655
655
  **Vite: "Parse failure: Expected ';', '}' or <eof>" on GraphQL files**
@@ -0,0 +1,294 @@
1
+ import { StandardSchemaV1 } from "./types/standard-schema.mjs";
2
+ import "./types/index.mjs";
3
+ import { DefineDirectiveConfig, DefineServerConfig, DirectiveDefinition, Flatten } from "./types/define.mjs";
4
+ import { NPMConfig, Resolvers, ResolversTypes } from "#graphql/server";
5
+
6
+ //#region src/define.d.ts
7
+
8
+ /**
9
+ * Define schema extensions programmatically for GraphQL types.
10
+ *
11
+ * This function allows you to provide runtime schema definitions that extend
12
+ * your GraphQL types with additional validation or transformation logic using
13
+ * StandardSchemaV1 compliant validators (e.g., Valibot, Zod, ArkType).
14
+ *
15
+ * @template T - A partial record mapping GraphQL type names to their schema definitions
16
+ * @param config - An object mapping GraphQL type names to StandardSchemaV1 schema instances
17
+ * @returns The flattened schema configuration with proper type inference
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * import * as v from 'valibot'
22
+ *
23
+ * export const schemas = defineSchema({
24
+ * CreateUserInput: v.object({
25
+ * email: v.pipe(v.string(), v.email()),
26
+ * age: v.pipe(v.number(), v.minValue(18))
27
+ * })
28
+ * })
29
+ * ```
30
+ */
31
+ declare function defineSchema<T extends Partial<Record<keyof ResolversTypes, StandardSchemaV1>>>(config: T): Flatten<T>;
32
+ /**
33
+ * Define a complete GraphQL resolver object with full type safety.
34
+ *
35
+ * Use this function when you need to define multiple resolver types (Query, Mutation,
36
+ * Subscription, and custom types) in a single resolver file. This provides type inference
37
+ * and auto-completion for all resolver fields.
38
+ *
39
+ * @param resolvers - A complete resolver object containing Query, Mutation, Subscription, or custom type resolvers
40
+ * @returns The same resolver object with preserved type information
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * export const userResolvers = defineResolver({
45
+ * Query: {
46
+ * user: async (_, { id }, context) => {
47
+ * return await context.storage.getItem(`user:${id}`)
48
+ * }
49
+ * },
50
+ * Mutation: {
51
+ * createUser: async (_, { input }, context) => {
52
+ * const user = { id: generateId(), ...input }
53
+ * await context.storage.setItem(`user:${user.id}`, user)
54
+ * return user
55
+ * }
56
+ * },
57
+ * User: {
58
+ * fullName: (parent) => `${parent.firstName} ${parent.lastName}`
59
+ * }
60
+ * })
61
+ * ```
62
+ */
63
+ declare function defineResolver(resolvers: Resolvers): Resolvers;
64
+ /**
65
+ * Define GraphQL Query resolvers with full type safety.
66
+ *
67
+ * Use this helper when you only need to define Query resolvers. It automatically
68
+ * wraps your resolvers in the correct structure and provides type inference for
69
+ * all query fields defined in your GraphQL schema.
70
+ *
71
+ * @param resolvers - An object containing Query field resolvers (defaults to empty object)
72
+ * @returns A Resolvers object with the Query field populated
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * export const userQueries = defineQuery({
77
+ * user: async (_, { id }, context) => {
78
+ * return await fetchUser(id)
79
+ * },
80
+ * users: async (_, __, context) => {
81
+ * return await fetchAllUsers()
82
+ * }
83
+ * })
84
+ * ```
85
+ *
86
+ * @remarks
87
+ * Named exports are required. Do not use default exports with this function.
88
+ */
89
+ declare function defineQuery(resolvers?: Resolvers['Query']): Resolvers;
90
+ /**
91
+ * Define GraphQL Mutation resolvers with full type safety.
92
+ *
93
+ * Use this helper when you only need to define Mutation resolvers. It automatically
94
+ * wraps your resolvers in the correct structure and provides type inference for
95
+ * all mutation fields defined in your GraphQL schema.
96
+ *
97
+ * @param resolvers - An object containing Mutation field resolvers (defaults to empty object)
98
+ * @returns A Resolvers object with the Mutation field populated
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * export const userMutations = defineMutation({
103
+ * createUser: async (_, { input }, context) => {
104
+ * const user = await context.db.users.create(input)
105
+ * return user
106
+ * },
107
+ * updateUser: async (_, { id, input }, context) => {
108
+ * return await context.db.users.update(id, input)
109
+ * },
110
+ * deleteUser: async (_, { id }, context) => {
111
+ * return await context.db.users.delete(id)
112
+ * }
113
+ * })
114
+ * ```
115
+ *
116
+ * @remarks
117
+ * Named exports are required. Do not use default exports with this function.
118
+ */
119
+ declare function defineMutation(resolvers?: Resolvers['Mutation']): Resolvers;
120
+ /**
121
+ * Define GraphQL Subscription resolvers with full type safety.
122
+ *
123
+ * Use this helper when you need to define real-time Subscription resolvers. It automatically
124
+ * wraps your resolvers in the correct structure and provides type inference for
125
+ * all subscription fields defined in your GraphQL schema.
126
+ *
127
+ * @param resolvers - An object containing Subscription field resolvers (defaults to empty object)
128
+ * @returns A Resolvers object with the Subscription field populated
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * import { Repeater } from '@repeaterjs/repeater'
133
+ *
134
+ * export const messageSubscriptions = defineSubscription({
135
+ * messageAdded: {
136
+ * subscribe: async function* (_, { channelId }, context) {
137
+ * const pubsub = context.pubsub
138
+ * yield* pubsub.subscribe(`channel:${channelId}`)
139
+ * }
140
+ * },
141
+ * userStatusChanged: {
142
+ * subscribe: async (_, __, context) => {
143
+ * return new Repeater(async (push, stop) => {
144
+ * // Real-time subscription logic
145
+ * })
146
+ * }
147
+ * }
148
+ * })
149
+ * ```
150
+ *
151
+ * @remarks
152
+ * Named exports are required. Subscriptions require async iterators or generators.
153
+ */
154
+ declare function defineSubscription(resolvers?: Resolvers['Subscription']): Resolvers;
155
+ /**
156
+ * Define custom GraphQL type resolvers with full type safety.
157
+ *
158
+ * Use this function to define field resolvers for custom GraphQL types (non-scalar types).
159
+ * This is useful for computed fields, field-level transformations, or resolving relationships
160
+ * between types. The function provides type inference for all custom types in your schema.
161
+ *
162
+ * @param resolvers - An object containing custom type field resolvers
163
+ * @returns The same resolver object with preserved type information
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * // For a User type with computed fields
168
+ * export const userTypeResolvers = defineType({
169
+ * User: {
170
+ * fullName: (parent) => `${parent.firstName} ${parent.lastName}`,
171
+ * age: (parent) => {
172
+ * const today = new Date()
173
+ * return today.getFullYear() - parent.birthYear
174
+ * },
175
+ * posts: async (parent, _, context) => {
176
+ * return await context.db.posts.findByUserId(parent.id)
177
+ * }
178
+ * }
179
+ * })
180
+ * ```
181
+ *
182
+ * @remarks
183
+ * This is functionally equivalent to defineResolver but semantically indicates
184
+ * you're defining type-specific field resolvers rather than root-level operations.
185
+ */
186
+ declare function defineType(resolvers: Resolvers): Resolvers;
187
+ /**
188
+ * Define GraphQL server configuration with full type safety.
189
+ *
190
+ * Use this function to configure your GraphQL server settings including schema,
191
+ * context, plugins, and framework-specific options. Supports both GraphQL Yoga
192
+ * and Apollo Server configurations with proper type inference.
193
+ *
194
+ * @template T - The NPMConfig type (GraphQL Yoga or Apollo Server config)
195
+ * @param config - Partial server configuration object
196
+ * @returns The same configuration object with preserved type information
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * // GraphQL Yoga configuration
201
+ * export default defineGraphQLConfig({
202
+ * schema: {
203
+ * // Schema directives, custom scalars, etc.
204
+ * },
205
+ * context: async (event) => {
206
+ * return {
207
+ * user: await getUserFromEvent(event),
208
+ * db: getDatabase()
209
+ * }
210
+ * },
211
+ * yoga: {
212
+ * graphiql: true,
213
+ * maskedErrors: false
214
+ * }
215
+ * })
216
+ * ```
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * // Apollo Server configuration
221
+ * export default defineGraphQLConfig({
222
+ * apollo: {
223
+ * introspection: true,
224
+ * csrfPrevention: true,
225
+ * plugins: [ApolloServerPluginLandingPageGraphQLPlayground()]
226
+ * }
227
+ * })
228
+ * ```
229
+ */
230
+ declare function defineGraphQLConfig<T extends NPMConfig = NPMConfig>(config: Partial<DefineServerConfig<T>>): Partial<DefineServerConfig<T>>;
231
+ /**
232
+ * Define a custom GraphQL directive with full type safety.
233
+ *
234
+ * This function creates a GraphQL directive definition that can be used to add
235
+ * custom behavior to your GraphQL schema. It automatically generates the directive's
236
+ * schema definition string and provides runtime transformation capabilities.
237
+ *
238
+ * @param config - The directive configuration object
239
+ * @param config.name - The name of the directive (without the @ symbol)
240
+ * @param config.locations - Array of GraphQL directive locations where this directive can be applied
241
+ * @param config.args - Optional arguments the directive accepts with their types and default values
242
+ * @param config.transformer - Optional function to transform the schema when this directive is applied
243
+ * @returns A DirectiveDefinition object with the schema definition and runtime behavior
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils'
248
+ *
249
+ * export const upperDirective = defineDirective({
250
+ * name: 'upper',
251
+ * locations: ['FIELD_DEFINITION'],
252
+ * transformer: (schema) => {
253
+ * return mapSchema(schema, {
254
+ * [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
255
+ * const directive = getDirective(schema, fieldConfig, 'upper')?.[0]
256
+ * if (directive) {
257
+ * const { resolve = defaultFieldResolver } = fieldConfig
258
+ * fieldConfig.resolve = async (source, args, context, info) => {
259
+ * const result = await resolve(source, args, context, info)
260
+ * return typeof result === 'string' ? result.toUpperCase() : result
261
+ * }
262
+ * }
263
+ * return fieldConfig
264
+ * }
265
+ * })
266
+ * }
267
+ * })
268
+ * ```
269
+ *
270
+ * @example
271
+ * ```typescript
272
+ * // Directive with arguments
273
+ * export const authDirective = defineDirective({
274
+ * name: 'auth',
275
+ * locations: ['FIELD_DEFINITION', 'OBJECT'],
276
+ * args: {
277
+ * requires: {
278
+ * type: 'Role!',
279
+ * defaultValue: 'USER'
280
+ * }
281
+ * },
282
+ * transformer: (schema) => {
283
+ * // Implementation for auth checking
284
+ * }
285
+ * })
286
+ * ```
287
+ *
288
+ * @remarks
289
+ * The generated directive schema definition is stored as a non-enumerable `__schema`
290
+ * property on the returned object and is automatically included in your GraphQL schema.
291
+ */
292
+ declare function defineDirective(config: DefineDirectiveConfig): DirectiveDefinition;
293
+ //#endregion
294
+ export { defineDirective, defineGraphQLConfig, defineMutation, defineQuery, defineResolver, defineSchema, defineSubscription, defineType };
@@ -0,0 +1,321 @@
1
+ //#region src/define.ts
2
+ /**
3
+ * Define schema extensions programmatically for GraphQL types.
4
+ *
5
+ * This function allows you to provide runtime schema definitions that extend
6
+ * your GraphQL types with additional validation or transformation logic using
7
+ * StandardSchemaV1 compliant validators (e.g., Valibot, Zod, ArkType).
8
+ *
9
+ * @template T - A partial record mapping GraphQL type names to their schema definitions
10
+ * @param config - An object mapping GraphQL type names to StandardSchemaV1 schema instances
11
+ * @returns The flattened schema configuration with proper type inference
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import * as v from 'valibot'
16
+ *
17
+ * export const schemas = defineSchema({
18
+ * CreateUserInput: v.object({
19
+ * email: v.pipe(v.string(), v.email()),
20
+ * age: v.pipe(v.number(), v.minValue(18))
21
+ * })
22
+ * })
23
+ * ```
24
+ */
25
+ function defineSchema(config) {
26
+ return config;
27
+ }
28
+ /**
29
+ * Define a complete GraphQL resolver object with full type safety.
30
+ *
31
+ * Use this function when you need to define multiple resolver types (Query, Mutation,
32
+ * Subscription, and custom types) in a single resolver file. This provides type inference
33
+ * and auto-completion for all resolver fields.
34
+ *
35
+ * @param resolvers - A complete resolver object containing Query, Mutation, Subscription, or custom type resolvers
36
+ * @returns The same resolver object with preserved type information
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * export const userResolvers = defineResolver({
41
+ * Query: {
42
+ * user: async (_, { id }, context) => {
43
+ * return await context.storage.getItem(`user:${id}`)
44
+ * }
45
+ * },
46
+ * Mutation: {
47
+ * createUser: async (_, { input }, context) => {
48
+ * const user = { id: generateId(), ...input }
49
+ * await context.storage.setItem(`user:${user.id}`, user)
50
+ * return user
51
+ * }
52
+ * },
53
+ * User: {
54
+ * fullName: (parent) => `${parent.firstName} ${parent.lastName}`
55
+ * }
56
+ * })
57
+ * ```
58
+ */
59
+ function defineResolver(resolvers) {
60
+ return resolvers;
61
+ }
62
+ /**
63
+ * Define GraphQL Query resolvers with full type safety.
64
+ *
65
+ * Use this helper when you only need to define Query resolvers. It automatically
66
+ * wraps your resolvers in the correct structure and provides type inference for
67
+ * all query fields defined in your GraphQL schema.
68
+ *
69
+ * @param resolvers - An object containing Query field resolvers (defaults to empty object)
70
+ * @returns A Resolvers object with the Query field populated
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * export const userQueries = defineQuery({
75
+ * user: async (_, { id }, context) => {
76
+ * return await fetchUser(id)
77
+ * },
78
+ * users: async (_, __, context) => {
79
+ * return await fetchAllUsers()
80
+ * }
81
+ * })
82
+ * ```
83
+ *
84
+ * @remarks
85
+ * Named exports are required. Do not use default exports with this function.
86
+ */
87
+ function defineQuery(resolvers = {}) {
88
+ return { Query: { ...resolvers } };
89
+ }
90
+ /**
91
+ * Define GraphQL Mutation resolvers with full type safety.
92
+ *
93
+ * Use this helper when you only need to define Mutation resolvers. It automatically
94
+ * wraps your resolvers in the correct structure and provides type inference for
95
+ * all mutation fields defined in your GraphQL schema.
96
+ *
97
+ * @param resolvers - An object containing Mutation field resolvers (defaults to empty object)
98
+ * @returns A Resolvers object with the Mutation field populated
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * export const userMutations = defineMutation({
103
+ * createUser: async (_, { input }, context) => {
104
+ * const user = await context.db.users.create(input)
105
+ * return user
106
+ * },
107
+ * updateUser: async (_, { id, input }, context) => {
108
+ * return await context.db.users.update(id, input)
109
+ * },
110
+ * deleteUser: async (_, { id }, context) => {
111
+ * return await context.db.users.delete(id)
112
+ * }
113
+ * })
114
+ * ```
115
+ *
116
+ * @remarks
117
+ * Named exports are required. Do not use default exports with this function.
118
+ */
119
+ function defineMutation(resolvers = {}) {
120
+ return { Mutation: { ...resolvers } };
121
+ }
122
+ /**
123
+ * Define GraphQL Subscription resolvers with full type safety.
124
+ *
125
+ * Use this helper when you need to define real-time Subscription resolvers. It automatically
126
+ * wraps your resolvers in the correct structure and provides type inference for
127
+ * all subscription fields defined in your GraphQL schema.
128
+ *
129
+ * @param resolvers - An object containing Subscription field resolvers (defaults to empty object)
130
+ * @returns A Resolvers object with the Subscription field populated
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * import { Repeater } from '@repeaterjs/repeater'
135
+ *
136
+ * export const messageSubscriptions = defineSubscription({
137
+ * messageAdded: {
138
+ * subscribe: async function* (_, { channelId }, context) {
139
+ * const pubsub = context.pubsub
140
+ * yield* pubsub.subscribe(`channel:${channelId}`)
141
+ * }
142
+ * },
143
+ * userStatusChanged: {
144
+ * subscribe: async (_, __, context) => {
145
+ * return new Repeater(async (push, stop) => {
146
+ * // Real-time subscription logic
147
+ * })
148
+ * }
149
+ * }
150
+ * })
151
+ * ```
152
+ *
153
+ * @remarks
154
+ * Named exports are required. Subscriptions require async iterators or generators.
155
+ */
156
+ function defineSubscription(resolvers = {}) {
157
+ return { Subscription: { ...resolvers } };
158
+ }
159
+ /**
160
+ * Define custom GraphQL type resolvers with full type safety.
161
+ *
162
+ * Use this function to define field resolvers for custom GraphQL types (non-scalar types).
163
+ * This is useful for computed fields, field-level transformations, or resolving relationships
164
+ * between types. The function provides type inference for all custom types in your schema.
165
+ *
166
+ * @param resolvers - An object containing custom type field resolvers
167
+ * @returns The same resolver object with preserved type information
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * // For a User type with computed fields
172
+ * export const userTypeResolvers = defineType({
173
+ * User: {
174
+ * fullName: (parent) => `${parent.firstName} ${parent.lastName}`,
175
+ * age: (parent) => {
176
+ * const today = new Date()
177
+ * return today.getFullYear() - parent.birthYear
178
+ * },
179
+ * posts: async (parent, _, context) => {
180
+ * return await context.db.posts.findByUserId(parent.id)
181
+ * }
182
+ * }
183
+ * })
184
+ * ```
185
+ *
186
+ * @remarks
187
+ * This is functionally equivalent to defineResolver but semantically indicates
188
+ * you're defining type-specific field resolvers rather than root-level operations.
189
+ */
190
+ function defineType(resolvers) {
191
+ return resolvers;
192
+ }
193
+ /**
194
+ * Define GraphQL server configuration with full type safety.
195
+ *
196
+ * Use this function to configure your GraphQL server settings including schema,
197
+ * context, plugins, and framework-specific options. Supports both GraphQL Yoga
198
+ * and Apollo Server configurations with proper type inference.
199
+ *
200
+ * @template T - The NPMConfig type (GraphQL Yoga or Apollo Server config)
201
+ * @param config - Partial server configuration object
202
+ * @returns The same configuration object with preserved type information
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * // GraphQL Yoga configuration
207
+ * export default defineGraphQLConfig({
208
+ * schema: {
209
+ * // Schema directives, custom scalars, etc.
210
+ * },
211
+ * context: async (event) => {
212
+ * return {
213
+ * user: await getUserFromEvent(event),
214
+ * db: getDatabase()
215
+ * }
216
+ * },
217
+ * yoga: {
218
+ * graphiql: true,
219
+ * maskedErrors: false
220
+ * }
221
+ * })
222
+ * ```
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * // Apollo Server configuration
227
+ * export default defineGraphQLConfig({
228
+ * apollo: {
229
+ * introspection: true,
230
+ * csrfPrevention: true,
231
+ * plugins: [ApolloServerPluginLandingPageGraphQLPlayground()]
232
+ * }
233
+ * })
234
+ * ```
235
+ */
236
+ function defineGraphQLConfig(config) {
237
+ return config;
238
+ }
239
+ /**
240
+ * Define a custom GraphQL directive with full type safety.
241
+ *
242
+ * This function creates a GraphQL directive definition that can be used to add
243
+ * custom behavior to your GraphQL schema. It automatically generates the directive's
244
+ * schema definition string and provides runtime transformation capabilities.
245
+ *
246
+ * @param config - The directive configuration object
247
+ * @param config.name - The name of the directive (without the @ symbol)
248
+ * @param config.locations - Array of GraphQL directive locations where this directive can be applied
249
+ * @param config.args - Optional arguments the directive accepts with their types and default values
250
+ * @param config.transformer - Optional function to transform the schema when this directive is applied
251
+ * @returns A DirectiveDefinition object with the schema definition and runtime behavior
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils'
256
+ *
257
+ * export const upperDirective = defineDirective({
258
+ * name: 'upper',
259
+ * locations: ['FIELD_DEFINITION'],
260
+ * transformer: (schema) => {
261
+ * return mapSchema(schema, {
262
+ * [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
263
+ * const directive = getDirective(schema, fieldConfig, 'upper')?.[0]
264
+ * if (directive) {
265
+ * const { resolve = defaultFieldResolver } = fieldConfig
266
+ * fieldConfig.resolve = async (source, args, context, info) => {
267
+ * const result = await resolve(source, args, context, info)
268
+ * return typeof result === 'string' ? result.toUpperCase() : result
269
+ * }
270
+ * }
271
+ * return fieldConfig
272
+ * }
273
+ * })
274
+ * }
275
+ * })
276
+ * ```
277
+ *
278
+ * @example
279
+ * ```typescript
280
+ * // Directive with arguments
281
+ * export const authDirective = defineDirective({
282
+ * name: 'auth',
283
+ * locations: ['FIELD_DEFINITION', 'OBJECT'],
284
+ * args: {
285
+ * requires: {
286
+ * type: 'Role!',
287
+ * defaultValue: 'USER'
288
+ * }
289
+ * },
290
+ * transformer: (schema) => {
291
+ * // Implementation for auth checking
292
+ * }
293
+ * })
294
+ * ```
295
+ *
296
+ * @remarks
297
+ * The generated directive schema definition is stored as a non-enumerable `__schema`
298
+ * property on the returned object and is automatically included in your GraphQL schema.
299
+ */
300
+ function defineDirective(config) {
301
+ const args = config.args ? Object.entries(config.args).map(([name, arg]) => {
302
+ const defaultValue = arg.defaultValue !== void 0 ? ` = ${JSON.stringify(arg.defaultValue)}` : "";
303
+ return `${name}: ${arg.type}${defaultValue}`;
304
+ }).join(", ") : "";
305
+ const argsString = args ? `(${args})` : "";
306
+ const locations = config.locations.join(" | ");
307
+ const schemaDefinition = `directive @${config.name}${argsString} on ${locations}`;
308
+ Object.defineProperty(config, "__schema", {
309
+ value: schemaDefinition,
310
+ enumerable: false,
311
+ configurable: false,
312
+ writable: false
313
+ });
314
+ return {
315
+ ...config,
316
+ locations: [...config.locations]
317
+ };
318
+ }
319
+
320
+ //#endregion
321
+ export { defineDirective, defineGraphQLConfig, defineMutation, defineQuery, defineResolver, defineSchema, defineSubscription, defineType };
@@ -1,6 +1,6 @@
1
- import * as h33 from "h3";
1
+ import * as h31 from "h3";
2
2
 
3
3
  //#region src/routes/apollo-server.d.ts
4
- declare const _default: h33.EventHandlerWithFetch<h33.EventHandlerRequest, Promise<any>>;
4
+ declare const _default: h31.EventHandlerWithFetch<h31.EventHandlerRequest, Promise<any>>;
5
5
  //#endregion
6
6
  export { _default as default };
@@ -1,6 +1,6 @@
1
- import * as h31 from "h3";
1
+ import * as h33 from "h3";
2
2
 
3
3
  //#region src/routes/graphql-yoga.d.ts
4
- declare const _default: h31.EventHandlerWithFetch<h31.EventHandlerRequest, Promise<Response>>;
4
+ declare const _default: h33.EventHandlerWithFetch<h33.EventHandlerRequest, Promise<Response>>;
5
5
  //#endregion
6
6
  export { _default as default };
package/dist/setup.mjs CHANGED
@@ -199,7 +199,7 @@ async function setupNitroGraphQL(nitro) {
199
199
  if (nitro.options.imports) {
200
200
  nitro.options.imports.presets ??= [];
201
201
  nitro.options.imports.presets.push({
202
- from: fileURLToPath(new URL("utils/define", import.meta.url)),
202
+ from: fileURLToPath(new URL("define", import.meta.url)),
203
203
  imports: [
204
204
  "defineResolver",
205
205
  "defineMutation",
@@ -346,7 +346,7 @@ export default <IGraphQLConfig> {
346
346
  if (serverConfigPath) writeFileIfNotExists(serverConfigPath, `// Example GraphQL config file please change it to your needs
347
347
  // import * as tables from '../drizzle/schema/index'
348
348
  // import { useDatabase } from '../utils/useDb'
349
- import { defineGraphQLConfig } from 'nitro-graphql/utils/define'
349
+ import { defineGraphQLConfig } from 'nitro-graphql/define'
350
350
 
351
351
  export default defineGraphQLConfig({
352
352
  // graphql-yoga example config
@@ -1,37 +1,14 @@
1
- import { StandardSchemaV1 } from "../types/standard-schema.mjs";
2
- import "../types/index.mjs";
3
1
  import { GraphQLSchema } from "graphql";
4
2
  import { ApolloServerOptions } from "@apollo/server";
5
3
  import { H3Event } from "h3";
6
4
  import { YogaServerOptions } from "graphql-yoga";
7
- import { NPMConfig, Resolvers, ResolversTypes } from "#graphql/server";
5
+ import { NPMConfig } from "#graphql/server";
8
6
 
9
- //#region src/utils/define.d.ts
7
+ //#region src/types/define.d.ts
10
8
  type Flatten<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
11
- declare function defineSchema<T extends Partial<Record<keyof ResolversTypes, StandardSchemaV1>>>(config: T): Flatten<T>;
12
- declare function defineResolver(resolvers: Resolvers): Resolvers;
13
- type ResolverQuery = Resolvers extends {
14
- Query: infer Q;
15
- } ? Q : never;
16
- declare function defineQuery(resolvers?: Resolvers['Query']): Resolvers;
17
- declare function defineMutation(resolvers?: Resolvers['Mutation']): Resolvers;
18
- declare function defineSubscription(resolvers?: Resolvers['Subscription']): Resolvers;
19
- declare function defineType(resolvers: Resolvers): Resolvers;
20
9
  type DefineServerConfig<T extends NPMConfig = NPMConfig> = T['framework'] extends 'graphql-yoga' ? Partial<YogaServerOptions<H3Event, Partial<H3Event>>> : T['framework'] extends 'apollo-server' ? Partial<ApolloServerOptions<H3Event>> : Partial<YogaServerOptions<H3Event, Partial<H3Event>>> | Partial<ApolloServerOptions<H3Event>>;
21
- declare function defineGraphQLConfig<T extends NPMConfig = NPMConfig>(config: Partial<DefineServerConfig<T>>): Partial<DefineServerConfig<T>>;
22
10
  type DirectiveLocationName = 'QUERY' | 'MUTATION' | 'SUBSCRIPTION' | 'FIELD' | 'FRAGMENT_DEFINITION' | 'FRAGMENT_SPREAD' | 'INLINE_FRAGMENT' | 'VARIABLE_DEFINITION' | 'SCHEMA' | 'SCALAR' | 'OBJECT' | 'FIELD_DEFINITION' | 'ARGUMENT_DEFINITION' | 'INTERFACE' | 'UNION' | 'ENUM' | 'ENUM_VALUE' | 'INPUT_OBJECT' | 'INPUT_FIELD_DEFINITION';
23
- type GraphQLScalarType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID' | 'JSON' | 'DateTime';
24
- type GraphQLBaseType = GraphQLScalarType | (string & {});
25
11
  type GraphQLArgumentType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID' | 'JSON' | 'DateTime' | 'String!' | 'Int!' | 'Float!' | 'Boolean!' | 'ID!' | 'JSON!' | 'DateTime!' | '[String]' | '[String!]' | '[String]!' | '[String!]!' | '[Int]' | '[Int!]' | '[Int]!' | '[Int!]!' | '[Float]' | '[Float!]' | '[Float]!' | '[Float!]!' | '[Boolean]' | '[Boolean!]' | '[Boolean]!' | '[Boolean!]!' | '[ID]' | '[ID!]' | '[ID]!' | '[ID!]!' | '[JSON]' | '[JSON!]' | '[JSON]!' | '[JSON!]!' | '[DateTime]' | '[DateTime!]' | '[DateTime]!' | '[DateTime!]!' | (string & {});
26
- interface DirectiveArgument<T extends GraphQLArgumentType = GraphQLArgumentType> {
27
- /**
28
- * GraphQL type for the argument
29
- * @example 'String', 'Int!', '[String!]!', 'DateTime', 'JSON'
30
- */
31
- type: T;
32
- defaultValue?: any;
33
- description?: string;
34
- }
35
12
  interface DirectiveArg {
36
13
  type: GraphQLArgumentType;
37
14
  defaultValue?: any;
@@ -57,17 +34,5 @@ interface DefineDirectiveConfig {
57
34
  isRepeatable?: boolean;
58
35
  transformer?: (schema: GraphQLSchema) => GraphQLSchema;
59
36
  }
60
- /**
61
- * Helper function to create directive arguments with proper type inference
62
- * @example
63
- * args: {
64
- * myArg: arg('String!', { defaultValue: 'hello' })
65
- * }
66
- */
67
- declare function arg<T extends GraphQLArgumentType>(type: T, options?: {
68
- defaultValue?: any;
69
- description?: string;
70
- }): DirectiveArgument<T>;
71
- declare function defineDirective(config: DefineDirectiveConfig): DirectiveDefinition;
72
37
  //#endregion
73
- export { DefineDirectiveConfig, DefineServerConfig, DirectiveArgument, DirectiveDefinition, GraphQLArgumentType, GraphQLBaseType, GraphQLScalarType, ResolverQuery, arg, defineDirective, defineGraphQLConfig, defineMutation, defineQuery, defineResolver, defineSchema, defineSubscription, defineType };
38
+ export { DefineDirectiveConfig, DefineServerConfig, DirectiveDefinition, Flatten };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nitro-graphql",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.21",
4
+ "version": "2.0.0-beta.22",
5
5
  "description": "GraphQL integration for Nitro",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
@@ -44,9 +44,9 @@
44
44
  "types": "./dist/utils/index.d.mts",
45
45
  "import": "./dist/utils/index.mjs"
46
46
  },
47
- "./utils/define": {
48
- "types": "./dist/utils/define.d.mts",
49
- "import": "./dist/utils/define.mjs"
47
+ "./define": {
48
+ "types": "./dist/define.d.mts",
49
+ "import": "./dist/define.mjs"
50
50
  },
51
51
  "./utils/apollo": {
52
52
  "types": "./dist/utils/apollo.d.mts",
@@ -116,17 +116,18 @@
116
116
  "@nuxt/kit": "^4.2.1",
117
117
  "@nuxt/schema": "^4.2.1",
118
118
  "@types/node": "^24.10.0",
119
+ "@vitejs/devtools": "^0.0.0-alpha.16",
119
120
  "bumpp": "^10.3.1",
120
121
  "changelogen": "^0.6.2",
121
122
  "crossws": "0.3.5",
122
123
  "eslint": "^9.39.1",
123
- "graphql": "16.11.0",
124
- "graphql-yoga": "^5.16.2",
124
+ "graphql": "^16.12.0",
125
+ "graphql-yoga": "^5.16.0",
125
126
  "h3": "^2.0.1-rc.5",
126
127
  "nitro": "npm:nitro-nightly@latest",
127
128
  "tsdown": "^0.16.1",
128
129
  "typescript": "^5.9.3",
129
- "vite": "^7.2.2",
130
+ "vite": "npm:rolldown-vite@latest",
130
131
  "vitepress-plugin-llms": "^1.9.1"
131
132
  },
132
133
  "resolutions": {
@@ -1,57 +0,0 @@
1
- //#region src/utils/define.ts
2
- function defineSchema(config) {
3
- return config;
4
- }
5
- function defineResolver(resolvers) {
6
- return resolvers;
7
- }
8
- function defineQuery(resolvers = {}) {
9
- return { Query: { ...resolvers } };
10
- }
11
- function defineMutation(resolvers = {}) {
12
- return { Mutation: { ...resolvers } };
13
- }
14
- function defineSubscription(resolvers = {}) {
15
- return { Subscription: { ...resolvers } };
16
- }
17
- function defineType(resolvers) {
18
- return resolvers;
19
- }
20
- function defineGraphQLConfig(config) {
21
- return config;
22
- }
23
- /**
24
- * Helper function to create directive arguments with proper type inference
25
- * @example
26
- * args: {
27
- * myArg: arg('String!', { defaultValue: 'hello' })
28
- * }
29
- */
30
- function arg(type, options) {
31
- return {
32
- type,
33
- ...options
34
- };
35
- }
36
- function defineDirective(config) {
37
- const args = config.args ? Object.entries(config.args).map(([name, arg$1]) => {
38
- const defaultValue = arg$1.defaultValue !== void 0 ? ` = ${JSON.stringify(arg$1.defaultValue)}` : "";
39
- return `${name}: ${arg$1.type}${defaultValue}`;
40
- }).join(", ") : "";
41
- const argsString = args ? `(${args})` : "";
42
- const locations = config.locations.join(" | ");
43
- const schemaDefinition = `directive @${config.name}${argsString} on ${locations}`;
44
- Object.defineProperty(config, "__schema", {
45
- value: schemaDefinition,
46
- enumerable: false,
47
- configurable: false,
48
- writable: false
49
- });
50
- return {
51
- ...config,
52
- locations: [...config.locations]
53
- };
54
- }
55
-
56
- //#endregion
57
- export { arg, defineDirective, defineGraphQLConfig, defineMutation, defineQuery, defineResolver, defineSchema, defineSubscription, defineType };
package/dist/vite.d.mts DELETED
@@ -1,30 +0,0 @@
1
- import { NitroGraphQLOptions } from "./types/index.mjs";
2
- import { NitroModule } from "nitro/types";
3
- import { Plugin } from "vite";
4
-
5
- //#region src/vite.d.ts
6
-
7
- /**
8
- * Vite plugin to load GraphQL files as strings AND auto-register Nitro module
9
- * This prevents Vite from trying to parse .graphql/.gql files as JavaScript
10
- * and automatically sets up the nitro-graphql module via the nitro: hook
11
- *
12
- * @example
13
- * ```ts
14
- * import { defineConfig } from 'vite'
15
- * import { nitro } from 'nitro/vite'
16
- * import { graphql } from 'nitro-graphql/vite'
17
- *
18
- * export default defineConfig({
19
- * plugins: [
20
- * graphql({ framework: 'graphql-yoga' }), // Auto-registers Nitro module
21
- * nitro()
22
- * ]
23
- * })
24
- * ```
25
- */
26
- declare function graphql(options?: NitroGraphQLOptions): Plugin & {
27
- nitro?: NitroModule;
28
- };
29
- //#endregion
30
- export { graphql };
package/dist/vite.mjs DELETED
@@ -1,49 +0,0 @@
1
- import { setupNitroGraphQL } from "./setup.mjs";
2
- import { readFile } from "node:fs/promises";
3
- import defu from "defu";
4
-
5
- //#region src/vite.ts
6
- /**
7
- * Vite plugin to load GraphQL files as strings AND auto-register Nitro module
8
- * This prevents Vite from trying to parse .graphql/.gql files as JavaScript
9
- * and automatically sets up the nitro-graphql module via the nitro: hook
10
- *
11
- * @example
12
- * ```ts
13
- * import { defineConfig } from 'vite'
14
- * import { nitro } from 'nitro/vite'
15
- * import { graphql } from 'nitro-graphql/vite'
16
- *
17
- * export default defineConfig({
18
- * plugins: [
19
- * graphql({ framework: 'graphql-yoga' }), // Auto-registers Nitro module
20
- * nitro()
21
- * ]
22
- * })
23
- * ```
24
- */
25
- function graphql(options) {
26
- return {
27
- name: "nitro-graphql:vite",
28
- enforce: "pre",
29
- async load(id) {
30
- if (!/\.(?:graphql|gql)$/i.test(id)) return null;
31
- try {
32
- const content = await readFile(id, "utf-8");
33
- return `export default ${JSON.stringify(content)}`;
34
- } catch (error) {
35
- if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return null;
36
- throw error;
37
- }
38
- },
39
- nitro: { async setup(nitro) {
40
- if (options) nitro.options.graphql = defu(nitro.options.graphql || {}, options);
41
- nitro.options.graphql = nitro.options.graphql || {};
42
- nitro.options.graphql._vitePlugin = true;
43
- await setupNitroGraphQL(nitro);
44
- } }
45
- };
46
- }
47
-
48
- //#endregion
49
- export { graphql };