Skip to content

Commit 911d297

Browse files
committed
memoise wrapper values
1 parent a62eea8 commit 911d297

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/type/wrappers.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ declare class GraphQLList<+T: GraphQLType> {
3434
// Note: constructors cannot be used for covariant types. Drop the "new".
3535
constructor(ofType: any): void;
3636
}
37+
const listCache = new WeakMap();
3738
// eslint-disable-next-line no-redeclare
3839
export function GraphQLList(ofType) {
3940
if (this instanceof GraphQLList) {
4041
this.ofType = assertType(ofType);
4142
} 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;
4350
}
4451
}
4552

@@ -75,12 +82,19 @@ declare class GraphQLNonNull<+T: GraphQLNullableType> {
7582
// Note: constructors cannot be used for covariant types. Drop the "new".
7683
constructor(ofType: any): void;
7784
}
85+
const nnCache = new WeakMap();
7886
// eslint-disable-next-line no-redeclare
7987
export function GraphQLNonNull(ofType) {
8088
if (this instanceof GraphQLNonNull) {
8189
this.ofType = assertNullableType(ofType);
8290
} 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;
8498
}
8599
}
86100

0 commit comments

Comments
 (0)