Skip to content

Commit 152305c

Browse files
committed
More efficient collection of input objects
1 parent e72c857 commit 152305c

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

codegen/custom-scalars/plugin.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const plugin: PluginFunction<CustomScalarsPluginConfig, string> = async (
6060

6161
const types = Object.values(schema.getTypeMap());
6262
const customScalars = new Set<string>();
63-
const inputObjects: InputObjectMap = new Map();
63+
let inputObjects: InputObjectMap = new Map();
6464

6565
for (const type of types) {
6666
if (isCustomScalar(type, config)) {
@@ -99,14 +99,7 @@ export const plugin: PluginFunction<CustomScalarsPluginConfig, string> = async (
9999
);
100100

101101
usedFields = fields;
102-
103-
const reachable = getReachableInputObjects(inputObjects, usedInputObjects);
104-
105-
for (const name of inputObjects.keys()) {
106-
if (!reachable.has(name)) {
107-
inputObjects.delete(name);
108-
}
109-
}
102+
inputObjects = getReachableInputObjects(inputObjects, usedInputObjects);
110103
}
111104

112105
// After filtering input objects used by documents, we need to figure out
@@ -330,24 +323,30 @@ function collectDocumentUsage(
330323
function getReachableInputObjects(
331324
inputObjects: InputObjectMap,
332325
usedInputObjects: Set<string>
333-
) {
334-
const reachable = new Set<string>();
326+
): InputObjectMap {
327+
const reachable: InputObjectMap = new Map();
335328
const queue: string[] = [];
336329

337330
for (const name of usedInputObjects) {
338-
if (inputObjects.has(name)) {
339-
reachable.add(name);
331+
const fields = inputObjects.get(name);
332+
333+
if (fields && !reachable.has(name)) {
334+
reachable.set(name, fields);
340335
queue.push(name);
341336
}
342337
}
343338

344339
while (queue.length) {
345-
const fields = inputObjects.get(queue.pop()!)!;
340+
const fields = reachable.get(queue.pop()!)!;
346341

347342
for (const typeName of Object.values(fields)) {
348-
if (inputObjects.has(typeName) && !reachable.has(typeName)) {
349-
reachable.add(typeName);
350-
queue.push(typeName);
343+
if (!reachable.has(typeName)) {
344+
const dependencyFields = inputObjects.get(typeName);
345+
346+
if (dependencyFields) {
347+
reachable.set(typeName, dependencyFields);
348+
queue.push(typeName);
349+
}
351350
}
352351
}
353352
}

0 commit comments

Comments
 (0)