wabe 0.6.9 → 0.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +156 -50
  2. package/bucket/b.txt +1 -0
  3. package/dev/index.ts +215 -0
  4. package/dist/authentication/Session.d.ts +4 -1
  5. package/dist/authentication/interface.d.ts +16 -0
  6. package/dist/cron/index.d.ts +0 -1
  7. package/dist/database/DatabaseController.d.ts +41 -13
  8. package/dist/database/interface.d.ts +1 -0
  9. package/dist/email/DevAdapter.d.ts +0 -1
  10. package/dist/email/interface.d.ts +1 -1
  11. package/dist/graphql/resolvers.d.ts +4 -2
  12. package/dist/hooks/index.d.ts +8 -2
  13. package/dist/index.d.ts +0 -1
  14. package/dist/index.js +32144 -32058
  15. package/dist/schema/Schema.d.ts +2 -1
  16. package/dist/server/index.d.ts +4 -2
  17. package/dist/utils/crypto.d.ts +7 -0
  18. package/dist/utils/helper.d.ts +5 -1
  19. package/generated/schema.graphql +22 -14
  20. package/generated/wabe.ts +4 -4
  21. package/package.json +23 -23
  22. package/src/authentication/OTP.test.ts +69 -0
  23. package/src/authentication/OTP.ts +64 -0
  24. package/src/authentication/Session.test.ts +629 -0
  25. package/src/authentication/Session.ts +493 -0
  26. package/src/authentication/defaultAuthentication.ts +209 -0
  27. package/src/authentication/index.ts +3 -0
  28. package/src/authentication/interface.ts +155 -0
  29. package/src/authentication/oauth/GitHub.test.ts +91 -0
  30. package/src/authentication/oauth/GitHub.ts +121 -0
  31. package/src/authentication/oauth/Google.test.ts +91 -0
  32. package/src/authentication/oauth/Google.ts +101 -0
  33. package/src/authentication/oauth/Oauth2Client.test.ts +219 -0
  34. package/src/authentication/oauth/Oauth2Client.ts +135 -0
  35. package/src/authentication/oauth/index.ts +2 -0
  36. package/src/authentication/oauth/utils.test.ts +33 -0
  37. package/src/authentication/oauth/utils.ts +27 -0
  38. package/src/authentication/providers/EmailOTP.test.ts +127 -0
  39. package/src/authentication/providers/EmailOTP.ts +84 -0
  40. package/src/authentication/providers/EmailPassword.test.ts +176 -0
  41. package/src/authentication/providers/EmailPassword.ts +116 -0
  42. package/src/authentication/providers/EmailPasswordSRP.test.ts +208 -0
  43. package/src/authentication/providers/EmailPasswordSRP.ts +179 -0
  44. package/src/authentication/providers/GitHub.ts +24 -0
  45. package/src/authentication/providers/Google.ts +24 -0
  46. package/src/authentication/providers/OAuth.test.ts +185 -0
  47. package/src/authentication/providers/OAuth.ts +106 -0
  48. package/src/authentication/providers/PhonePassword.test.ts +176 -0
  49. package/src/authentication/providers/PhonePassword.ts +115 -0
  50. package/src/authentication/providers/QRCodeOTP.test.ts +77 -0
  51. package/src/authentication/providers/QRCodeOTP.ts +58 -0
  52. package/src/authentication/providers/index.ts +6 -0
  53. package/src/authentication/resolvers/refreshResolver.test.ts +30 -0
  54. package/src/authentication/resolvers/refreshResolver.ts +19 -0
  55. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
  56. package/src/authentication/resolvers/signInWithResolver.test.ts +293 -0
  57. package/src/authentication/resolvers/signInWithResolver.ts +92 -0
  58. package/src/authentication/resolvers/signOutResolver.test.ts +38 -0
  59. package/src/authentication/resolvers/signOutResolver.ts +18 -0
  60. package/src/authentication/resolvers/signUpWithResolver.test.ts +180 -0
  61. package/src/authentication/resolvers/signUpWithResolver.ts +65 -0
  62. package/src/authentication/resolvers/verifyChallenge.test.ts +133 -0
  63. package/src/authentication/resolvers/verifyChallenge.ts +62 -0
  64. package/src/authentication/roles.test.ts +49 -0
  65. package/src/authentication/roles.ts +40 -0
  66. package/src/authentication/utils.test.ts +97 -0
  67. package/src/authentication/utils.ts +39 -0
  68. package/src/cache/InMemoryCache.test.ts +62 -0
  69. package/src/cache/InMemoryCache.ts +45 -0
  70. package/src/cron/index.test.ts +17 -0
  71. package/src/cron/index.ts +43 -0
  72. package/src/database/DatabaseController.test.ts +613 -0
  73. package/src/database/DatabaseController.ts +1007 -0
  74. package/src/database/index.test.ts +1372 -0
  75. package/src/database/index.ts +9 -0
  76. package/src/database/interface.ts +302 -0
  77. package/src/email/DevAdapter.ts +7 -0
  78. package/src/email/EmailController.test.ts +29 -0
  79. package/src/email/EmailController.ts +13 -0
  80. package/src/email/index.ts +2 -0
  81. package/src/email/interface.ts +36 -0
  82. package/src/email/templates/sendOtpCode.ts +120 -0
  83. package/src/file/FileController.ts +28 -0
  84. package/src/file/FileDevAdapter.ts +51 -0
  85. package/src/file/hookDeleteFile.ts +25 -0
  86. package/src/file/hookReadFile.ts +66 -0
  87. package/src/file/hookUploadFile.ts +50 -0
  88. package/src/file/index.test.ts +932 -0
  89. package/src/file/index.ts +2 -0
  90. package/src/file/interface.ts +39 -0
  91. package/src/graphql/GraphQLSchema.test.ts +4408 -0
  92. package/src/graphql/GraphQLSchema.ts +880 -0
  93. package/src/graphql/index.ts +2 -0
  94. package/src/graphql/parseGraphqlSchema.ts +85 -0
  95. package/src/graphql/parser.test.ts +203 -0
  96. package/src/graphql/parser.ts +542 -0
  97. package/src/graphql/pointerAndRelationFunction.ts +191 -0
  98. package/src/graphql/resolvers.ts +442 -0
  99. package/src/graphql/tests/aggregation.test.ts +1115 -0
  100. package/src/graphql/tests/e2e.test.ts +590 -0
  101. package/src/graphql/tests/scalars.test.ts +250 -0
  102. package/src/graphql/types.ts +227 -0
  103. package/src/hooks/HookObject.test.ts +122 -0
  104. package/src/hooks/HookObject.ts +165 -0
  105. package/src/hooks/authentication.ts +67 -0
  106. package/src/hooks/createUser.test.ts +77 -0
  107. package/src/hooks/createUser.ts +10 -0
  108. package/src/hooks/defaultFields.test.ts +176 -0
  109. package/src/hooks/defaultFields.ts +32 -0
  110. package/src/hooks/deleteSession.test.ts +181 -0
  111. package/src/hooks/deleteSession.ts +20 -0
  112. package/src/hooks/hashFieldHook.test.ts +152 -0
  113. package/src/hooks/hashFieldHook.ts +89 -0
  114. package/src/hooks/index.test.ts +258 -0
  115. package/src/hooks/index.ts +414 -0
  116. package/src/hooks/permissions.test.ts +412 -0
  117. package/src/hooks/permissions.ts +93 -0
  118. package/src/hooks/protected.test.ts +551 -0
  119. package/src/hooks/protected.ts +60 -0
  120. package/src/hooks/searchableFields.test.ts +147 -0
  121. package/src/hooks/searchableFields.ts +86 -0
  122. package/src/hooks/session.test.ts +134 -0
  123. package/src/hooks/session.ts +76 -0
  124. package/src/hooks/setEmail.test.ts +216 -0
  125. package/src/hooks/setEmail.ts +33 -0
  126. package/src/hooks/setupAcl.test.ts +618 -0
  127. package/src/hooks/setupAcl.ts +25 -0
  128. package/src/index.ts +9 -0
  129. package/src/schema/Schema.test.ts +482 -0
  130. package/src/schema/Schema.ts +757 -0
  131. package/src/schema/defaultResolvers.ts +93 -0
  132. package/src/schema/index.ts +1 -0
  133. package/src/schema/resolvers/meResolver.test.ts +62 -0
  134. package/src/schema/resolvers/meResolver.ts +10 -0
  135. package/src/schema/resolvers/resetPassword.test.ts +341 -0
  136. package/src/schema/resolvers/resetPassword.ts +63 -0
  137. package/src/schema/resolvers/sendEmail.test.ts +118 -0
  138. package/src/schema/resolvers/sendEmail.ts +21 -0
  139. package/src/schema/resolvers/sendOtpCode.test.ts +141 -0
  140. package/src/schema/resolvers/sendOtpCode.ts +52 -0
  141. package/src/security.test.ts +3434 -0
  142. package/src/server/defaultSessionHandler.test.ts +62 -0
  143. package/src/server/defaultSessionHandler.ts +105 -0
  144. package/src/server/generateCodegen.ts +433 -0
  145. package/src/server/index.test.ts +532 -0
  146. package/src/server/index.ts +334 -0
  147. package/src/server/interface.ts +11 -0
  148. package/src/server/routes/authHandler.ts +169 -0
  149. package/src/server/routes/index.ts +39 -0
  150. package/src/utils/crypto.test.ts +41 -0
  151. package/src/utils/crypto.ts +105 -0
  152. package/src/utils/export.ts +11 -0
  153. package/src/utils/helper.ts +204 -0
  154. package/src/utils/index.test.ts +11 -0
  155. package/src/utils/index.ts +189 -0
  156. package/src/utils/preload.ts +8 -0
  157. package/src/utils/testHelper.ts +116 -0
  158. package/tsconfig.json +32 -0
  159. package/bunfig.toml +0 -4
  160. package/dist/ai/index.d.ts +0 -1
  161. package/dist/ai/interface.d.ts +0 -9
  162. /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
