Skip to content

Commit f9ff0d4

Browse files
committed
let publisher filter method use generic predicate
1 parent 2de06f8 commit f9ff0d4

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

src/execution/execute.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -748,15 +748,15 @@ function executeField(
748748
return completed.then(undefined, (rawError) => {
749749
const error = locatedError(rawError, fieldNodes, pathToArray(path));
750750
const handledError = handleFieldError(error, returnType, errors);
751-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
751+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
752752
return handledError;
753753
});
754754
}
755755
return completed;
756756
} catch (rawError) {
757757
const error = locatedError(rawError, fieldNodes, pathToArray(path));
758758
const handledError = handleFieldError(error, returnType, errors);
759-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
759+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
760760
return handledError;
761761
}
762762
}
@@ -1188,10 +1188,7 @@ function completeListItemValue(
11881188
pathToArray(itemPath),
11891189
);
11901190
const handledError = handleFieldError(error, itemType, errors);
1191-
exeContext.publisher.filterSubsequentPayloads(
1192-
itemPath,
1193-
asyncPayloadRecord,
1194-
);
1191+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
11951192
return handledError;
11961193
}),
11971194
);
@@ -1203,7 +1200,7 @@ function completeListItemValue(
12031200
} catch (rawError) {
12041201
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
12051202
const handledError = handleFieldError(error, itemType, errors);
1206-
exeContext.publisher.filterSubsequentPayloads(itemPath, asyncPayloadRecord);
1203+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
12071204
completedResults.push(handledError);
12081205
}
12091206

@@ -1929,10 +1926,7 @@ function executeStreamField(
19291926
itemType,
19301927
asyncPayloadRecord.errors,
19311928
);
1932-
exeContext.publisher.filterSubsequentPayloads(
1933-
itemPath,
1934-
asyncPayloadRecord,
1935-
);
1929+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19361930
return handledError;
19371931
});
19381932
}
@@ -1943,14 +1937,11 @@ function executeStreamField(
19431937
itemType,
19441938
asyncPayloadRecord.errors,
19451939
);
1946-
exeContext.publisher.filterSubsequentPayloads(
1947-
itemPath,
1948-
asyncPayloadRecord,
1949-
);
1940+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19501941
}
19511942
} catch (error) {
19521943
asyncPayloadRecord.errors.push(error);
1953-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
1944+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
19541945
asyncPayloadRecord.addItems(null);
19551946
return asyncPayloadRecord;
19561947
}
@@ -1960,7 +1951,7 @@ function executeStreamField(
19601951
(resolvedItem) => asyncPayloadRecord.addItems([resolvedItem]),
19611952
(error) => {
19621953
asyncPayloadRecord.errors.push(error);
1963-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
1954+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
19641955
asyncPayloadRecord.addItems(null);
19651956
},
19661957
);
@@ -2014,18 +2005,15 @@ async function executeStreamIteratorItem(
20142005
itemType,
20152006
asyncPayloadRecord.errors,
20162007
);
2017-
exeContext.publisher.filterSubsequentPayloads(
2018-
itemPath,
2019-
asyncPayloadRecord,
2020-
);
2008+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
20212009
return handledError;
20222010
});
20232011
}
20242012
return { done: false, value: completedItem };
20252013
} catch (rawError) {
20262014
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
20272015
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
2028-
exeContext.publisher.filterSubsequentPayloads(itemPath, asyncPayloadRecord);
2016+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
20292017
return { done: false, value };
20302018
}
20312019
}
@@ -2068,7 +2056,7 @@ async function executeStreamIterator(
20682056
);
20692057
} catch (error) {
20702058
asyncPayloadRecord.errors.push(error);
2071-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
2059+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
20722060
asyncPayloadRecord.addItems(null);
20732061
// entire stream has errored and bubbled upwards
20742062
if (iterator?.return) {
@@ -2086,10 +2074,7 @@ async function executeStreamIterator(
20862074
(resolvedItem) => asyncPayloadRecord.addItems([resolvedItem]),
20872075
(error) => {
20882076
asyncPayloadRecord.errors.push(error);
2089-
exeContext.publisher.filterSubsequentPayloads(
2090-
path,
2091-
asyncPayloadRecord,
2092-
);
2077+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
20932078
asyncPayloadRecord.addItems(null);
20942079
},
20952080
);
@@ -2105,6 +2090,28 @@ async function executeStreamIterator(
21052090
}
21062091
}
21072092

2093+
function filterSubsequentPayloads(
2094+
exeContext: ExecutionContext,
2095+
nullPath: Path,
2096+
currentAsyncRecord: AsyncPayloadRecord | undefined,
2097+
): void {
2098+
const nullPathArray = pathToArray(nullPath);
2099+
exeContext.publisher.filter((asyncRecord) => {
2100+
if (asyncRecord === currentAsyncRecord) {
2101+
// don't remove payload from where error originates
2102+
return true;
2103+
}
2104+
for (let i = 0; i < nullPathArray.length; i++) {
2105+
if (asyncRecord.path[i] !== nullPathArray[i]) {
2106+
// asyncRecord points to a path unaffected by this payload
2107+
return true;
2108+
}
2109+
}
2110+
2111+
return false;
2112+
});
2113+
}
2114+
21082115
class DeferredFragmentRecord {
21092116
type: 'defer';
21102117
errors: Array<GraphQLError>;

src/execution/publisher.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Path, pathToArray } from '../jsutils/Path.js';
2-
31
import type {
42
AsyncPayloadRecord,
53
StreamRecord,
@@ -23,22 +21,11 @@ export class Publisher {
2321
this.subsequentPayloads.add(payload);
2422
}
2523

26-
filterSubsequentPayloads(
27-
nullPath: Path,
28-
currentAsyncRecord: AsyncPayloadRecord | undefined,
29-
): void {
30-
const nullPathArray = pathToArray(nullPath);
24+
filter(predicate: (payload: AsyncPayloadRecord) => boolean): void {
3125
this.subsequentPayloads.forEach((asyncRecord) => {
32-
if (asyncRecord === currentAsyncRecord) {
33-
// don't remove payload from where error originates
26+
if (predicate(asyncRecord)) {
3427
return;
3528
}
36-
for (let i = 0; i < nullPathArray.length; i++) {
37-
if (asyncRecord.path[i] !== nullPathArray[i]) {
38-
// asyncRecord points to a path unaffected by this payload
39-
return;
40-
}
41-
}
4229
// asyncRecord path points to nulled error field
4330
if (isStreamPayload(asyncRecord) && asyncRecord.iterator?.return) {
4431
asyncRecord.iterator.return().catch(() => {

0 commit comments

Comments
 (0)