@@ -34,12 +34,19 @@ declare class GraphQLList<+T: GraphQLType> {
34
34
// Note: constructors cannot be used for covariant types. Drop the "new".
35
35
constructor ( ofType : any ) : void ;
36
36
}
37
+ const listCache = new WeakMap ( ) ;
37
38
// eslint-disable-next-line no-redeclare
38
39
export function GraphQLList ( ofType ) {
39
40
if ( this instanceof GraphQLList ) {
40
41
this . ofType = assertType ( ofType ) ;
41
42
} else {
42
- return new GraphQLList ( ofType ) ;
43
+ const cachedValue = listCache . get ( ofType ) ;
44
+ if ( cachedValue !== undefined ) {
45
+ return cachedValue ;
46
+ }
47
+ const newValue = new GraphQLList ( ofType ) ;
48
+ listCache . set ( ofType , newValue ) ;
49
+ return newValue ;
43
50
}
44
51
}
45
52
@@ -75,12 +82,19 @@ declare class GraphQLNonNull<+T: GraphQLNullableType> {
75
82
// Note: constructors cannot be used for covariant types. Drop the "new".
76
83
constructor ( ofType : any ) : void ;
77
84
}
85
+ const nnCache = new WeakMap ( ) ;
78
86
// eslint-disable-next-line no-redeclare
79
87
export function GraphQLNonNull ( ofType ) {
80
88
if ( this instanceof GraphQLNonNull ) {
81
89
this . ofType = assertNullableType ( ofType ) ;
82
90
} else {
83
- return new GraphQLNonNull ( ofType ) ;
91
+ const cachedValue = nnCache . get ( ofType ) ;
92
+ if ( cachedValue !== undefined ) {
93
+ return cachedValue ;
94
+ }
95
+ const newValue = new GraphQLNonNull ( ofType ) ;
96
+ nnCache . set ( ofType , newValue ) ;
97
+ return newValue ;
84
98
}
85
99
}
86
100
0 commit comments