@@ -0,0 +1,880 @@
1
+ import {
2
+ GraphQLEnumType,
3
+ type GraphQLFieldConfig,
4
+ GraphQLID,
5
+ GraphQLInputObjectType,
6
+ GraphQLInt,
7
+ GraphQLList,
8
+ GraphQLNonNull,
9
+ GraphQLObjectType,
10
+ type GraphQLOutputType,
11
+ GraphQLScalarType,
12
+ GraphQLBoolean,
13
+ GraphQLString,
14
+ } from 'graphql'
15
+ import { pluralize } from 'wabe-pluralize'
16
+ import type { WabeTypes } from '..'
17
+ import type {
18
+ ClassInterface,
19
+ MutationResolver,
20
+ QueryResolver,
21
+ Schema,
22
+ SchemaFields,
23
+ } from '../schema'
24
+ import { firstLetterInLowerCase } from '../utils'
25
+ import type { DevWabeTypes } from '../utils/helper'
26
+ import { GraphqlParser, type GraphqlParserFactory } from './parser'
27
+ import {
28
+ mutationToCreateMultipleObjects,
29
+ mutationToCreateObject,
30
+ mutationToDeleteMultipleObjects,
31
+ mutationToDeleteObject,
32
+ mutationToUpdateMultipleObjects,
33
+ mutationToUpdateObject,
34
+ queryForMultipleObject,
35
+ queryForOneObject,
36
+ } from './resolvers'
37
+ import { DateScalarType, FileScalarType, IdWhereInput, SearchWhereInput } from './types'
38
+
39
+ type AllPossibleObject =
40
+ | 'object'
41
+ | 'inputObject'
42
+ | 'whereInputObject'
43
+ | 'connectionObject'
44
+ | 'pointerInputObject'
45
+ | 'relationInputObject'
46
+ | 'updateInputObject'
47
+ | 'createInputObject'
48
+ | 'orderEnumType'
49
+
50
+ export type AllObjects = Record<string, Partial<Record<AllPossibleObject, any>>>
51
+
52
+ export class GraphQLSchema {
53
+ private schemas: Schema<DevWabeTypes>
54
+
55
+ private allObjects: AllObjects
56
+
57
+ constructor(schemas: Schema<any>) {
58
+ this.schemas = schemas
59
+ this.allObjects = {}
60
+ }
61
+
62
+ createSchema() {
63
+ if (!this.schemas) throw new Error('Schema not found')
64
+
65
+ const scalars = this.createScalars()
66
+ const enums = this.createEnums()
67
+
68
+ const classes = this.schemas.schema.classes || []
69
+
70
+ const graphqlParser = GraphqlParser({ scalars, enums })
71
+
72
+ classes.map((wabeClass) => this.createCompleteObject(graphqlParser, wabeClass))
73
+
74
+ const queriesMutationAndObjects = classes.reduce(
75
+ (acc, current) => {
76
+ const className = current.name.replace(' ', '')
77
+
78
+ const currentObject = this.allObjects[className]
79
+
80
+ if (!currentObject) throw new Error('Object not found')
81
+
82
+ const {
83
+ object,
84
+ inputObject,
85
+ pointerInputObject,
86
+ relationInputObject,
87
+ createInputObject,
88
+ updateInputObject,
89
+ whereInputObject,
90
+ connectionObject,
91
+ orderEnumType,
92
+ } = currentObject
93
+
94
+ // Queries
95
+ const defaultQueries = this.createDefaultQueries({
96
+ className,
97
+ whereInputType: whereInputObject,
98
+ object,
99
+ connectionObject,
100
+ orderEnumType,
101
+ })
102
+
103
+ const defaultMutations = this.createDefaultMutations({
104
+ className,
105
+ whereInputType: whereInputObject,
106
+ object,
107
+ connectionObject,
108
+ defaultUpdateInputType: updateInputObject,
109
+ defaultCreateInputType: createInputObject,
110
+ orderEnumType,
111
+ })
112
+
113
+ const defaultQueriesKeys = Object.keys(defaultQueries)
114
+ const defaultMutationsKeys = Object.keys(defaultMutations)
115
+
116
+ // Loop to avoid O(n)² complexity of spread on accumulator
117
+ for (const key in defaultQueriesKeys) {
118
+ // @ts-expect-error
119
+ acc.queries[defaultQueriesKeys[key]] =
120
+ // @ts-expect-error
121
+ defaultQueries[defaultQueriesKeys[key]]
122
+ }
123
+
124
+ for (const key in defaultMutationsKeys) {
125
+ // @ts-expect-error
126
+ acc.mutations[defaultMutationsKeys[key]] =
127
+ // @ts-expect-error
128
+ defaultMutations[defaultMutationsKeys[key]]
129
+ }
130
+
131
+ acc.objects.push(object)
132
+ acc.objects.push(inputObject)
133
+ acc.objects.push(pointerInputObject)
134
+ acc.objects.push(relationInputObject)
135
+
136
+ return acc
137
+ },
138
+ {
139
+ queries: {},
140
+ mutations: {},
141
+ objects: [...this.createFileObjects()],
142
+ } as {
143
+ queries: Record<string, GraphQLFieldConfig<any, any, any>>
144
+ mutations: Record<string, GraphQLFieldConfig<any, any, any>>
145
+ objects: Array<GraphQLObjectType | GraphQLInputObjectType>
146
+ },
147
+ )
148
+
149
+ const customQueries = this.createCustomQueries({
150
+ resolvers: this.schemas.schema.resolvers?.queries || {},
151
+ graphqlParser,
152
+ })
153
+ const customQueriesKeys = Object.keys(customQueries)
154
+
155
+ for (const key in customQueriesKeys) {
156
+ // @ts-expect-error
157
+ queriesMutationAndObjects.queries[customQueriesKeys[key]] =
158
+ // @ts-expect-error
159
+ customQueries[customQueriesKeys[key]]
160
+ }
161
+
162
+ // Mutations
163
+ const customMutations = this.createCustomMutations({
164
+ resolvers: this.schemas.schema.resolvers?.mutations || {},
165
+ graphqlParser,
166
+ })
167
+ const customMutationsKeys = Object.keys(customMutations)
168
+
169
+ for (const key in customMutationsKeys) {
170
+ // @ts-expect-error
171
+ queriesMutationAndObjects.mutations[customMutationsKeys[key]] =
172
+ // @ts-expect-error
173
+ customMutations[customMutationsKeys[key]]
174
+ }
175
+
176
+ return {
177
+ queries: queriesMutationAndObjects.queries,
178
+ mutations: queriesMutationAndObjects.mutations,
179
+ scalars,
180
+ enums,
181
+ objects: queriesMutationAndObjects.objects,
182
+ }
183
+ }
184
+
185
+ createFileObjects() {
186
+ const fileInfoObject = new GraphQLObjectType({
187
+ name: 'FileInfo',
188
+ description: 'Object containing information about the file',
189
+ fields: () => ({
190
+ name: { type: GraphQLString },
191
+ url: { type: GraphQLString },
192
+ urlGeneratedAt: {
193
+ type: DateScalarType,
194
+ },
195
+ isPresignedUrl: { type: GraphQLBoolean },
196
+ }),
197
+ })
198
+
199
+ const fileInputObject = new GraphQLInputObjectType({
200
+ name: 'FileInput',
201
+ description: 'Input to create a file',
202
+ fields: () => ({
203
+ file: { type: FileScalarType },
204
+ url: { type: GraphQLString },
205
+ }),
206
+ })
207
+
208
+ this.allObjects.FileInfo = {
209
+ object: fileInfoObject,
210
+ inputObject: fileInputObject,
211
+ }
212
+
213
+ return [fileInfoObject]
214
+ }
215
+
216
+ createScalars() {
217
+ return (
218
+ this.schemas.schema.scalars?.map(
219
+ (scalar) =>
220
+ new GraphQLScalarType({
221
+ ...scalar,
222
+ }),
223
+ ) || []
224
+ )
225
+ }
226
+
227
+ createOrderEnumType(wabeClass: ClassInterface<DevWabeTypes>) {
228
+ const fields = wabeClass.fields
229
+
230
+ const classEnums = Object.keys(fields).reduce(
231
+ (acc, fieldName) => {
232
+ acc[`${fieldName}_ASC`] = { value: { [fieldName]: 'ASC' } }
233
+ acc[`${fieldName}_DESC`] = { value: { [fieldName]: 'DESC' } }
234
+
235
+ return acc
236
+ },
237
+ {} as Record<string, any>,
238
+ )
239
+
240
+ return new GraphQLEnumType({
241
+ name: `${wabeClass.name}Order`,
242
+ values: classEnums,
243
+ })
244
+ }
245
+
246
+ createEnums() {
247
+ return (
248
+ this.schemas.schema.enums?.map((wabeEnum) => {
249
+ const enumValues = wabeEnum.values
250
+
251
+ const values = Object.keys(enumValues).reduce(
252
+ (acc, value) => {
253
+ acc[value] = { value: enumValues[value] }
254
+
255
+ return acc
256
+ },
257
+ {} as Record<string, any>,
258
+ )
259
+
260
+ return new GraphQLEnumType({
261
+ ...wabeEnum,
262
+ values,
263
+ })
264
+ }) || []
265
+ )
266
+ }
267
+
268
+ createObject({
269
+ wabeClass,
270
+ graphqlParser,
271
+ }: {
272
+ wabeClass: ClassInterface<DevWabeTypes>
273
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>
274
+ }) {
275
+ const { name, fields, description } = wabeClass
276
+
277
+ const nameWithoutSpace = name.replace(' ', '')
278
+
279
+ const graphqlParserWithInput = graphqlParser({
280
+ schemaFields: fields,
281
+ graphqlObjectType: 'Object',
282
+ allObjects: this.allObjects,
283
+ })
284
+
285
+ return new GraphQLObjectType({
286
+ name: nameWithoutSpace,
287
+ description,
288
+ // We need to use function here to have lazy loading of fields
289
+ fields: () => ({
290
+ id: { type: new GraphQLNonNull(GraphQLID) },
291
+ ...graphqlParserWithInput.getGraphqlFields(nameWithoutSpace),
292
+ }),
293
+ })
294
+ }
295
+
296
+ createPointerInputObject({
297
+ wabeClass,
298
+ inputCreateFields,
299
+ }: {
300
+ wabeClass: ClassInterface<DevWabeTypes>
301
+ inputCreateFields: GraphQLInputObjectType
302
+ }) {
303
+ const { name } = wabeClass
304
+
305
+ const nameWithoutSpace = name.replace(' ', '')
306
+
307
+ return new GraphQLInputObjectType({
308
+ name: `${nameWithoutSpace}PointerInput`,
309
+ description: `Input to link an object to a pointer ${nameWithoutSpace}`,
310
+ fields: () => ({
311
+ unlink: { type: GraphQLBoolean },
312
+ link: { type: GraphQLID },
313
+ createAndLink: { type: inputCreateFields },
314
+ }),
315
+ })
316
+ }
317
+
318
+ createRelationInputObject({
319
+ wabeClass,
320
+ inputCreateFields,
321
+ }: {
322
+ wabeClass: ClassInterface<DevWabeTypes>
323
+ inputCreateFields: GraphQLInputObjectType
324
+ }) {
325
+ const { name } = wabeClass
326
+
327
+ const nameWithoutSpace = name.replace(' ', '')
328
+
329
+ return new GraphQLInputObjectType({
330
+ name: `${nameWithoutSpace}RelationInput`,
331
+ description: `Input to add a relation to the class ${nameWithoutSpace}`,
332
+ fields: () => ({
333
+ add: { type: new GraphQLList(new GraphQLNonNull(GraphQLID)) },
334
+ remove: {
335
+ type: new GraphQLList(new GraphQLNonNull(GraphQLID)),
336
+ },
337
+ createAndAdd: {
338
+ type: new GraphQLList(new GraphQLNonNull(inputCreateFields)),
339
+ },
340
+ }),
341
+ })
342
+ }
343
+
344
+ createInputObject({
345
+ wabeClass,
346
+ graphqlParser,
347
+ }: {
348
+ wabeClass: ClassInterface<DevWabeTypes>
349
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>
350
+ }) {
351
+ const { name, fields, description } = wabeClass
352
+
353
+ const nameWithoutSpace = name.replace(' ', '')
354
+
355
+ const graphqlParserWithInput = graphqlParser({
356
+ schemaFields: fields,
357
+ graphqlObjectType: 'InputObject',
358
+ allObjects: this.allObjects,
359
+ })
360
+
361
+ return new GraphQLInputObjectType({
362
+ name: `${nameWithoutSpace}Input`,
363
+ description,
364
+ fields: () => ({
365
+ ...graphqlParserWithInput.getGraphqlFields(nameWithoutSpace),
366
+ }),
367
+ })
368
+ }
369
+
370
+ createCreateInputObject({
371
+ wabeClass,
372
+ graphqlParser,
373
+ }: {
374
+ wabeClass: ClassInterface<DevWabeTypes>
375
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>
376
+ }) {
377
+ const { name, fields, description } = wabeClass
378
+
379
+ const nameWithoutSpace = name.replace(' ', '')
380
+
381
+ const graphqlParserWithInput = graphqlParser({
382
+ schemaFields: fields,
383
+ graphqlObjectType: 'CreateFieldsInput',
384
+ allObjects: this.allObjects,
385
+ })
386
+
387
+ return new GraphQLInputObjectType({
388
+ name: `${nameWithoutSpace}CreateFieldsInput`,
389
+ description,
390
+ fields: () => ({
391
+ ...graphqlParserWithInput.getGraphqlFields(nameWithoutSpace),
392
+ }),
393
+ })
394
+ }
395
+
396
+ createUpdateInputObject({
397
+ wabeClass,
398
+ graphqlParser,
399
+ }: {
400
+ wabeClass: ClassInterface<DevWabeTypes>
401
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>
402
+ }) {
403
+ const { name, fields, description } = wabeClass
404
+
405
+ const nameWithoutSpace = name.replace(' ', '')
406
+
407
+ const graphqlParserWithInput = graphqlParser({
408
+ schemaFields: fields,
409
+ graphqlObjectType: 'UpdateFieldsInput',
410
+ allObjects: this.allObjects,
411
+ })
412
+
413
+ return new GraphQLInputObjectType({
414
+ name: `${nameWithoutSpace}UpdateFieldsInput`,
415
+ description,
416
+ fields: () => ({
417
+ ...graphqlParserWithInput.getGraphqlFields(nameWithoutSpace),
418
+ }),
419
+ })
420
+ }
421
+
422
+ createWhereInputObject({
423
+ wabeClass,
424
+ graphqlParser,
425
+ }: {
426
+ wabeClass: ClassInterface<DevWabeTypes>
427
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>
428
+ }) {
429
+ const { name, fields, description } = wabeClass
430
+
431
+ const nameWithoutSpace = name.replace(' ', '')
432
+
433
+ const graphqlParserWithInput = graphqlParser({
434
+ schemaFields: fields,
435
+ graphqlObjectType: 'WhereInputObject',
436
+ allObjects: this.allObjects,
437
+ })
438
+
439
+ const inputObject = new GraphQLInputObjectType({
440
+ name: `${nameWithoutSpace}WhereInput`,
441
+ description,
442
+ fields: (): any => ({
443
+ id: { type: IdWhereInput },
444
+ ...graphqlParserWithInput.getGraphqlFields(nameWithoutSpace),
445
+ OR: {
446
+ type: new GraphQLList(inputObject),
447
+ },
448
+ AND: {
449
+ type: new GraphQLList(inputObject),
450
+ },
451
+ search: { type: SearchWhereInput },
452
+ }),
453
+ })
454
+
455
+ return inputObject
456
+ }
457
+
458
+ createConnectionObject({
459
+ object,
460
+ wabeClass,
461
+ }: {
462
+ object: GraphQLObjectType
463
+ wabeClass: ClassInterface<DevWabeTypes>
464
+ }) {
465
+ const edgeObject = new GraphQLObjectType({
466
+ name: `${wabeClass.name}Edge`,
467
+ fields: () => ({
468
+ node: { type: new GraphQLNonNull(object) },
469
+ }),
470
+ })
471
+
472
+ const connectionObject = new GraphQLObjectType({
473
+ name: `${wabeClass.name}Connection`,
474
+ fields: () => ({
475
+ ok: { type: GraphQLBoolean },
476
+ totalCount: { type: GraphQLInt },
477
+ edges: { type: new GraphQLList(edgeObject) },
478
+ }),
479
+ })
480
+
481
+ return connectionObject
482
+ }
483
+
484
+ createCompleteObject(
485
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>,
486
+ wabeClass: ClassInterface<DevWabeTypes>,
487
+ ) {
488
+ const object = this.createObject({ graphqlParser, wabeClass })
489
+
490
+ const connectionObject = this.createConnectionObject({
491
+ object,
492
+ wabeClass,
493
+ })
494
+
495
+ const inputObject = this.createInputObject({
496
+ graphqlParser,
497
+ wabeClass,
498
+ })
499
+
500
+ const createInputObject = this.createCreateInputObject({
501
+ graphqlParser,
502
+ wabeClass,
503
+ })
504
+
505
+ const pointerInputObject = this.createPointerInputObject({
506
+ inputCreateFields: createInputObject,
507
+ wabeClass,
508
+ })
509
+
510
+ const relationInputObject = this.createRelationInputObject({
511
+ inputCreateFields: createInputObject,
512
+ wabeClass,
513
+ })
514
+
515
+ const updateInputObject = this.createUpdateInputObject({
516
+ graphqlParser,
517
+ wabeClass,
518
+ })
519
+
520
+ const whereInputObject = this.createWhereInputObject({
521
+ graphqlParser,
522
+ wabeClass,
523
+ })
524
+
525
+ const orderEnumType = this.createOrderEnumType(wabeClass)
526
+
527
+ this.allObjects[wabeClass.name] = {
528
+ connectionObject,
529
+ createInputObject,
530
+ updateInputObject,
531
+ whereInputObject,
532
+ pointerInputObject,
533
+ relationInputObject,
534
+ inputObject,
535
+ object,
536
+ orderEnumType,
537
+ }
538
+ }
539
+
540
+ _getGraphQLOutputType(
541
+ currentQueryOrMutation: QueryResolver<DevWabeTypes> | MutationResolver<DevWabeTypes>,
542
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>,
543
+ currentArgs: SchemaFields<DevWabeTypes>,
544
+ ): GraphQLOutputType | undefined {
545
+ if (currentQueryOrMutation.type === 'Object') {
546
+ const objectGraphqlParser = graphqlParser({
547
+ schemaFields: currentQueryOrMutation.outputObject.fields,
548
+ graphqlObjectType: 'Object',
549
+ allObjects: this.allObjects,
550
+ })
551
+
552
+ return new GraphQLObjectType({
553
+ name: currentQueryOrMutation.outputObject.name,
554
+ fields: () =>
555
+ objectGraphqlParser.getGraphqlFields(currentQueryOrMutation.outputObject.name),
556
+ })
557
+ }
558
+
559
+ if (currentQueryOrMutation.type === 'Array' && currentQueryOrMutation.typeValue === 'Object') {
560
+ const outputObject = graphqlParser({
561
+ schemaFields: currentQueryOrMutation.outputObject.fields,
562
+ graphqlObjectType: 'Object',
563
+ allObjects: this.allObjects,
564
+ })
565
+
566
+ const graphqlObject = new GraphQLObjectType({
567
+ name: currentQueryOrMutation.outputObject.name,
568
+ fields: () => outputObject.getGraphqlFields(currentQueryOrMutation.outputObject.name),
569
+ })
570
+
571
+ return new GraphQLList(
572
+ currentQueryOrMutation.typeValueRequired
573
+ ? new GraphQLNonNull(graphqlObject)
574
+ : graphqlObject,
575
+ )
576
+ }
577
+
578
+ const graphqlParserWithInput = graphqlParser({
579
+ schemaFields: currentArgs,
580
+ graphqlObjectType: 'Object',
581
+ allObjects: this.allObjects,
582
+ })
583
+
584
+ return graphqlParserWithInput.getGraphqlType(currentQueryOrMutation)
585
+ }
586
+
587
+ createCustomMutations({
588
+ resolvers,
589
+ graphqlParser,
590
+ }: {
591
+ resolvers: Record<string, MutationResolver<DevWabeTypes>>
592
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>
593
+ }) {
594
+ return Object.keys(resolvers).reduce(
595
+ (acc, currentKey) => {
596
+ const currentMutation = resolvers[currentKey]
597
+
598
+ if (!currentMutation) return acc
599
+
600
+ const required = !!currentMutation?.required
601
+ const input = currentMutation?.args?.input || {}
602
+ const numberOfFieldsInInput = Object.keys(input).length
603
+
604
+ const currentKeyWithFirstLetterUpperCase = `${currentKey[0]?.toUpperCase()}${currentKey.slice(
605
+ 1,
606
+ )}`
607
+
608
+ const graphqlParserWithInput = graphqlParser({
609
+ schemaFields: input,
610
+ graphqlObjectType: 'InputObject',
611
+ allObjects: this.allObjects,
612
+ })
613
+
614
+ const outputType = this._getGraphQLOutputType(currentMutation, graphqlParser, input)
615
+
616
+ if (!outputType) throw new Error('Invalid mutation output type')
617
+
618
+ const graphqlInput = new GraphQLInputObjectType({
619
+ name: `${currentKeyWithFirstLetterUpperCase}Input`,
620
+ fields: graphqlParserWithInput.getGraphqlFields(currentKeyWithFirstLetterUpperCase),
621
+ })
622
+
623
+ acc[currentKey] = {
624
+ type: required ? new GraphQLNonNull(outputType) : outputType,
625
+ args:
626
+ numberOfFieldsInInput > 0
627
+ ? { input: { type: new GraphQLNonNull(graphqlInput) } }
628
+ : undefined,
629
+ description: currentMutation.description,
630
+ resolve: currentMutation.resolve,
631
+ }
632
+
633
+ return acc
634
+ },
635
+ {} as Record<string, GraphQLFieldConfig<any, any, any>>,
636
+ )
637
+ }
638
+
639
+ createCustomQueries({
640
+ resolvers,
641
+ graphqlParser,
642
+ }: {
643
+ resolvers: Record<string, QueryResolver<DevWabeTypes>>
644
+ graphqlParser: GraphqlParserFactory<DevWabeTypes>
645
+ }) {
646
+ return Object.keys(resolvers).reduce(
647
+ (acc, currentKey) => {
648
+ const currentQuery = resolvers[currentKey]
649
+
650
+ if (!currentQuery) return acc
651
+
652
+ const required = !!currentQuery?.required
653
+ const currentArgs = currentQuery?.args || {}
654
+
655
+ const graphqlParserWithInput = graphqlParser({
656
+ schemaFields: currentArgs,
657
+ graphqlObjectType: 'Object',
658
+ allObjects: this.allObjects,
659
+ })
660
+
661
+ const outputType = this._getGraphQLOutputType(currentQuery, graphqlParser, currentArgs)
662
+
663
+ if (!outputType) throw new Error('Invalid mutation output type')
664
+
665
+ acc[currentKey] = {
666
+ type: required ? new GraphQLNonNull(outputType) : outputType,
667
+ args: graphqlParserWithInput.getGraphqlFields(currentKey),
668
+ description: currentQuery.description,
669
+ resolve: currentQuery.resolve,
670
+ }
671
+
672
+ return acc
673
+ },
674
+ {} as Record<string, GraphQLFieldConfig<any, any, any>>,
675
+ )
676
+ }
677
+
678
+ createDefaultQueries({
679
+ className,
680
+ whereInputType,
681
+ object,
682
+ connectionObject,
683
+ orderEnumType,
684
+ }: {
685
+ className: string
686
+ whereInputType: GraphQLInputObjectType
687
+ object: GraphQLObjectType
688
+ connectionObject: GraphQLObjectType
689
+ orderEnumType: GraphQLEnumType
690
+ }) {
691
+ const classNameWithFirstLetterLowerCase = firstLetterInLowerCase(className)
692
+
693
+ return {
694
+ [classNameWithFirstLetterLowerCase]: {
695
+ type: object,
696
+ description: object.description,
697
+ args: { id: { type: GraphQLID } },
698
+ resolve: (root, args, ctx, info) => queryForOneObject(root, args, ctx, info, className),
699
+ },
700
+ [pluralize(classNameWithFirstLetterLowerCase)]: {
701
+ type: new GraphQLNonNull(connectionObject),
702
+ description: object.description,
703
+ args: {
704
+ where: { type: whereInputType },
705
+ offset: { type: GraphQLInt },
706
+ first: { type: GraphQLInt },
707
+ order: { type: new GraphQLList(new GraphQLNonNull(orderEnumType)) },
708
+ },
709
+ resolve: (root, args, ctx, info) =>
710
+ queryForMultipleObject(root, args, ctx, info, className),
711
+ },
712
+ } as Record<string, GraphQLFieldConfig<any, any, any>>
713
+ }
714
+
715
+ createDefaultMutations({
716
+ className,
717
+ object,
718
+ defaultUpdateInputType,
719
+ defaultCreateInputType,
720
+ whereInputType,
721
+ connectionObject,
722
+ orderEnumType,
723
+ }: {
724
+ className: string
725
+ defaultUpdateInputType: GraphQLInputObjectType
726
+ defaultCreateInputType: GraphQLInputObjectType
727
+ whereInputType: GraphQLInputObjectType
728
+ object: GraphQLObjectType
729
+ connectionObject: GraphQLObjectType
730
+ orderEnumType: GraphQLEnumType
731
+ }) {
732
+ const classNameWithFirstLetterLowerCase = firstLetterInLowerCase(className)
733
+
734
+ const pluralClassName = pluralize(className)
735
+
736
+ const createPayloadType = new GraphQLObjectType({
737
+ name: `Create${className}Payload`,
738
+ fields: () => ({
739
+ [classNameWithFirstLetterLowerCase]: { type: object },
740
+ ok: { type: GraphQLBoolean },
741
+ }),
742
+ })
743
+
744
+ const createInputType = new GraphQLInputObjectType({
745
+ name: `Create${className}Input`,
746
+ fields: () => ({
747
+ fields: { type: defaultCreateInputType },
748
+ }),
749
+ })
750
+
751
+ const createsInputType = new GraphQLInputObjectType({
752
+ name: `Create${pluralClassName}Input`,
753
+ fields: () => ({
754
+ fields: {
755
+ type: new GraphQLNonNull(new GraphQLList(defaultCreateInputType)),
756
+ },
757
+ offset: { type: GraphQLInt },
758
+ first: { type: GraphQLInt },
759
+ order: { type: new GraphQLList(orderEnumType) },
760
+ }),
761
+ })
762
+
763
+ const updatePayloadType = new GraphQLObjectType({
764
+ name: `Update${className}Payload`,
765
+ fields: () => ({
766
+ [classNameWithFirstLetterLowerCase]: { type: object },
767
+ ok: { type: GraphQLBoolean },
768
+ }),
769
+ })
770
+
771
+ const updateInputType = new GraphQLInputObjectType({
772
+ name: `Update${className}Input`,
773
+ fields: () => ({
774
+ id: { type: GraphQLID },
775
+ fields: { type: defaultUpdateInputType },
776
+ }),
777
+ })
778
+
779
+ const updatesInputType = new GraphQLInputObjectType({
780
+ name: `Update${pluralClassName}Input`,
781
+ fields: () => ({
782
+ fields: { type: defaultUpdateInputType },
783
+ where: { type: whereInputType },
784
+ offset: { type: GraphQLInt },
785
+ first: { type: GraphQLInt },
786
+ order: { type: new GraphQLList(orderEnumType) },
787
+ }),
788
+ })
789
+
790
+ const deletePayloadType = new GraphQLObjectType({
791
+ name: `Delete${className}Payload`,
792
+ fields: () => ({
793
+ [classNameWithFirstLetterLowerCase]: { type: object },
794
+ ok: { type: GraphQLBoolean },
795
+ }),
796
+ })
797
+
798
+ const deleteInputType = new GraphQLInputObjectType({
799
+ name: `Delete${className}Input`,
800
+ fields: () => ({
801
+ id: { type: GraphQLID },
802
+ }),
803
+ })
804
+
805
+ const deletesInputType = new GraphQLInputObjectType({
806
+ name: `Delete${pluralClassName}Input`,
807
+ fields: () => ({
808
+ where: { type: whereInputType },
809
+ order: { type: new GraphQLList(orderEnumType) },
810
+ }),
811
+ })
812
+
813
+ return {
814
+ [`create${className}`]: {
815
+ type: createPayloadType,
816
+ description: object.description,
817
+ args: { input: { type: new GraphQLNonNull(createInputType) } },
818
+ resolve: (root, args, ctx, info) =>
819
+ mutationToCreateObject(root, args, ctx, info, className as keyof WabeTypes['types']),
820
+ },
821
+ [`create${pluralize(className)}`]: {
822
+ type: new GraphQLNonNull(connectionObject),
823
+ description: object.description,
824
+ args: { input: { type: new GraphQLNonNull(createsInputType) } },
825
+ resolve: (root, args, ctx, info) =>
826
+ mutationToCreateMultipleObjects(
827
+ root,
828
+ args,
829
+ ctx,
830
+ info,
831
+ className as keyof WabeTypes['types'],
832
+ ),
833
+ },
834
+ [`update${className}`]: {
835
+ type: updatePayloadType,
836
+ description: object.description,
837
+ args: { input: { type: new GraphQLNonNull(updateInputType) } },
838
+ resolve: (root, args, ctx, info) =>
839
+ mutationToUpdateObject(root, args, ctx, info, className as keyof WabeTypes['types']),
840
+ },
841
+ [`update${pluralize(className)}`]: {
842
+ type: new GraphQLNonNull(connectionObject),
843
+ description: object.description,
844
+ args: { input: { type: new GraphQLNonNull(updatesInputType) } },
845
+ resolve: (root, args, ctx, info) =>
846
+ mutationToUpdateMultipleObjects(
847
+ root,
848
+ args,
849
+ ctx,
850
+ info,
851
+ className as keyof WabeTypes['types'],
852
+ ),
853
+ },
854
+ [`delete${className}`]: {
855
+ type: deletePayloadType,
856
+ description: object.description,
857
+ args: {
858
+ input: {
859
+ type: new GraphQLNonNull(deleteInputType),
860
+ },
861
+ },
862
+ resolve: (root, args, ctx, info) =>
863
+ mutationToDeleteObject(root, args, ctx, info, className as keyof WabeTypes['types']),
864
+ },
865
+ [`delete${pluralize(className)}`]: {
866
+ type: new GraphQLNonNull(connectionObject),
867
+ description: object.description,
868
+ args: { input: { type: new GraphQLNonNull(deletesInputType) } },
869
+ resolve: (root, args, ctx, info) =>
870
+ mutationToDeleteMultipleObjects(
871
+ root,
872
+ args,
873
+ ctx,
874
+ info,
875
+ className as keyof WabeTypes['types'],
876
+ ),
877
+ },
878
+ } as Record<string, GraphQLFieldConfig<any, any, any>>
879
+ }
880
+ }