nitro-graphql 2.0.0-beta.3 → 2.0.0-beta.31
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 +477 -20
- package/dist/define.d.mts +296 -0
- package/dist/define.mjs +323 -0
- package/dist/ecosystem/nuxt.mjs +109 -0
- package/dist/index.d.mts +43 -0
- package/dist/index.mjs +63 -0
- package/dist/rollup.d.mts +12 -0
- package/dist/rollup.mjs +282 -0
- package/dist/routes/apollo-server.d.mts +6 -0
- package/dist/routes/{apollo-server.js → apollo-server.mjs} +7 -7
- package/dist/routes/debug.d.mts +61 -0
- package/dist/routes/debug.mjs +445 -0
- package/dist/routes/graphql-yoga.d.mts +6 -0
- package/dist/routes/{graphql-yoga.js → graphql-yoga.mjs} +11 -7
- package/dist/routes/health.d.mts +10 -0
- package/dist/routes/{health.js → health.mjs} +3 -2
- package/dist/setup.d.mts +11 -0
- package/dist/setup.mjs +376 -0
- package/dist/{utils/define.d.ts → types/define.d.mts} +4 -27
- package/dist/types/define.mjs +1 -0
- package/dist/types/index.d.mts +246 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/standard-schema.mjs +1 -0
- package/dist/utils/{apollo.d.ts → apollo.d.mts} +2 -2
- package/dist/utils/apollo.mjs +59 -0
- package/dist/utils/{client-codegen.d.ts → client-codegen.d.mts} +6 -3
- package/dist/utils/{client-codegen.js → client-codegen.mjs} +6 -6
- package/dist/utils/errors.d.mts +73 -0
- package/dist/utils/errors.mjs +89 -0
- package/dist/utils/file-generator.d.mts +37 -0
- package/dist/utils/file-generator.mjs +72 -0
- package/dist/utils/{index.d.ts → index.d.mts} +4 -3
- package/dist/utils/{index.js → index.mjs} +80 -40
- package/dist/utils/path-resolver.d.mts +70 -0
- package/dist/utils/path-resolver.mjs +127 -0
- package/dist/utils/{server-codegen.d.ts → server-codegen.d.mts} +1 -1
- package/dist/utils/{server-codegen.js → server-codegen.mjs} +3 -3
- package/dist/utils/type-generation.d.mts +12 -0
- package/dist/utils/type-generation.mjs +420 -0
- package/dist/virtual/debug-info.d.mts +9 -0
- package/dist/virtual/debug-info.mjs +26 -0
- package/dist/virtual/graphql-config.d.mts +9 -0
- package/dist/virtual/graphql-config.mjs +10 -0
- package/dist/virtual/module-config.d.mts +9 -0
- package/dist/virtual/module-config.mjs +10 -0
- package/dist/virtual/server-directives.d.mts +11 -0
- package/dist/virtual/server-directives.mjs +10 -0
- package/dist/virtual/server-resolvers.d.mts +11 -0
- package/dist/virtual/server-resolvers.mjs +10 -0
- package/dist/virtual/server-schemas.d.mts +11 -0
- package/dist/virtual/server-schemas.mjs +10 -0
- package/package.json +79 -70
- package/dist/ecosystem/nuxt.js +0 -67
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -264
- package/dist/rollup.js +0 -114
- package/dist/routes/apollo-server.d.ts +0 -6
- package/dist/routes/graphql-yoga.d.ts +0 -6
- package/dist/routes/health.d.ts +0 -6
- package/dist/types/index.d.ts +0 -128
- package/dist/utils/apollo.js +0 -61
- package/dist/utils/define.js +0 -57
- package/dist/utils/type-generation.d.ts +0 -7
- package/dist/utils/type-generation.js +0 -287
- /package/dist/ecosystem/{nuxt.d.ts → nuxt.d.mts} +0 -0
- /package/dist/graphql/{index.d.ts → index.d.mts} +0 -0
- /package/dist/graphql/{index.js → index.mjs} +0 -0
- /package/dist/graphql/{server.d.ts → server.d.mts} +0 -0
- /package/dist/graphql/{server.js → server.mjs} +0 -0
- /package/dist/types/{standard-schema.d.ts → standard-schema.d.mts} +0 -0
- /package/dist/utils/{directive-parser.d.ts → directive-parser.d.mts} +0 -0
- /package/dist/utils/{directive-parser.js → directive-parser.mjs} +0 -0
|
@@ -0,0 +1,296 @@
|
|
|
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 (parent, { id }, context) => {
|
|
47
|
+
* return await context.storage.getItem(`user:${id}`)
|
|
48
|
+
* }
|
|
49
|
+
* },
|
|
50
|
+
* Mutation: {
|
|
51
|
+
* createUser: async (parent, { 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, args, context) => `${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 (parent, { id }, context) => {
|
|
78
|
+
* return await fetchUser(id)
|
|
79
|
+
* },
|
|
80
|
+
* users: async (parent, args, 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 (parent, { input }, context) => {
|
|
104
|
+
* const user = await context.db.users.create(input)
|
|
105
|
+
* return user
|
|
106
|
+
* },
|
|
107
|
+
* updateUser: async (parent, { id, input }, context) => {
|
|
108
|
+
* return await context.db.users.update(id, input)
|
|
109
|
+
* },
|
|
110
|
+
* deleteUser: async (parent, { 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* (parent, { channelId }, context) {
|
|
137
|
+
* const pubsub = context.pubsub
|
|
138
|
+
* yield* pubsub.subscribe(`channel:${channelId}`)
|
|
139
|
+
* }
|
|
140
|
+
* },
|
|
141
|
+
* userStatusChanged: {
|
|
142
|
+
* subscribe: async (parent, args, 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 = defineField({
|
|
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 defineField(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
|
+
* import { createDefaultMaskError } from 'nitro-graphql/utils'
|
|
201
|
+
*
|
|
202
|
+
* // GraphQL Yoga configuration
|
|
203
|
+
* export default defineGraphQLConfig({
|
|
204
|
+
* schema: {
|
|
205
|
+
* // Schema directives, custom scalars, etc.
|
|
206
|
+
* },
|
|
207
|
+
* context: async (event) => {
|
|
208
|
+
* return {
|
|
209
|
+
* user: await getUserFromEvent(event),
|
|
210
|
+
* db: getDatabase()
|
|
211
|
+
* }
|
|
212
|
+
* },
|
|
213
|
+
* maskedErrors: {
|
|
214
|
+
* maskError: createDefaultMaskError()
|
|
215
|
+
* },
|
|
216
|
+
* graphiql: true
|
|
217
|
+
* })
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* // Apollo Server configuration
|
|
223
|
+
* export default defineGraphQLConfig({
|
|
224
|
+
* apollo: {
|
|
225
|
+
* introspection: true,
|
|
226
|
+
* csrfPrevention: true,
|
|
227
|
+
* plugins: [ApolloServerPluginLandingPageGraphQLPlayground()]
|
|
228
|
+
* }
|
|
229
|
+
* })
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
declare function defineGraphQLConfig<T extends NPMConfig = NPMConfig>(config: Partial<DefineServerConfig<T>>): Partial<DefineServerConfig<T>>;
|
|
233
|
+
/**
|
|
234
|
+
* Define a custom GraphQL directive with full type safety.
|
|
235
|
+
*
|
|
236
|
+
* This function creates a GraphQL directive definition that can be used to add
|
|
237
|
+
* custom behavior to your GraphQL schema. It automatically generates the directive's
|
|
238
|
+
* schema definition string and provides runtime transformation capabilities.
|
|
239
|
+
*
|
|
240
|
+
* @param config - The directive configuration object
|
|
241
|
+
* @param config.name - The name of the directive (without the @ symbol)
|
|
242
|
+
* @param config.locations - Array of GraphQL directive locations where this directive can be applied
|
|
243
|
+
* @param config.args - Optional arguments the directive accepts with their types and default values
|
|
244
|
+
* @param config.transformer - Optional function to transform the schema when this directive is applied
|
|
245
|
+
* @returns A DirectiveDefinition object with the schema definition and runtime behavior
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils'
|
|
250
|
+
*
|
|
251
|
+
* export const upperDirective = defineDirective({
|
|
252
|
+
* name: 'upper',
|
|
253
|
+
* locations: ['FIELD_DEFINITION'],
|
|
254
|
+
* transformer: (schema) => {
|
|
255
|
+
* return mapSchema(schema, {
|
|
256
|
+
* [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
|
|
257
|
+
* const directive = getDirective(schema, fieldConfig, 'upper')?.[0]
|
|
258
|
+
* if (directive) {
|
|
259
|
+
* const { resolve = defaultFieldResolver } = fieldConfig
|
|
260
|
+
* fieldConfig.resolve = async (source, args, context, info) => {
|
|
261
|
+
* const result = await resolve(source, args, context, info)
|
|
262
|
+
* return typeof result === 'string' ? result.toUpperCase() : result
|
|
263
|
+
* }
|
|
264
|
+
* }
|
|
265
|
+
* return fieldConfig
|
|
266
|
+
* }
|
|
267
|
+
* })
|
|
268
|
+
* }
|
|
269
|
+
* })
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* // Directive with arguments
|
|
275
|
+
* export const authDirective = defineDirective({
|
|
276
|
+
* name: 'auth',
|
|
277
|
+
* locations: ['FIELD_DEFINITION', 'OBJECT'],
|
|
278
|
+
* args: {
|
|
279
|
+
* requires: {
|
|
280
|
+
* type: 'Role!',
|
|
281
|
+
* defaultValue: 'USER'
|
|
282
|
+
* }
|
|
283
|
+
* },
|
|
284
|
+
* transformer: (schema) => {
|
|
285
|
+
* // Implementation for auth checking
|
|
286
|
+
* }
|
|
287
|
+
* })
|
|
288
|
+
* ```
|
|
289
|
+
*
|
|
290
|
+
* @remarks
|
|
291
|
+
* The generated directive schema definition is stored as a non-enumerable `__schema`
|
|
292
|
+
* property on the returned object and is automatically included in your GraphQL schema.
|
|
293
|
+
*/
|
|
294
|
+
declare function defineDirective(config: DefineDirectiveConfig): DirectiveDefinition;
|
|
295
|
+
//#endregion
|
|
296
|
+
export { defineDirective, defineField, defineGraphQLConfig, defineMutation, defineQuery, defineResolver, defineSchema, defineSubscription };
|
package/dist/define.mjs
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
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 (parent, { id }, context) => {
|
|
43
|
+
* return await context.storage.getItem(`user:${id}`)
|
|
44
|
+
* }
|
|
45
|
+
* },
|
|
46
|
+
* Mutation: {
|
|
47
|
+
* createUser: async (parent, { 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, args, context) => `${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 (parent, { id }, context) => {
|
|
76
|
+
* return await fetchUser(id)
|
|
77
|
+
* },
|
|
78
|
+
* users: async (parent, args, 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 (parent, { input }, context) => {
|
|
104
|
+
* const user = await context.db.users.create(input)
|
|
105
|
+
* return user
|
|
106
|
+
* },
|
|
107
|
+
* updateUser: async (parent, { id, input }, context) => {
|
|
108
|
+
* return await context.db.users.update(id, input)
|
|
109
|
+
* },
|
|
110
|
+
* deleteUser: async (parent, { 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* (parent, { channelId }, context) {
|
|
139
|
+
* const pubsub = context.pubsub
|
|
140
|
+
* yield* pubsub.subscribe(`channel:${channelId}`)
|
|
141
|
+
* }
|
|
142
|
+
* },
|
|
143
|
+
* userStatusChanged: {
|
|
144
|
+
* subscribe: async (parent, args, 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 = defineField({
|
|
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 defineField(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
|
+
* import { createDefaultMaskError } from 'nitro-graphql/utils'
|
|
207
|
+
*
|
|
208
|
+
* // GraphQL Yoga configuration
|
|
209
|
+
* export default defineGraphQLConfig({
|
|
210
|
+
* schema: {
|
|
211
|
+
* // Schema directives, custom scalars, etc.
|
|
212
|
+
* },
|
|
213
|
+
* context: async (event) => {
|
|
214
|
+
* return {
|
|
215
|
+
* user: await getUserFromEvent(event),
|
|
216
|
+
* db: getDatabase()
|
|
217
|
+
* }
|
|
218
|
+
* },
|
|
219
|
+
* maskedErrors: {
|
|
220
|
+
* maskError: createDefaultMaskError()
|
|
221
|
+
* },
|
|
222
|
+
* graphiql: true
|
|
223
|
+
* })
|
|
224
|
+
* ```
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```typescript
|
|
228
|
+
* // Apollo Server configuration
|
|
229
|
+
* export default defineGraphQLConfig({
|
|
230
|
+
* apollo: {
|
|
231
|
+
* introspection: true,
|
|
232
|
+
* csrfPrevention: true,
|
|
233
|
+
* plugins: [ApolloServerPluginLandingPageGraphQLPlayground()]
|
|
234
|
+
* }
|
|
235
|
+
* })
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
function defineGraphQLConfig(config) {
|
|
239
|
+
return config;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Define a custom GraphQL directive with full type safety.
|
|
243
|
+
*
|
|
244
|
+
* This function creates a GraphQL directive definition that can be used to add
|
|
245
|
+
* custom behavior to your GraphQL schema. It automatically generates the directive's
|
|
246
|
+
* schema definition string and provides runtime transformation capabilities.
|
|
247
|
+
*
|
|
248
|
+
* @param config - The directive configuration object
|
|
249
|
+
* @param config.name - The name of the directive (without the @ symbol)
|
|
250
|
+
* @param config.locations - Array of GraphQL directive locations where this directive can be applied
|
|
251
|
+
* @param config.args - Optional arguments the directive accepts with their types and default values
|
|
252
|
+
* @param config.transformer - Optional function to transform the schema when this directive is applied
|
|
253
|
+
* @returns A DirectiveDefinition object with the schema definition and runtime behavior
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* import { mapSchema, getDirective, MapperKind } from '@graphql-tools/utils'
|
|
258
|
+
*
|
|
259
|
+
* export const upperDirective = defineDirective({
|
|
260
|
+
* name: 'upper',
|
|
261
|
+
* locations: ['FIELD_DEFINITION'],
|
|
262
|
+
* transformer: (schema) => {
|
|
263
|
+
* return mapSchema(schema, {
|
|
264
|
+
* [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
|
|
265
|
+
* const directive = getDirective(schema, fieldConfig, 'upper')?.[0]
|
|
266
|
+
* if (directive) {
|
|
267
|
+
* const { resolve = defaultFieldResolver } = fieldConfig
|
|
268
|
+
* fieldConfig.resolve = async (source, args, context, info) => {
|
|
269
|
+
* const result = await resolve(source, args, context, info)
|
|
270
|
+
* return typeof result === 'string' ? result.toUpperCase() : result
|
|
271
|
+
* }
|
|
272
|
+
* }
|
|
273
|
+
* return fieldConfig
|
|
274
|
+
* }
|
|
275
|
+
* })
|
|
276
|
+
* }
|
|
277
|
+
* })
|
|
278
|
+
* ```
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* // Directive with arguments
|
|
283
|
+
* export const authDirective = defineDirective({
|
|
284
|
+
* name: 'auth',
|
|
285
|
+
* locations: ['FIELD_DEFINITION', 'OBJECT'],
|
|
286
|
+
* args: {
|
|
287
|
+
* requires: {
|
|
288
|
+
* type: 'Role!',
|
|
289
|
+
* defaultValue: 'USER'
|
|
290
|
+
* }
|
|
291
|
+
* },
|
|
292
|
+
* transformer: (schema) => {
|
|
293
|
+
* // Implementation for auth checking
|
|
294
|
+
* }
|
|
295
|
+
* })
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* @remarks
|
|
299
|
+
* The generated directive schema definition is stored as a non-enumerable `__schema`
|
|
300
|
+
* property on the returned object and is automatically included in your GraphQL schema.
|
|
301
|
+
*/
|
|
302
|
+
function defineDirective(config) {
|
|
303
|
+
const args = config.args ? Object.entries(config.args).map(([name, arg]) => {
|
|
304
|
+
const defaultValue = arg.defaultValue !== void 0 ? ` = ${JSON.stringify(arg.defaultValue)}` : "";
|
|
305
|
+
return `${name}: ${arg.type}${defaultValue}`;
|
|
306
|
+
}).join(", ") : "";
|
|
307
|
+
const argsString = args ? `(${args})` : "";
|
|
308
|
+
const locations = config.locations.join(" | ");
|
|
309
|
+
const schemaDefinition = `directive @${config.name}${argsString} on ${locations}`;
|
|
310
|
+
Object.defineProperty(config, "__schema", {
|
|
311
|
+
value: schemaDefinition,
|
|
312
|
+
enumerable: false,
|
|
313
|
+
configurable: false,
|
|
314
|
+
writable: false
|
|
315
|
+
});
|
|
316
|
+
return {
|
|
317
|
+
...config,
|
|
318
|
+
locations: [...config.locations]
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
//#endregion
|
|
323
|
+
export { defineDirective, defineField, defineGraphQLConfig, defineMutation, defineQuery, defineResolver, defineSchema, defineSubscription };
|