-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.ts
More file actions
733 lines (609 loc) · 20.4 KB
/
index.ts
File metadata and controls
733 lines (609 loc) · 20.4 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
import type { GraphQLResolveInfo } from 'graphql'
import { TSEnumType, UnionToIntersection, getEnumProperties, ObjectToUnion, ExpandRecursively, MaybePromise, MaybeFunction } from './utils'
import { buildSchema, printSchema } from './schema'
type GraphQLRootType = 'Query' | 'Mutation' | 'Subscription'
type GarphType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID' | 'ObjectType' | 'InterfaceType' | 'InputType' | 'Scalar' | 'Enum' | 'List' | 'PaginatedList' | 'Union' | 'Ref' | 'Optional' | 'Args' | 'OmitResolver'
export abstract class Type<T, X extends GarphType> {
_name?: string
_is: X
_inner?: any
_output?: any
_args?: Args
_shape: T
typeDef: TypeDefinition<T>
description(text: string) {
this.typeDef.description = text
return this
}
deprecated(reason: string) {
this.typeDef.deprecated = reason
return this
}
}
export type TypeDefinition<T> = {
name?: string
type: GarphType
shape?: T
args?: Args
description?: string
isOptional?: boolean
isRequired?: boolean
deprecated?: string
scalarOptions?: ScalarOptions<any, any>
defaultValue?: any
interfaces?: AnyInterface[]
extend?: AnyTypes[]
}
export type AnyType = Type<any, any>
export type AnyString = Type<string, 'String'>
export type AnyID = Type<string, 'ID'>
export type AnyBoolean = Type<boolean, 'Boolean'>
export type AnyNumber = Type<number, any>
export type AnyInt = Type<number, 'Int'>
export type AnyFloat = Type<number, 'Float'>
export type AnyRef = Type<any, 'Ref'>
export type AnyList = Type<any, 'List'>
export type AnyPaginatedList = Type<any, 'PaginatedList'>
export type AnyUnion = Type<any, 'Union'>
export type AnyEnum = Type<any, 'Enum'>
export type AnyScalar = Type<any, 'Scalar'>
export type AnyInput = Type<any, 'InputType'>
export type AnyInterface = Type<any, 'InterfaceType'>
export type AnyArgs = Type<any, 'Args'>
export type AnyOptional = Type<any, 'Optional'>
export type AnyObject = Type<any, 'ObjectType'>
export type AnyOmitResolver = Type<any, 'OmitResolver'>
export type Args = {
[key: string]: AnyType
}
export type AnyTypes = {
[key: string]: AnyType
}
export type AnyObjects = {
[key: string]: AnyObject
}
type ScalarOptions<I, O> = {
serialize: (value: I) => O
parseValue: (value: O) => I
parseLiteral?: (ast: any) => I
specifiedByUrl?: string
}
type InferResolverConfig = {
context?: any
}
type InferOptions = {
omitResolver?: AnyOmitResolver | never
}
type RefType = () => AnyType
// TODO: Refactor Args to get rid of this mess
export type Infer<T, options extends InferOptions = { omitResolver: never }> = ExpandRecursively<InferRaw<T, options>>
export type InferRaw<T, options extends InferOptions = { omitResolver: never }> = T extends AnyInput | AnyObject | AnyInterface ? {
__typename?: T['_name']
} & {
[K in keyof T['_shape'] as T['_shape'][K] extends AnyOptional | options['omitResolver'] ? never :
T['_shape'][K] extends AnyArgs ?
T['_shape'][K]['_shape'] extends AnyOptional | options['omitResolver'] ? never : K :
K]: InferRaw<T['_shape'][K], options>
} & {
[K in keyof T['_shape'] as T['_shape'][K] extends AnyOptional | options['omitResolver'] ? K :
T['_shape'][K] extends AnyArgs ?
T['_shape'][K]['_shape'] extends AnyOptional | options['omitResolver'] ? K : never :
never]?: InferRaw<T['_shape'][K], options>
} : InferShallow<T, options>
export type InferShallow<T, options extends InferOptions = { omitResolver: never }> =
T extends AnyString | AnyID | AnyScalar | AnyNumber | AnyBoolean ? T['_shape'] :
T extends AnyEnum ? T['_inner'] :
T extends AnyUnion ? InferRaw<ObjectToUnion<T['_inner']>, options> :
T extends AnyList ? InferRaw<T['_shape'], options>[] :
T extends AnyPaginatedList ? T['_inner'] :
T extends AnyOptional ? InferRaw<T['_shape'], options> | null | undefined :
T extends AnyOmitResolver ? InferRaw<T['_shape'], options> :
T extends AnyArgs ? InferRaw<T['_shape'], options> :
T extends AnyRef ? InferRaw<T['_inner'], options> :
T
export type InferArgs<T extends AnyType> = ExpandRecursively<InferArgsRaw<T>>
export type InferArgsRaw<T extends AnyType> = T extends AnyObject | AnyInterface ? {
[K in keyof T['_shape']]: InferArgRaw<T['_shape'][K]>
} : never
export type InferArg<T> = ExpandRecursively<InferArgRaw<T>>
export type InferArgRaw<T> = T extends AnyArgs ? {
[K in keyof T['_args'] as T['_args'][K] extends AnyOptional ? never : K]: InferRaw<T['_args'][K]>
} & {
[K in keyof T['_args'] as T['_args'][K] extends AnyOptional ? K : never]?: InferRaw<T['_args'][K]>
}: never
export type InferUnionNames<T> = T extends AnyUnion ? ObjectToUnion<T['_inner']>['_name'] : never
export type InferResolvers<T extends AnyTypes, X extends InferResolverConfig> = {
[K in keyof T]: K extends 'Subscription' ? {
[G in keyof T[K]['_shape']]?: {
subscribe: (parent: {}, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<AsyncIterator<{
[key in G]: Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>
}>>
resolve?: (value: Infer<T[K]['_shape'][G]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>>
}
} : {
[G in keyof T[K]['_shape']]?: (parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<MaybeFunction<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>>> | AsyncGenerator<Infer<T[K]['_shape'][G]['_shape'], { omitResolver: AnyOmitResolver }>>
} | {
[G in keyof T[K]['_shape']]?: {
resolve: (parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>> | AsyncGenerator<Infer<T[K]['_shape'][G]['_shape'], { omitResolver: AnyOmitResolver }>>
} | {
load: (queries: { parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo } []) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>[]>
} | {
loadBatch: (queries: { parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo } []) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>[]>
}
} & {
__isTypeOf?: (parent: Infer<T[K]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<boolean>
__resolveType?: (parent: Infer<T[K]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<InferUnionNames<T[K]>>
}
}
export type InferResolversStrict<T extends AnyTypes, X extends InferResolverConfig> = {
[K in keyof T]: K extends 'Subscription' ? {
[G in keyof T[K]['_shape']]: {
subscribe: (parent: {}, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<AsyncIterator<{ [G in keyof T[K]['_shape']]: Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }> }>>
resolve?: (value: Infer<T[K]['_shape'][G]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>>
}
} : {
[G in keyof T[K]['_shape']]: (parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<MaybeFunction<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>>> | AsyncGenerator<Infer<T[K]['_shape'][G]['_shape'], { omitResolver: AnyOmitResolver }>>
} | {
[G in keyof T[K]['_shape']]: {
resolve: (parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>> | AsyncGenerator<Infer<T[K]['_shape'][G]['_shape'], { omitResolver: AnyOmitResolver }>>
} | {
load: (queries: { parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo } []) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>>[]
} | {
loadBatch: (queries: { parent: K extends GraphQLRootType ? {} : Infer<T[K]>, args: InferArg<T[K]['_shape'][G]>, context: X['context'], info: GraphQLResolveInfo } []) => MaybePromise<Infer<T[K]['_shape'][G], { omitResolver: AnyOmitResolver }>>[]
}
} & {
__isTypeOf?: (parent: Infer<T[K]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<boolean>
__resolveType?: (parent: Infer<T[K]>, context: X['context'], info: GraphQLResolveInfo) => MaybePromise<InferUnionNames<T[K]>>
}
}
class GType<N extends string, T extends AnyTypes> extends Type<T, 'ObjectType'> {
declare _name: N
constructor(name: string, shape: T, interfaces?: AnyInterface[], extend?: AnyTypes[]) {
super()
this.typeDef = {
name,
type: 'ObjectType',
shape,
interfaces,
extend
}
}
implements<D extends AnyInterface>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.interfaces = Array.isArray(ref) ? ref : [ref]
return new GType<N, T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref], this.typeDef.extend)
}
extend<D extends AnyTypes>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
return new GType<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, this.typeDef.interfaces, Array.isArray(ref) ? ref : [ref])
}
}
class GInput<N extends string, T extends AnyTypes> extends Type<T, 'InputType'> {
declare _name: N
constructor(name: string, shape: T, extend?: AnyTypes[]) {
super()
this.typeDef = {
name,
type: 'InputType',
shape,
extend
}
}
extend<D extends AnyTypes>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
return new GInput<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref])
}
}
class GInterface<N extends string, T extends AnyTypes> extends Type<T, 'InterfaceType'> {
declare _name: N
constructor(name: string, shape: T, interfaces?: AnyInterface[], extend?: AnyTypes[]) {
super()
this.typeDef = {
name,
type: 'InterfaceType',
shape,
interfaces,
extend
}
}
implements<D extends AnyInterface>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.interfaces = Array.isArray(ref) ? ref : [ref]
return new GInterface<N ,T & UnionToIntersection<D['_shape']>>(this.typeDef.name, this.typeDef.shape as any, Array.isArray(ref) ? ref : [ref], this.typeDef.extend)
}
extend<D extends AnyTypes>(ref: D | D[]) {
// This is temporary construct, until we figure out how to properly manage to shared schema
this.typeDef.extend = Array.isArray(ref) ? ref : [ref]
return new GInterface<N, T & UnionToIntersection<D>>(this.typeDef.name, this.typeDef.shape as any, this.typeDef.interfaces, Array.isArray(ref) ? ref : [ref])
}
}
class GString<T extends GarphType> extends Type<string, T> {
constructor(type: 'String' | 'ID' = 'String') {
super()
this.typeDef = {
type
}
}
optional() {
return new GOptional<this>(this)
}
required() {
this.typeDef.isRequired = true
return this
}
list() {
return new GList<this>(this)
}
default(value: string) {
this.typeDef.defaultValue = value
return this
}
omitResolver () {
return new GOmitResolver<this>(this)
}
args<X extends Args>(args: X) {
return new GArgs<this, X>(this, args)
}
}
class GNumber<T extends GarphType> extends Type<number, T> {
constructor(type: 'Int' | 'Float' = 'Int') {
super()
this.typeDef = {
type
}
}
optional() {
return new GOptional<this>(this)
}
required() {
this.typeDef.isRequired = true
return this
}
list() {
return new GList<this>(this)
}
default(value: number) {
this.typeDef.defaultValue = value
return this
}
omitResolver () {
return new GOmitResolver<this>(this)
}
args<X extends Args>(args: X) {
return new GArgs<this, X>(this, args)
}
}
class GBoolean extends Type<boolean, 'Boolean'> {
constructor() {
super()
this.typeDef = {
type: 'Boolean'
}
}
optional() {
return new GOptional<this>(this)
}
required() {
this.typeDef.isRequired = true
return this
}
list() {
return new GList<this>(this)
}
default(value: boolean) {
this.typeDef.defaultValue = value
return this
}
omitResolver () {
return new GOmitResolver<this>(this)
}
args<X extends Args>(args: X) {
return new GArgs<this, X>(this, args)
}
}
class GEnum<N extends string, T extends readonly string[] | TSEnumType> extends Type<readonly string[], 'Enum'> {
declare _name: N
declare _inner: T extends readonly string[] ? T[number] : keyof T
constructor(name: string, shape: T) {
super()
let enumShape: readonly string[]
if (Array.isArray(shape)) {
enumShape = shape
} else {
enumShape = getEnumProperties(shape as TSEnumType)
}
this.typeDef = {
name,
type: 'Enum',
shape: enumShape
}
}
}
class GUnion<N extends string, T extends AnyObjects> extends Type<T, 'Union'> {
declare _name: N
declare _inner: T
constructor(name: string, shape: T) {
super()
this.typeDef = {
name,
type: 'Union',
shape
}
}
}
class GRef<T> extends Type<T, 'Ref'> {
declare _inner: T extends RefType ? ReturnType<T> : T
constructor(ref: T) {
super()
this.typeDef = {
shape: ref,
type: 'Ref'
}
}
optional() {
return new GOptional<this>(this)
}
required() {
this.typeDef.isRequired = true
return this
}
list() {
return new GList<this>(this)
}
paginatedList() {
return new GPaginatedList<this>(this)
}
default(value: InferRaw<this['_inner']>) {
this.typeDef.defaultValue = value
return this
}
omitResolver () {
return new GOmitResolver<this>(this)
}
args<X extends Args>(x: X) {
return new GArgs<this, X>(this, x)
}
}
class GScalar<I, O> extends Type<I, 'Scalar'> {
declare _output: O
constructor(name: string, scalarOptions?: ScalarOptions<I, O>) {
super()
this.typeDef = {
name,
type: 'Scalar',
scalarOptions
}
}
specifiedByUrl(url: string) {
this.typeDef.scalarOptions.specifiedByUrl = url
return this
}
}
class GList<T extends AnyType> extends Type<T, 'List'> {
constructor(shape: T) {
super()
this.typeDef = {
type: 'List',
shape: shape
}
}
optional() {
return new GOptional<this>(this)
}
required() {
this.typeDef.isRequired = true
return this
}
default(value: typeof this._inner) {
this.typeDef.defaultValue = value
return this
}
omitResolver () {
return new GOmitResolver<this>(this)
}
args<X extends Args>(args: X) {
return new GArgs<this, X>(this, args)
}
list() {
return new GList<this>(this)
}
}
class GPaginatedList<T extends AnyType> extends Type<T, 'PaginatedList'> {
declare _inner: {
edges: {
node: InferRaw<T>
cursor: string
}[]
pageInfo: {
hasNextPage: boolean,
hasPreviousPage: boolean,
startCursor?: string,
endCursor?: string
}
}
constructor(shape: T) {
super()
this.typeDef = {
type: 'PaginatedList',
shape: shape
}
}
optional() {
return new GOptional<this>(this)
}
required() {
this.typeDef.isRequired = true
return this
}
omitResolver () {
return new GOmitResolver<this>(this)
}
args<X extends Args>(args: X) {
return new GArgs<this, X>(this, args)
}
// list() {
// return new GList<this>(this)
// }
}
class GOptional<T extends AnyType> extends Type<T, 'Optional'> {
constructor(shape: T) {
super()
this.typeDef = shape.typeDef
this.typeDef.isOptional = true
this.typeDef.isRequired = false
}
list() {
return new GList<this>(this)
}
default(value: InferRaw<T>) {
this.typeDef.defaultValue = value
return this
}
omitResolver () {
return new GOmitResolver<this>(this)
}
args<X extends Args>(args: X) {
return new GArgs<this, X>(this, args)
}
}
class GOmitResolver<T extends AnyType> extends Type<T, 'OmitResolver'> {
constructor(shape: T) {
super()
this.typeDef = shape.typeDef
}
optional() {
return new GOptional<this>(this)
}
required() {
this.typeDef.isRequired = true
return this
}
default(value: InferRaw<T>) {
this.typeDef.defaultValue = value
return this
}
args<X extends Args>(args: X) {
return new GArgs<this, X>(this, args)
}
list() {
return new GList<this>(this)
}
}
class GArgs<T extends AnyType, X extends Args> extends Type<T, 'Args'> {
declare _args: X
constructor(shape: T, args: X) {
super()
this.typeDef = shape.typeDef
this.typeDef.args = args
}
}
export class GarphSchema {
types: Map<string, AnyType> = new Map()
nodeType = this.interface('Node', {
id: this.id()
})
pageInfoType = this.type('PageInfo', {
hasNextPage: this.boolean(),
hasPreviousPage: this.boolean(),
startCursor: this.string().optional(),
endCursor: this.string().optional()
})
pageInfoArgs = {
first: this.int().optional(),
last: this.int().optional(),
before: this.id().optional(),
after: this.id().optional()
}
registerType(type: AnyType) {
const name = type.typeDef.name
if (!['Node', 'PageInfo'].includes(name) && this.types.has(name)) throw new Error(`Type with name "${name}" already exists`)
this.types.set(name, type)
}
constructor ({ types }: { types: AnyType[] } = { types: [] }) {
types.forEach(t => this.registerType(t))
}
type<N extends string, T extends AnyTypes>(name: N, shape: T) {
const t = new GType<N, T>(name, shape)
this.registerType(t)
return t
}
node<N extends string, T extends AnyTypes>(name: N, shape: T) {
const t = new GType<N, T>(name, shape).implements(this.nodeType)
this.registerType(t)
return t
}
connection<N extends string, T extends AnyRef>(name: N, shape: T) {
const t = new GType(name, {
edges: new GList(shape),
pageInfo: this.pageInfoType
})
this.registerType(t)
return t
}
edge<N extends string, T extends AnyRef>(name: N, shape: T) {
const t = new GType<N, {
node: T
cursor: AnyString
}>(name, {
node: shape,
cursor: g.string()
})
this.registerType(t)
return t
}
inputType<N extends string, T extends AnyTypes>(name: N, shape: T) {
const t = new GInput<N, T>(name, shape)
this.registerType(t)
return t
}
enumType<N extends string, T extends readonly string[] | TSEnumType>(name: N, args: T) {
const t = new GEnum<N, T>(name, args)
this.registerType(t)
return t
}
unionType<N extends string, T extends AnyObjects>(name: N, args: T) {
const t = new GUnion<N, T>(name, args)
this.registerType(t)
return t
}
scalarType<I, O>(name: string, options?: ScalarOptions<I, O>) {
const t = new GScalar<I, O>(name, options)
this.registerType(t)
return t
}
interface<N extends string, T extends AnyTypes>(name: N, shape: T) {
const t = new GInterface<N, T>(name, shape)
this.registerType(t)
return t
}
string() {
return new GString<'String'>()
}
id() {
return new GString<'ID'>('ID')
}
int() {
return new GNumber<'Int'>('Int')
}
float() {
return new GNumber<'Float'>('Float')
}
boolean() {
return new GBoolean()
}
// The generic has to be any, because constraints cannot be applied to recursive types
// This is a limitation of TypeScript
ref<T>(ref: T) {
return new GRef<T>(ref)
}
// enum<T extends string>(args: T[]) {
// return new GEnum<T>('', args)
// }
// union<T extends AnyType>(args: T[]) {
// return new GUnion<T>('', args)
// }
}
export const g = new GarphSchema()
export { buildSchema, printSchema }