Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoise wrapper values #1252

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/type/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ declare class GraphQLList<+T: GraphQLType> {
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: any): void;
}
const listCache = new WeakMap();
// eslint-disable-next-line no-redeclare
export function GraphQLList(ofType) {
if (this instanceof GraphQLList) {
this.ofType = assertType(ofType);
} else {
return new GraphQLList(ofType);
const cachedValue = listCache.get(ofType);
if (cachedValue !== undefined) {
return cachedValue;
}
const newValue = new GraphQLList(ofType);
listCache.set(ofType, newValue);
return newValue;
}
}

Expand Down Expand Up @@ -75,12 +82,19 @@ declare class GraphQLNonNull<+T: GraphQLNullableType> {
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: any): void;
}
const nnCache = new WeakMap();
// eslint-disable-next-line no-redeclare
export function GraphQLNonNull(ofType) {
if (this instanceof GraphQLNonNull) {
this.ofType = assertNullableType(ofType);
} else {
return new GraphQLNonNull(ofType);
const cachedValue = nnCache.get(ofType);
if (cachedValue !== undefined) {
return cachedValue;
}
const newValue = new GraphQLNonNull(ofType);
nnCache.set(ofType, newValue);
return newValue;
}
}

Expand Down