@@ -5,14 +5,15 @@ import { buildSchema, printSchema } from './schema'
5
5
type GraphQLRootType = 'Query' | 'Mutation' | 'Subscription'
6
6
type GarphType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID' | 'ObjectType' | 'InterfaceType' | 'InputType' | 'Scalar' | 'Enum' | 'List' | 'PaginatedList' | 'Union' | 'Ref' | 'Internal' | 'Optional' | 'Args' | 'OmitResolver'
7
7
8
- export abstract class Type < T , X extends GarphType > {
8
+ export abstract class Type < TShape , TId extends GarphType , TInternal extends boolean = false > {
9
9
_name ?: string
10
- _is : X
10
+ _is : TId
11
11
_inner ?: any
12
12
_output ?: any
13
13
_args ?: Args
14
- _shape : T
15
- typeDef : TypeDefinition < T >
14
+ _shape : TShape
15
+ _internal : TInternal
16
+ typeDef : TypeDefinition < TShape >
16
17
17
18
description ( text : string ) {
18
19
this . typeDef . description = text
@@ -25,6 +26,7 @@ export abstract class Type<T, X extends GarphType> {
25
26
}
26
27
}
27
28
29
+
28
30
export type TypeDefinition < T > = {
29
31
name ?: string
30
32
type : GarphType
@@ -37,18 +39,19 @@ export type TypeDefinition<T> = {
37
39
scalarOptions ?: ScalarOptions < any , any >
38
40
defaultValue ?: any
39
41
interfaces ?: AnyInterface [ ]
40
- extend ?: AnyTypes [ ]
42
+ extend ?: AnyExposedTypes [ ]
41
43
}
42
44
43
- export type AnyType = Type < any , any >
45
+ export type AnyType < TInternal extends boolean = boolean > = Type < any , any , TInternal >
46
+ export type AnyExposedType = AnyType < false >
44
47
export type AnyString = Type < string , 'String' >
45
48
export type AnyID = Type < string , 'ID' >
46
49
export type AnyBoolean = Type < boolean , 'Boolean' >
47
50
export type AnyNumber = Type < number , any >
48
51
export type AnyInt = Type < number , 'Int' >
49
52
export type AnyFloat = Type < number , 'Float' >
50
53
export type AnyRef = Type < any , 'Ref' >
51
- export type AnyInternal = Type < any , 'Internal' >
54
+ export type AnyInternal = Type < any , 'Internal' , true >
52
55
export type AnyList = Type < any , 'List' >
53
56
export type AnyPaginatedList = Type < any , 'PaginatedList' >
54
57
export type AnyUnion = Type < any , 'Union' >
@@ -62,13 +65,15 @@ export type AnyObject = Type<any, 'ObjectType'>
62
65
export type AnyOmitResolver = Type < any , 'OmitResolver' >
63
66
64
67
export type Args = {
65
- [ key : string ] : AnyType
68
+ [ key : string ] : AnyExposedType
66
69
}
67
70
68
- export type AnyTypes = {
69
- [ key : string ] : AnyType
71
+ export type AnyTypes < TInternal extends boolean = boolean > = {
72
+ [ key : string ] : AnyType < TInternal >
70
73
}
71
74
75
+ export type AnyExposedTypes = AnyTypes < false >
76
+
72
77
export type AnyObjects = {
73
78
[ key : string ] : AnyObject
74
79
}
@@ -88,10 +93,11 @@ type InferOptions = {
88
93
omitResolver ?: AnyOmitResolver | never
89
94
}
90
95
91
- type RefType = ( ) => AnyType
96
+ type RefType = ( ) => AnyExposedType
92
97
93
98
// TODO: Refactor Args to get rid of this mess
94
99
export type Infer < T , options extends InferOptions = { omitResolver : never } > = ExpandRecursively < InferRaw < T , options > >
100
+
95
101
export type InferRaw < T , options extends InferOptions = { omitResolver : never } > = T extends AnyInput | AnyObject | AnyInterface ? {
96
102
__typename ?: T [ '_name' ]
97
103
} & {
@@ -119,21 +125,21 @@ export type InferShallow<T, options extends InferOptions = { omitResolver: never
119
125
T extends AnyInternal ? T [ '_shape' ] :
120
126
T
121
127
122
- export type InferArgs < T extends AnyType > = ExpandRecursively < InferArgsRaw < T > >
123
- export type InferArgsRaw < T extends AnyType > = T extends AnyObject | AnyInterface ? {
128
+ export type InferArgs < T extends AnyExposedType > = ExpandRecursively < InferArgsRaw < T > >
129
+ export type InferArgsRaw < T extends AnyExposedType > = T extends AnyObject | AnyInterface ? {
124
130
[ K in keyof T [ '_shape' ] ] : InferArgRaw < T [ '_shape' ] [ K ] >
125
131
} : never
126
132
127
133
export type InferArg < T > = ExpandRecursively < InferArgRaw < T > >
128
134
export type InferArgRaw < T > = T extends AnyArgs ? {
129
- [ K in keyof T [ '_args' ] as T [ '_args' ] [ K ] extends AnyOptional ? never : K ] : InferRaw < T [ '_args' ] [ K ] >
135
+ [ K in keyof T [ '_args' ] as T [ '_args' ] [ K ] extends AnyOptional ? never : K ] : InferRaw < T [ '_args' ] [ K ] >
130
136
} & {
131
- [ K in keyof T [ '_args' ] as T [ '_args' ] [ K ] extends AnyOptional ? K : never ] ?: InferRaw < T [ '_args' ] [ K ] >
132
- } : never
137
+ [ K in keyof T [ '_args' ] as T [ '_args' ] [ K ] extends AnyOptional ? K : never ] ?: InferRaw < T [ '_args' ] [ K ] >
138
+ } : never
133
139
134
140
export type InferUnionNames < T > = T extends AnyUnion ? ObjectToUnion < T [ '_inner' ] > [ '_name' ] : never
135
141
136
- export type InferResolvers < T extends AnyTypes , X extends InferResolverConfig > = {
142
+ export type InferResolvers < T extends AnyExposedTypes , X extends InferResolverConfig > = {
137
143
[ K in keyof T ] : K extends 'Subscription' ? {
138
144
[ G in keyof T [ K ] [ '_shape' ] ] ?: {
139
145
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 } > } > >
@@ -145,17 +151,17 @@ export type InferResolvers<T extends AnyTypes, X extends InferResolverConfig> =
145
151
[ G in keyof T [ K ] [ '_shape' ] ] ?: {
146
152
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 } > >
147
153
} | {
148
- 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 } > [ ] >
154
+ 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 } > [ ] >
149
155
} | {
150
- 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 } > [ ] >
156
+ 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 } > [ ] >
151
157
}
152
158
} & {
153
159
__isTypeOf ?: ( parent : Infer < T [ K ] > , context : X [ 'context' ] , info : GraphQLResolveInfo ) => MaybePromise < boolean >
154
160
__resolveType ?: ( parent : Infer < T [ K ] > , context : X [ 'context' ] , info : GraphQLResolveInfo ) => MaybePromise < InferUnionNames < T [ K ] > >
155
161
}
156
162
}
157
163
158
- export type InferResolversStrict < T extends AnyTypes , X extends InferResolverConfig > = {
164
+ export type InferResolversStrict < T extends AnyExposedTypes , X extends InferResolverConfig > = {
159
165
[ K in keyof T ] : K extends 'Subscription' ? {
160
166
[ G in keyof T [ K ] [ '_shape' ] ] : {
161
167
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 } > } > >
@@ -167,9 +173,9 @@ export type InferResolversStrict<T extends AnyTypes, X extends InferResolverConf
167
173
[ G in keyof T [ K ] [ '_shape' ] ] : {
168
174
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 } > >
169
175
} | {
170
- 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 } > > [ ]
176
+ 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 } > > [ ]
171
177
} | {
172
- 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 } > > [ ]
178
+ 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 } > > [ ]
173
179
}
174
180
} & {
175
181
__isTypeOf ?: ( parent : Infer < T [ K ] > , context : X [ 'context' ] , info : GraphQLResolveInfo ) => MaybePromise < boolean >
@@ -180,7 +186,7 @@ export type InferResolversStrict<T extends AnyTypes, X extends InferResolverConf
180
186
class GType < N extends string , T extends AnyTypes > extends Type < T , 'ObjectType' > {
181
187
declare _name : N
182
188
183
- constructor ( name : string , shape : T , interfaces ?: AnyInterface [ ] , extend ?: AnyTypes [ ] ) {
189
+ constructor ( name : string , shape : T , interfaces ?: AnyInterface [ ] , extend ?: AnyExposedTypes [ ] ) {
184
190
super ( )
185
191
this . typeDef = {
186
192
name,
@@ -197,17 +203,17 @@ class GType<N extends string, T extends AnyTypes> extends Type<T, 'ObjectType'>
197
203
return new GType < N , T & UnionToIntersection < D [ '_shape' ] > > ( this . typeDef . name , this . typeDef . shape as any , Array . isArray ( ref ) ? ref : [ ref ] , this . typeDef . extend )
198
204
}
199
205
200
- extend < D extends AnyTypes > ( ref : D | D [ ] ) {
206
+ extend < D extends AnyExposedTypes > ( ref : D | D [ ] ) {
201
207
// This is temporary construct, until we figure out how to properly manage to shared schema
202
208
this . typeDef . extend = Array . isArray ( ref ) ? ref : [ ref ]
203
209
return new GType < N , T & UnionToIntersection < D > > ( this . typeDef . name , this . typeDef . shape as any , this . typeDef . interfaces , Array . isArray ( ref ) ? ref : [ ref ] )
204
210
}
205
211
}
206
212
207
- class GInput < N extends string , T extends AnyTypes > extends Type < T , 'InputType' > {
213
+ class GInput < N extends string , T extends AnyExposedTypes > extends Type < T , 'InputType' > {
208
214
declare _name : N
209
215
210
- constructor ( name : string , shape : T , extend ?: AnyTypes [ ] ) {
216
+ constructor ( name : string , shape : T , extend ?: AnyExposedTypes [ ] ) {
211
217
super ( )
212
218
this . typeDef = {
213
219
name,
@@ -217,17 +223,17 @@ class GInput<N extends string, T extends AnyTypes> extends Type<T, 'InputType'>
217
223
}
218
224
}
219
225
220
- extend < D extends AnyTypes > ( ref : D | D [ ] ) {
226
+ extend < D extends AnyExposedTypes > ( ref : D | D [ ] ) {
221
227
// This is temporary construct, until we figure out how to properly manage to shared schema
222
228
this . typeDef . extend = Array . isArray ( ref ) ? ref : [ ref ]
223
229
return new GInput < N , T & UnionToIntersection < D > > ( this . typeDef . name , this . typeDef . shape as any , Array . isArray ( ref ) ? ref : [ ref ] )
224
230
}
225
231
}
226
232
227
- class GInterface < N extends string , T extends AnyTypes > extends Type < T , 'InterfaceType' > {
233
+ class GInterface < N extends string , T extends AnyExposedTypes > extends Type < T , 'InterfaceType' > {
228
234
declare _name : N
229
235
230
- constructor ( name : string , shape : T , interfaces ?: AnyInterface [ ] , extend ?: AnyTypes [ ] ) {
236
+ constructor ( name : string , shape : T , interfaces ?: AnyInterface [ ] , extend ?: AnyExposedTypes [ ] ) {
231
237
super ( )
232
238
this . typeDef = {
233
239
name,
@@ -241,10 +247,10 @@ class GInterface<N extends string, T extends AnyTypes> extends Type<T, 'Interfac
241
247
implements < D extends AnyInterface > ( ref : D | D [ ] ) {
242
248
// This is temporary construct, until we figure out how to properly manage to shared schema
243
249
this . typeDef . interfaces = Array . isArray ( ref ) ? ref : [ ref ]
244
- return new GInterface < N , T & UnionToIntersection < D [ '_shape' ] > > ( this . typeDef . name , this . typeDef . shape as any , Array . isArray ( ref ) ? ref : [ ref ] , this . typeDef . extend )
250
+ return new GInterface < N , T & UnionToIntersection < D [ '_shape' ] > > ( this . typeDef . name , this . typeDef . shape as any , Array . isArray ( ref ) ? ref : [ ref ] , this . typeDef . extend )
245
251
}
246
252
247
- extend < D extends AnyTypes > ( ref : D | D [ ] ) {
253
+ extend < D extends AnyExposedTypes > ( ref : D | D [ ] ) {
248
254
// This is temporary construct, until we figure out how to properly manage to shared schema
249
255
this . typeDef . extend = Array . isArray ( ref ) ? ref : [ ref ]
250
256
return new GInterface < N , T & UnionToIntersection < D > > ( this . typeDef . name , this . typeDef . shape as any , this . typeDef . interfaces , Array . isArray ( ref ) ? ref : [ ref ] )
@@ -434,7 +440,7 @@ class GRef<T> extends Type<T, 'Ref'> {
434
440
}
435
441
}
436
442
437
- class GInternal < T > extends Type < T , 'Internal' > {
443
+ class GInternal < T > extends Type < T , 'Internal' , true > {
438
444
constructor ( ) {
439
445
super ( )
440
446
this . typeDef = {
@@ -443,17 +449,13 @@ class GInternal<T> extends Type<T, 'Internal'> {
443
449
}
444
450
445
451
optional ( ) {
446
- return new GOptional < this> ( this )
452
+ return new GOptionalBase < this> ( this ) ;
447
453
}
448
454
449
455
required ( ) {
450
456
this . typeDef . isRequired = true
451
457
return this
452
458
}
453
-
454
- omitResolver ( ) {
455
- return new GOmitResolver < this> ( this )
456
- }
457
459
}
458
460
459
461
class GScalar < I , O > extends Type < I , 'Scalar' > {
@@ -497,7 +499,7 @@ class GList<T extends AnyType> extends Type<T, 'List'> {
497
499
return this
498
500
}
499
501
500
- omitResolver ( ) {
502
+ omitResolver ( ) {
501
503
return new GOmitResolver < this> ( this )
502
504
}
503
505
@@ -510,7 +512,7 @@ class GList<T extends AnyType> extends Type<T, 'List'> {
510
512
}
511
513
}
512
514
513
- class GPaginatedList < T extends AnyType > extends Type < T , 'PaginatedList' > {
515
+ class GPaginatedList < T extends AnyExposedType > extends Type < T , 'PaginatedList' > {
514
516
declare _inner : {
515
517
edges : {
516
518
node : InferRaw < T >
@@ -541,7 +543,7 @@ class GPaginatedList<T extends AnyType> extends Type<T, 'PaginatedList'> {
541
543
return this
542
544
}
543
545
544
- omitResolver ( ) {
546
+ omitResolver ( ) {
545
547
return new GOmitResolver < this> ( this )
546
548
}
547
549
@@ -554,12 +556,13 @@ class GPaginatedList<T extends AnyType> extends Type<T, 'PaginatedList'> {
554
556
// }
555
557
}
556
558
557
- class GOptional < T extends AnyType > extends Type < T , 'Optional' > {
559
+ class GOptionalBase < T extends AnyType > extends Type < T , 'Optional' , T [ "_internal" ] > {
558
560
constructor ( shape : T ) {
559
561
super ( )
560
562
this . typeDef = shape . typeDef
561
563
this . typeDef . isOptional = true
562
564
this . typeDef . isRequired = false
565
+ this . _internal = shape . _internal
563
566
}
564
567
565
568
list ( ) {
@@ -570,8 +573,10 @@ class GOptional<T extends AnyType> extends Type<T, 'Optional'> {
570
573
this . typeDef . defaultValue = value
571
574
return this
572
575
}
576
+ }
573
577
574
- omitResolver ( ) {
578
+ class GOptional < T extends AnyExposedType > extends GOptionalBase < T > {
579
+ omitResolver ( ) {
575
580
return new GOmitResolver < this> ( this )
576
581
}
577
582
@@ -580,7 +585,7 @@ class GOptional<T extends AnyType> extends Type<T, 'Optional'> {
580
585
}
581
586
}
582
587
583
- class GOmitResolver < T extends AnyType > extends Type < T , 'OmitResolver' > {
588
+ class GOmitResolver < T extends AnyExposedType > extends Type < T , 'OmitResolver' > {
584
589
constructor ( shape : T ) {
585
590
super ( )
586
591
this . typeDef = shape . typeDef
@@ -609,7 +614,7 @@ class GOmitResolver<T extends AnyType> extends Type<T, 'OmitResolver'> {
609
614
}
610
615
}
611
616
612
- class GArgs < T extends AnyType , X extends Args > extends Type < T , 'Args' > {
617
+ class GArgs < T extends AnyExposedType , X extends Args > extends Type < T , 'Args' > {
613
618
declare _args : X
614
619
615
620
constructor ( shape : T , args : X ) {
@@ -620,7 +625,7 @@ class GArgs<T extends AnyType, X extends Args> extends Type<T, 'Args'> {
620
625
}
621
626
622
627
export class GarphSchema {
623
- types : Map < string , AnyType > = new Map ( )
628
+ types : Map < string , AnyExposedType > = new Map ( )
624
629
625
630
nodeType = this . interface ( 'Node' , {
626
631
id : this . id ( )
@@ -640,13 +645,13 @@ export class GarphSchema {
640
645
after : this . id ( ) . optional ( )
641
646
}
642
647
643
- registerType ( type : AnyType ) {
648
+ registerType ( type : AnyExposedType ) {
644
649
const name = type . typeDef . name
645
650
if ( this . types . has ( name ) ) throw new Error ( `Type with name "${ name } " already exists` )
646
651
this . types . set ( name , type )
647
652
}
648
653
649
- constructor ( { types } : { types : AnyType [ ] } = { types : [ ] } ) {
654
+ constructor ( { types } : { types : AnyExposedType [ ] } = { types : [ ] } ) {
650
655
types . forEach ( t => this . registerType ( t ) )
651
656
}
652
657
@@ -656,7 +661,7 @@ export class GarphSchema {
656
661
return t
657
662
}
658
663
659
- node < N extends string , T extends AnyTypes > ( name : N , shape : T ) {
664
+ node < N extends string , T extends AnyExposedTypes > ( name : N , shape : T ) {
660
665
const t = new GType < N , T > ( name , shape ) . implements ( this . nodeType )
661
666
this . registerType ( t )
662
667
return t
@@ -685,7 +690,7 @@ export class GarphSchema {
685
690
return t
686
691
}
687
692
688
- inputType < N extends string , T extends AnyTypes > ( name : N , shape : T ) {
693
+ inputType < N extends string , T extends AnyExposedTypes > ( name : N , shape : T ) {
689
694
const t = new GInput < N , T > ( name , shape )
690
695
this . registerType ( t )
691
696
return t
@@ -709,7 +714,7 @@ export class GarphSchema {
709
714
return t
710
715
}
711
716
712
- interface < N extends string , T extends AnyTypes > ( name : N , shape : T ) {
717
+ interface < N extends string , T extends AnyExposedTypes > ( name : N , shape : T ) {
713
718
const t = new GInterface < N , T > ( name , shape )
714
719
this . registerType ( t )
715
720
return t
@@ -741,7 +746,7 @@ export class GarphSchema {
741
746
return new GRef < T > ( ref )
742
747
}
743
748
744
- internal < T > ( ) {
749
+ internal < T > ( ) {
745
750
return new GInternal < T > ( )
746
751
}
747
752
0 commit comments