-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphql.zod.ts
More file actions
1123 lines (907 loc) · 39.8 KB
/
graphql.zod.ts
File metadata and controls
1123 lines (907 loc) · 39.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { FieldType } from '../data/field.zod';
/**
* GraphQL Protocol Support
*
* GraphQL is a query language for APIs and a runtime for executing those queries.
* It provides a complete and understandable description of the data in your API,
* gives clients the power to ask for exactly what they need, and enables powerful
* developer tools.
*
* ## Overview
*
* GraphQL provides:
* - Type-safe schema definition
* - Precise data fetching (no over/under-fetching)
* - Introspection and documentation
* - Real-time subscriptions
* - Batched queries with DataLoader
*
* ## Use Cases
*
* 1. **Modern API Development**
* - Mobile and web applications
* - Microservices federation
* - Real-time dashboards
*
* 2. **Data Aggregation**
* - Multi-source data integration
* - Complex nested queries
* - Efficient data loading
*
* 3. **Developer Experience**
* - Self-documenting API
* - Type safety and validation
* - GraphQL playground
*
* @see https://graphql.org/
* @see https://spec.graphql.org/
*
* @example GraphQL Query
* ```graphql
* query GetCustomer($id: ID!) {
* customer(id: $id) {
* id
* name
* email
* orders(limit: 10, status: "active") {
* id
* total
* items {
* product {
* name
* price
* }
* }
* }
* }
* }
* ```
*
* @example GraphQL Mutation
* ```graphql
* mutation CreateOrder($input: CreateOrderInput!) {
* createOrder(input: $input) {
* id
* orderNumber
* status
* }
* }
* ```
*/
// ==========================================
// 1. GraphQL Type System
// ==========================================
/**
* GraphQL Scalar Types
*
* Built-in scalar types in GraphQL plus custom scalars.
*/
export const GraphQLScalarType = z.enum([
// Built-in GraphQL Scalars
'ID',
'String',
'Int',
'Float',
'Boolean',
// Extended Scalars (common custom types)
'DateTime',
'Date',
'Time',
'JSON',
'JSONObject',
'Upload',
'URL',
'Email',
'PhoneNumber',
'Currency',
'Decimal',
'BigInt',
'Long',
'UUID',
'Base64',
'Void',
]);
export type GraphQLScalarType = z.infer<typeof GraphQLScalarType>;
/**
* GraphQL Type Configuration
*
* Configuration for generating GraphQL types from Object definitions.
*/
export const GraphQLTypeConfigSchema = z.object({
/** Type name in GraphQL schema */
name: z.string().describe('GraphQL type name (PascalCase recommended)'),
/** Source Object name */
object: z.string().describe('Source ObjectQL object name'),
/** Description for GraphQL schema documentation */
description: z.string().optional().describe('Type description'),
/** Fields to include/exclude */
fields: z.object({
/** Include only these fields (allow list) */
include: z.array(z.string()).optional().describe('Fields to include'),
/** Exclude these fields (deny list) */
exclude: z.array(z.string()).optional().describe('Fields to exclude (e.g., sensitive fields)'),
/** Custom field mappings */
mappings: z.record(z.string(), z.object({
graphqlName: z.string().optional().describe('Custom GraphQL field name'),
graphqlType: z.string().optional().describe('Override GraphQL type'),
description: z.string().optional().describe('Field description'),
deprecationReason: z.string().optional().describe('Why field is deprecated'),
nullable: z.boolean().optional().describe('Override nullable'),
})).optional().describe('Field-level customizations'),
}).optional().describe('Field configuration'),
/** Interfaces this type implements */
interfaces: z.array(z.string()).optional().describe('GraphQL interface names'),
/** Whether this is an interface definition */
isInterface: z.boolean().optional().default(false).describe('Define as GraphQL interface'),
/** Custom directives */
directives: z.array(z.object({
name: z.string().describe('Directive name'),
args: z.record(z.string(), z.unknown()).optional().describe('Directive arguments'),
})).optional().describe('GraphQL directives'),
});
export type GraphQLTypeConfig = z.infer<typeof GraphQLTypeConfigSchema>;
export type GraphQLTypeConfigInput = z.input<typeof GraphQLTypeConfigSchema>;
// ==========================================
// 2. Query Generation Configuration
// ==========================================
/**
* GraphQL Query Configuration
*
* Configuration for auto-generating query fields from Objects.
*/
export const GraphQLQueryConfigSchema = z.object({
/** Query name */
name: z.string().describe('Query field name (camelCase recommended)'),
/** Source Object */
object: z.string().describe('Source ObjectQL object name'),
/** Query type: single record or list */
type: z.enum(['get', 'list', 'search']).describe('Query type'),
/** Description */
description: z.string().optional().describe('Query description'),
/** Input arguments */
args: z.record(z.string(), z.object({
type: z.string().describe('GraphQL type (e.g., "ID!", "String", "Int")'),
description: z.string().optional().describe('Argument description'),
defaultValue: z.unknown().optional().describe('Default value'),
})).optional().describe('Query arguments'),
/** Filtering configuration */
filtering: z.object({
enabled: z.boolean().default(true).describe('Allow filtering'),
fields: z.array(z.string()).optional().describe('Filterable fields'),
operators: z.array(z.enum([
'eq', 'ne', 'gt', 'gte', 'lt', 'lte',
'in', 'notIn', 'contains', 'startsWith', 'endsWith',
'isNull', 'isNotNull',
])).optional().describe('Allowed filter operators'),
}).optional().describe('Filtering capabilities'),
/** Sorting configuration */
sorting: z.object({
enabled: z.boolean().default(true).describe('Allow sorting'),
fields: z.array(z.string()).optional().describe('Sortable fields'),
defaultSort: z.object({
field: z.string(),
direction: z.enum(['ASC', 'DESC']),
}).optional().describe('Default sort order'),
}).optional().describe('Sorting capabilities'),
/** Pagination configuration */
pagination: z.object({
enabled: z.boolean().default(true).describe('Enable pagination'),
type: z.enum(['offset', 'cursor', 'relay']).default('offset').describe('Pagination style'),
defaultLimit: z.number().int().min(1).default(20).describe('Default page size'),
maxLimit: z.number().int().min(1).default(100).describe('Maximum page size'),
cursors: z.object({
field: z.string().default('id').describe('Field to use for cursor pagination'),
}).optional(),
}).optional().describe('Pagination configuration'),
/** Field selection */
fields: z.object({
/** Always include these fields */
required: z.array(z.string()).optional().describe('Required fields (always returned)'),
/** Allow selecting these fields */
selectable: z.array(z.string()).optional().describe('Selectable fields'),
}).optional().describe('Field selection configuration'),
/** Authorization */
authRequired: z.boolean().default(true).describe('Require authentication'),
permissions: z.array(z.string()).optional().describe('Required permissions'),
/** Caching */
cache: z.object({
enabled: z.boolean().default(false).describe('Enable caching'),
ttl: z.number().int().min(0).optional().describe('Cache TTL in seconds'),
key: z.string().optional().describe('Cache key template'),
}).optional().describe('Query caching'),
});
export type GraphQLQueryConfig = z.infer<typeof GraphQLQueryConfigSchema>;
export type GraphQLQueryConfigInput = z.input<typeof GraphQLQueryConfigSchema>;
// ==========================================
// 3. Mutation Generation Configuration
// ==========================================
/**
* GraphQL Mutation Configuration
*
* Configuration for auto-generating mutation fields from Objects.
*/
export const GraphQLMutationConfigSchema = z.object({
/** Mutation name */
name: z.string().describe('Mutation field name (camelCase recommended)'),
/** Source Object */
object: z.string().describe('Source ObjectQL object name'),
/** Mutation type */
type: z.enum(['create', 'update', 'delete', 'upsert', 'custom']).describe('Mutation type'),
/** Description */
description: z.string().optional().describe('Mutation description'),
/** Input type configuration */
input: z.object({
/** Input type name */
typeName: z.string().optional().describe('Custom input type name'),
/** Fields to include in input */
fields: z.object({
include: z.array(z.string()).optional().describe('Fields to include'),
exclude: z.array(z.string()).optional().describe('Fields to exclude'),
required: z.array(z.string()).optional().describe('Required input fields'),
}).optional().describe('Input field configuration'),
/** Validation */
validation: z.object({
enabled: z.boolean().default(true).describe('Enable input validation'),
rules: z.array(z.string()).optional().describe('Custom validation rules'),
}).optional().describe('Input validation'),
}).optional().describe('Input configuration'),
/** Return type configuration */
output: z.object({
/** Type of output */
type: z.enum(['object', 'payload', 'boolean', 'custom']).default('object').describe('Output type'),
/** Include success/error envelope */
includeEnvelope: z.boolean().optional().default(false).describe('Wrap in success/error payload'),
/** Custom output type */
customType: z.string().optional().describe('Custom output type name'),
}).optional().describe('Output configuration'),
/** Transaction handling */
transaction: z.object({
enabled: z.boolean().default(true).describe('Use database transaction'),
isolationLevel: z.enum(['read_uncommitted', 'read_committed', 'repeatable_read', 'serializable']).optional().describe('Transaction isolation level'),
}).optional().describe('Transaction configuration'),
/** Authorization */
authRequired: z.boolean().default(true).describe('Require authentication'),
permissions: z.array(z.string()).optional().describe('Required permissions'),
/** Hooks */
hooks: z.object({
before: z.array(z.string()).optional().describe('Pre-mutation hooks'),
after: z.array(z.string()).optional().describe('Post-mutation hooks'),
}).optional().describe('Lifecycle hooks'),
});
export type GraphQLMutationConfig = z.infer<typeof GraphQLMutationConfigSchema>;
export type GraphQLMutationConfigInput = z.input<typeof GraphQLMutationConfigSchema>;
// ==========================================
// 4. Subscription Configuration
// ==========================================
/**
* GraphQL Subscription Configuration
*
* Configuration for real-time GraphQL subscriptions.
*/
export const GraphQLSubscriptionConfigSchema = z.object({
/** Subscription name */
name: z.string().describe('Subscription field name (camelCase recommended)'),
/** Source Object */
object: z.string().describe('Source ObjectQL object name'),
/** Subscription trigger events */
events: z.array(z.enum(['created', 'updated', 'deleted', 'custom'])).describe('Events to subscribe to'),
/** Description */
description: z.string().optional().describe('Subscription description'),
/** Filtering */
filter: z.object({
enabled: z.boolean().default(true).describe('Allow filtering subscriptions'),
fields: z.array(z.string()).optional().describe('Filterable fields'),
}).optional().describe('Subscription filtering'),
/** Payload configuration */
payload: z.object({
/** Include the modified entity */
includeEntity: z.boolean().default(true).describe('Include entity in payload'),
/** Include previous values (for updates) */
includePreviousValues: z.boolean().optional().default(false).describe('Include previous field values'),
/** Include mutation metadata */
includeMeta: z.boolean().optional().default(true).describe('Include metadata (timestamp, user, etc.)'),
}).optional().describe('Payload configuration'),
/** Authorization */
authRequired: z.boolean().default(true).describe('Require authentication'),
permissions: z.array(z.string()).optional().describe('Required permissions'),
/** Rate limiting for subscriptions */
rateLimit: z.object({
enabled: z.boolean().default(true).describe('Enable rate limiting'),
maxSubscriptionsPerUser: z.number().int().min(1).default(10).describe('Max concurrent subscriptions per user'),
throttleMs: z.number().int().min(0).optional().describe('Throttle interval in milliseconds'),
}).optional().describe('Subscription rate limiting'),
});
export type GraphQLSubscriptionConfig = z.infer<typeof GraphQLSubscriptionConfigSchema>;
export type GraphQLSubscriptionConfigInput = z.input<typeof GraphQLSubscriptionConfigSchema>;
// ==========================================
// 5. Resolver Configuration
// ==========================================
/**
* GraphQL Resolver Configuration
*
* Configuration for custom resolver logic.
*/
export const GraphQLResolverConfigSchema = z.object({
/** Field path (e.g., "Query.users", "Mutation.createUser") */
path: z.string().describe('Resolver path (Type.field)'),
/** Resolver implementation type */
type: z.enum(['datasource', 'computed', 'script', 'proxy']).describe('Resolver implementation type'),
/** Implementation details */
implementation: z.object({
/** For datasource type */
datasource: z.string().optional().describe('Datasource ID'),
query: z.string().optional().describe('Query/SQL to execute'),
/** For computed type */
expression: z.string().optional().describe('Computation expression'),
dependencies: z.array(z.string()).optional().describe('Dependent fields'),
/** For script type */
script: z.string().optional().describe('Script ID or inline code'),
/** For proxy type */
url: z.string().optional().describe('Proxy URL'),
method: z.enum(['GET', 'POST', 'PUT', 'DELETE']).optional().describe('HTTP method'),
}).optional().describe('Implementation configuration'),
/** Caching */
cache: z.object({
enabled: z.boolean().default(false).describe('Enable resolver caching'),
ttl: z.number().int().min(0).optional().describe('Cache TTL in seconds'),
keyArgs: z.array(z.string()).optional().describe('Arguments to include in cache key'),
}).optional().describe('Resolver caching'),
});
export type GraphQLResolverConfig = z.infer<typeof GraphQLResolverConfigSchema>;
export type GraphQLResolverConfigInput = z.input<typeof GraphQLResolverConfigSchema>;
// ==========================================
// 6. DataLoader Configuration
// ==========================================
/**
* GraphQL DataLoader Configuration
*
* Configuration for batching and caching with DataLoader pattern.
* Prevents N+1 query problems in GraphQL.
*/
export const GraphQLDataLoaderConfigSchema = z.object({
/** Loader name */
name: z.string().describe('DataLoader name'),
/** Source Object or datasource */
source: z.string().describe('Source object or datasource'),
/** Batch function configuration */
batchFunction: z.object({
/** Type of batch operation */
type: z.enum(['findByIds', 'query', 'script', 'custom']).describe('Batch function type'),
/** For findByIds */
keyField: z.string().optional().describe('Field to batch on (e.g., "id")'),
/** For query */
query: z.string().optional().describe('Query template'),
/** For script */
script: z.string().optional().describe('Script ID'),
/** Maximum batch size */
maxBatchSize: z.number().int().min(1).optional().default(100).describe('Maximum batch size'),
}).describe('Batch function configuration'),
/** Caching */
cache: z.object({
enabled: z.boolean().default(true).describe('Enable per-request caching'),
/** Cache key function */
keyFn: z.string().optional().describe('Custom cache key function'),
}).optional().describe('DataLoader caching'),
/** Options */
options: z.object({
/** Batch multiple requests in single tick */
batch: z.boolean().default(true).describe('Enable batching'),
/** Cache loaded values */
cache: z.boolean().default(true).describe('Enable caching'),
/** Maximum cache size */
maxCacheSize: z.number().int().min(0).optional().describe('Max cache entries'),
}).optional().describe('DataLoader options'),
});
export type GraphQLDataLoaderConfig = z.infer<typeof GraphQLDataLoaderConfigSchema>;
export type GraphQLDataLoaderConfigInput = z.input<typeof GraphQLDataLoaderConfigSchema>;
// ==========================================
// 7. GraphQL Directive Schema
// ==========================================
/**
* GraphQL Directive Location
*
* Where a directive can be used in the schema.
*/
export const GraphQLDirectiveLocation = z.enum([
// Executable Directive Locations
'QUERY',
'MUTATION',
'SUBSCRIPTION',
'FIELD',
'FRAGMENT_DEFINITION',
'FRAGMENT_SPREAD',
'INLINE_FRAGMENT',
'VARIABLE_DEFINITION',
// Type System Directive Locations
'SCHEMA',
'SCALAR',
'OBJECT',
'FIELD_DEFINITION',
'ARGUMENT_DEFINITION',
'INTERFACE',
'UNION',
'ENUM',
'ENUM_VALUE',
'INPUT_OBJECT',
'INPUT_FIELD_DEFINITION',
]);
export type GraphQLDirectiveLocation = z.infer<typeof GraphQLDirectiveLocation>;
/**
* GraphQL Directive Configuration
*
* Custom directives for schema metadata and behavior.
*/
export const GraphQLDirectiveConfigSchema = z.object({
/** Directive name */
name: z.string().regex(/^[a-z][a-zA-Z0-9]*$/).describe('Directive name (camelCase)'),
/** Description */
description: z.string().optional().describe('Directive description'),
/** Where directive can be used */
locations: z.array(GraphQLDirectiveLocation).describe('Directive locations'),
/** Arguments */
args: z.record(z.string(), z.object({
type: z.string().describe('Argument type'),
description: z.string().optional().describe('Argument description'),
defaultValue: z.unknown().optional().describe('Default value'),
})).optional().describe('Directive arguments'),
/** Is repeatable */
repeatable: z.boolean().optional().default(false).describe('Can be applied multiple times'),
/** Implementation */
implementation: z.object({
/** Directive behavior type */
type: z.enum(['auth', 'validation', 'transform', 'cache', 'deprecation', 'custom']).describe('Directive type'),
/** Handler function */
handler: z.string().optional().describe('Handler function name or script'),
}).optional().describe('Directive implementation'),
});
export type GraphQLDirectiveConfig = z.infer<typeof GraphQLDirectiveConfigSchema>;
export type GraphQLDirectiveConfigInput = z.input<typeof GraphQLDirectiveConfigSchema>;
// ==========================================
// 8. GraphQL Security - Query Depth Limiting
// ==========================================
/**
* Query Depth Limiting Configuration
*
* Prevents deeply nested queries that could cause performance issues.
*/
export const GraphQLQueryDepthLimitSchema = z.object({
/** Enable depth limiting */
enabled: z.boolean().default(true).describe('Enable query depth limiting'),
/** Maximum allowed depth */
maxDepth: z.number().int().min(1).default(10).describe('Maximum query depth'),
/** Fields to ignore in depth calculation */
ignoreFields: z.array(z.string()).optional().describe('Fields excluded from depth calculation'),
/** Callback on depth exceeded */
onDepthExceeded: z.enum(['reject', 'log', 'warn']).default('reject').describe('Action when depth exceeded'),
/** Custom error message */
errorMessage: z.string().optional().describe('Custom error message for depth violations'),
});
export type GraphQLQueryDepthLimit = z.infer<typeof GraphQLQueryDepthLimitSchema>;
export type GraphQLQueryDepthLimitInput = z.input<typeof GraphQLQueryDepthLimitSchema>;
// ==========================================
// 9. GraphQL Security - Query Complexity
// ==========================================
/**
* Query Complexity Calculation Configuration
*
* Assigns complexity scores to fields and limits total query complexity.
* Prevents expensive queries from overloading the server.
*/
export const GraphQLQueryComplexitySchema = z.object({
/** Enable complexity limiting */
enabled: z.boolean().default(true).describe('Enable query complexity limiting'),
/** Maximum allowed complexity score */
maxComplexity: z.number().int().min(1).default(1000).describe('Maximum query complexity'),
/** Default field complexity */
defaultFieldComplexity: z.number().int().min(0).default(1).describe('Default complexity per field'),
/** Field-specific complexity scores */
fieldComplexity: z.record(z.string(), z.union([
z.number().int().min(0),
z.object({
/** Base complexity */
base: z.number().int().min(0).describe('Base complexity'),
/** Multiplier based on arguments */
multiplier: z.string().optional().describe('Argument multiplier (e.g., "limit")'),
/** Custom complexity calculation */
calculator: z.string().optional().describe('Custom calculator function'),
}),
])).optional().describe('Per-field complexity configuration'),
/** List multiplier */
listMultiplier: z.number().min(0).default(10).describe('Multiplier for list fields'),
/** Callback on complexity exceeded */
onComplexityExceeded: z.enum(['reject', 'log', 'warn']).default('reject').describe('Action when complexity exceeded'),
/** Custom error message */
errorMessage: z.string().optional().describe('Custom error message for complexity violations'),
});
export type GraphQLQueryComplexity = z.infer<typeof GraphQLQueryComplexitySchema>;
export type GraphQLQueryComplexityInput = z.input<typeof GraphQLQueryComplexitySchema>;
// ==========================================
// 10. GraphQL Security - Rate Limiting
// ==========================================
/**
* GraphQL Rate Limiting Configuration
*
* Rate limiting for GraphQL operations.
*/
export const GraphQLRateLimitSchema = z.object({
/** Enable rate limiting */
enabled: z.boolean().default(true).describe('Enable rate limiting'),
/** Rate limit strategy */
strategy: z.enum(['token_bucket', 'fixed_window', 'sliding_window', 'cost_based']).default('token_bucket').describe('Rate limiting strategy'),
/** Global rate limits */
global: z.object({
/** Requests per time window */
maxRequests: z.number().int().min(1).default(1000).describe('Maximum requests per window'),
/** Time window in milliseconds */
windowMs: z.number().int().min(1000).default(60000).describe('Time window in milliseconds'),
}).optional().describe('Global rate limits'),
/** Per-user rate limits */
perUser: z.object({
/** Requests per time window */
maxRequests: z.number().int().min(1).default(100).describe('Maximum requests per user per window'),
/** Time window in milliseconds */
windowMs: z.number().int().min(1000).default(60000).describe('Time window in milliseconds'),
}).optional().describe('Per-user rate limits'),
/** Cost-based rate limiting */
costBased: z.object({
/** Enable cost-based limiting */
enabled: z.boolean().default(false).describe('Enable cost-based rate limiting'),
/** Maximum cost per time window */
maxCost: z.number().int().min(1).default(10000).describe('Maximum cost per window'),
/** Time window in milliseconds */
windowMs: z.number().int().min(1000).default(60000).describe('Time window in milliseconds'),
/** Use complexity as cost */
useComplexityAsCost: z.boolean().default(true).describe('Use query complexity as cost'),
}).optional().describe('Cost-based rate limiting'),
/** Operation-specific limits */
operations: z.record(z.string(), z.object({
maxRequests: z.number().int().min(1).describe('Max requests for this operation'),
windowMs: z.number().int().min(1000).describe('Time window'),
})).optional().describe('Per-operation rate limits'),
/** Callback on limit exceeded */
onLimitExceeded: z.enum(['reject', 'queue', 'log']).default('reject').describe('Action when rate limit exceeded'),
/** Custom error message */
errorMessage: z.string().optional().describe('Custom error message for rate limit violations'),
/** Headers to include in response */
includeHeaders: z.boolean().default(true).describe('Include rate limit headers in response'),
});
export type GraphQLRateLimit = z.infer<typeof GraphQLRateLimitSchema>;
export type GraphQLRateLimitInput = z.input<typeof GraphQLRateLimitSchema>;
// ==========================================
// 11. GraphQL Security - Persisted Queries
// ==========================================
/**
* Persisted Queries Configuration
*
* Only allow pre-registered queries to execute (allow list approach).
* Improves security and performance.
*/
export const GraphQLPersistedQuerySchema = z.object({
/** Enable persisted queries */
enabled: z.boolean().default(false).describe('Enable persisted queries'),
/** Enforcement mode */
mode: z.enum(['optional', 'required']).default('optional').describe('Persisted query mode (optional: allow both, required: only persisted)'),
/** Query store configuration */
store: z.object({
/** Store type */
type: z.enum(['memory', 'redis', 'database', 'file']).default('memory').describe('Query store type'),
/** Store connection string */
connection: z.string().optional().describe('Store connection string or path'),
/** TTL for cached queries */
ttl: z.number().int().min(0).optional().describe('TTL in seconds for stored queries'),
}).optional().describe('Query store configuration'),
/** Automatic Persisted Queries (APQ) */
apq: z.object({
/** Enable APQ */
enabled: z.boolean().default(true).describe('Enable Automatic Persisted Queries'),
/** Hash algorithm */
hashAlgorithm: z.enum(['sha256', 'sha1', 'md5']).default('sha256').describe('Hash algorithm for query IDs'),
/** Cache control */
cache: z.object({
/** Cache TTL */
ttl: z.number().int().min(0).default(3600).describe('Cache TTL in seconds'),
/** Max cache size */
maxSize: z.number().int().min(1).optional().describe('Maximum number of cached queries'),
}).optional().describe('APQ cache configuration'),
}).optional().describe('Automatic Persisted Queries configuration'),
/** Query allow list */
allowlist: z.object({
/** Enable allow list mode */
enabled: z.boolean().default(false).describe('Enable query allow list (reject queries not in list)'),
/** Allowed query IDs */
queries: z.array(z.object({
id: z.string().describe('Query ID or hash'),
operation: z.string().optional().describe('Operation name'),
query: z.string().optional().describe('Query string'),
})).optional().describe('Allowed queries'),
/** External allow list source */
source: z.string().optional().describe('External allow list source (file path or URL)'),
}).optional().describe('Query allow list configuration'),
/** Security */
security: z.object({
/** Maximum query size */
maxQuerySize: z.number().int().min(1).optional().describe('Maximum query string size in bytes'),
/** Reject introspection in production */
rejectIntrospection: z.boolean().default(false).describe('Reject introspection queries'),
}).optional().describe('Security configuration'),
});
export type GraphQLPersistedQuery = z.infer<typeof GraphQLPersistedQuerySchema>;
export type GraphQLPersistedQueryInput = z.input<typeof GraphQLPersistedQuerySchema>;
// ==========================================
// 12. GraphQL Federation
// ==========================================
/**
* Federation Entity Key Definition
*
* Defines how entities are uniquely identified across subgraphs.
* Corresponds to the `@key` directive in Apollo Federation.
*
* @see https://www.apollographql.com/docs/federation/entities
*/
export const FederationEntityKeySchema = z.object({
/** Fields composing the key (e.g., "id" or "sku packageId") */
fields: z.string().describe('Selection set of fields composing the entity key'),
/** Whether this key can be used for resolution across subgraphs */
resolvable: z.boolean().optional().default(true).describe('Whether entities can be resolved from this subgraph'),
});
export type FederationEntityKey = z.infer<typeof FederationEntityKeySchema>;
/**
* Federation External Field
*
* Marks a field as owned by another subgraph (`@external`).
*/
export const FederationExternalFieldSchema = z.object({
/** Field name */
field: z.string().describe('Field name marked as external'),
/** The subgraph that owns this field */
ownerSubgraph: z.string().optional().describe('Subgraph that owns this field'),
});
export type FederationExternalField = z.infer<typeof FederationExternalFieldSchema>;
/**
* Federation Requires Directive
*
* Specifies fields that must be fetched from other subgraphs
* before resolving a computed field.
* Corresponds to the `@requires` directive in Apollo Federation.
*/
export const FederationRequiresSchema = z.object({
/** The field that has this requirement */
field: z.string().describe('Field with the requirement'),
/** Selection set of external fields required for resolution */
fields: z.string().describe('Selection set of required fields (e.g., "price weight")'),
});
export type FederationRequires = z.infer<typeof FederationRequiresSchema>;
/**
* Federation Provides Directive
*
* Indicates that a field resolution provides additional fields
* of a returned entity type.
* Corresponds to the `@provides` directive in Apollo Federation.
*/
export const FederationProvidesSchema = z.object({
/** The field that provides additional data */
field: z.string().describe('Field that provides additional entity fields'),
/** Selection set of fields provided during resolution */
fields: z.string().describe('Selection set of provided fields (e.g., "name price")'),
});
export type FederationProvides = z.infer<typeof FederationProvidesSchema>;
/**
* Federation Entity Configuration
*
* Configures a type as a federated entity that can be referenced
* and extended across multiple subgraphs.
*/
export const FederationEntitySchema = z.object({
/** Type/Object name */
typeName: z.string().describe('GraphQL type name for this entity'),
/** Entity keys (`@key` directive) */
keys: z.array(FederationEntityKeySchema).min(1).describe('Entity key definitions'),
/** External fields (`@external`) */
externalFields: z.array(FederationExternalFieldSchema).optional().describe('Fields owned by other subgraphs'),
/** Requires directives (`@requires`) */
requires: z.array(FederationRequiresSchema).optional().describe('Required external fields for computed fields'),
/** Provides directives (`@provides`) */
provides: z.array(FederationProvidesSchema).optional().describe('Fields provided during resolution'),
/** Whether this subgraph owns this entity */
owner: z.boolean().optional().default(false).describe('Whether this subgraph is the owner of this entity'),
});
export type FederationEntity = z.infer<typeof FederationEntitySchema>;
/**
* Subgraph Configuration
*
* Configuration for an individual subgraph in a federated architecture.
*/
export const SubgraphConfigSchema = z.object({
/** Subgraph name */
name: z.string().describe('Unique subgraph identifier'),
/** Subgraph URL */
url: z.string().describe('Subgraph endpoint URL'),
/** Schema source */
schemaSource: z.enum(['introspection', 'file', 'registry']).default('introspection').describe('How to obtain the subgraph schema'),
/** Schema file path (when schemaSource is "file") */
schemaPath: z.string().optional().describe('Path to schema file (SDL format)'),
/** Federated entities defined by this subgraph */
entities: z.array(FederationEntitySchema).optional().describe('Entity definitions for this subgraph'),
/** Health check endpoint */
healthCheck: z.object({
enabled: z.boolean().default(true).describe('Enable health checking'),
path: z.string().default('/health').describe('Health check endpoint path'),
intervalMs: z.number().int().min(1000).default(30000).describe('Health check interval in milliseconds'),
}).optional().describe('Subgraph health check configuration'),
/** Request headers to forward */
forwardHeaders: z.array(z.string()).optional().describe('HTTP headers to forward to this subgraph'),
});
export type SubgraphConfig = z.infer<typeof SubgraphConfigSchema>;
export type SubgraphConfigInput = z.input<typeof SubgraphConfigSchema>;
/**
* Federation Gateway Configuration
*
* Root-level gateway configuration for Apollo Federation or similar.
* Manages query planning, routing, and composition across subgraphs.
*/
export const FederationGatewaySchema = z.object({
/** Enable federation mode */
enabled: z.boolean().default(false).describe('Enable GraphQL Federation gateway mode'),
/** Federation specification version */
version: z.enum(['v1', 'v2']).default('v2').describe('Federation specification version'),
/** Registered subgraphs */
subgraphs: z.array(SubgraphConfigSchema).describe('Subgraph configurations'),
/** Service discovery */
serviceDiscovery: z.object({
/** Discovery mode */
type: z.enum(['static', 'dns', 'consul', 'kubernetes']).default('static').describe('Service discovery method'),
/** Poll interval for dynamic discovery */
pollIntervalMs: z.number().int().min(1000).optional().describe('Discovery poll interval in milliseconds'),
/** Kubernetes namespace (when type is "kubernetes") */
namespace: z.string().optional().describe('Kubernetes namespace for subgraph discovery'),
}).optional().describe('Service discovery configuration'),
/** Query planning */
queryPlanning: z.object({
/** Execution strategy */
strategy: z.enum(['parallel', 'sequential', 'adaptive']).default('parallel').describe('Query execution strategy across subgraphs'),
/** Maximum query depth across subgraphs */
maxDepth: z.number().int().min(1).optional().describe('Max query depth in federated execution'),
/** Dry-run mode for debugging query plans */
dryRun: z.boolean().optional().default(false).describe('Log query plans without executing'),
}).optional().describe('Query planning configuration'),
/** Schema composition settings */
composition: z.object({
/** How schema conflicts are resolved */
conflictResolution: z.enum(['error', 'first_wins', 'last_wins']).default('error').describe('Strategy for resolving schema conflicts'),
/** Whether to validate composed schema */
validate: z.boolean().default(true).describe('Validate composed supergraph schema'),
}).optional().describe('Schema composition configuration'),
/** Gateway-level error handling */
errorHandling: z.object({
/** Whether to include subgraph names in errors */
includeSubgraphName: z.boolean().default(false).describe('Include subgraph name in error responses'),
/** Partial error behavior */
partialErrors: z.enum(['propagate', 'nullify', 'reject']).default('propagate').describe('Behavior when a subgraph returns partial errors'),
}).optional().describe('Error handling configuration'),
});
export type FederationGateway = z.infer<typeof FederationGatewaySchema>;
export type FederationGatewayInput = z.input<typeof FederationGatewaySchema>;
// ==========================================
// 13. Complete GraphQL Configuration
// ==========================================
/**
* Complete GraphQL Configuration
*
* Root configuration for GraphQL API generation and security.
*/
export const GraphQLConfigSchema = z.object({
/** Enable GraphQL API */
enabled: z.boolean().default(true).describe('Enable GraphQL API'),
/** GraphQL endpoint path */
path: z.string().default('/graphql').describe('GraphQL endpoint path'),
/** GraphQL Playground */
playground: z.object({
enabled: z.boolean().default(true).describe('Enable GraphQL Playground'),
path: z.string().default('/playground').describe('Playground path'),
}).optional().describe('GraphQL Playground configuration'),
/** Schema generation */
schema: z.object({
/** Auto-generate types from Objects */
autoGenerateTypes: z.boolean().default(true).describe('Auto-generate types from Objects'),
/** Type configurations */
types: z.array(GraphQLTypeConfigSchema).optional().describe('Type configurations'),