Skip to content

Commit a216dc8

Browse files
committed
let publisher filter method use generic predicate
1 parent a8d05eb commit a216dc8

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

@@ -1924,10 +1921,7 @@ function executeStreamField(
19241921
itemType,
19251922
asyncPayloadRecord.errors,
19261923
);
1927-
exeContext.publisher.filterSubsequentPayloads(
1928-
itemPath,
1929-
asyncPayloadRecord,
1930-
);
1924+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19311925
return handledError;
19321926
});
19331927
}
@@ -1938,14 +1932,11 @@ function executeStreamField(
19381932
itemType,
19391933
asyncPayloadRecord.errors,
19401934
);
1941-
exeContext.publisher.filterSubsequentPayloads(
1942-
itemPath,
1943-
asyncPayloadRecord,
1944-
);
1935+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19451936
}
19461937
} catch (error) {
19471938
asyncPayloadRecord.errors.push(error);
1948-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
1939+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
19491940
asyncPayloadRecord.addItems(null);
19501941
return asyncPayloadRecord;
19511942
}
@@ -1956,7 +1947,7 @@ function executeStreamField(
19561947
(value) => [value],
19571948
(error) => {
19581949
asyncPayloadRecord.errors.push(error);
1959-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
1950+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
19601951
return null;
19611952
},
19621953
);
@@ -2011,18 +2002,15 @@ async function executeStreamIteratorItem(
20112002
itemType,
20122003
asyncPayloadRecord.errors,
20132004
);
2014-
exeContext.publisher.filterSubsequentPayloads(
2015-
itemPath,
2016-
asyncPayloadRecord,
2017-
);
2005+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
20182006
return handledError;
20192007
});
20202008
}
20212009
return { done: false, value: completedItem };
20222010
} catch (rawError) {
20232011
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
20242012
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
2025-
exeContext.publisher.filterSubsequentPayloads(itemPath, asyncPayloadRecord);
2013+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
20262014
return { done: false, value };
20272015
}
20282016
}
@@ -2065,7 +2053,7 @@ async function executeStreamIterator(
20652053
);
20662054
} catch (error) {
20672055
asyncPayloadRecord.errors.push(error);
2068-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
2056+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
20692057
asyncPayloadRecord.addItems(null);
20702058
// entire stream has errored and bubbled upwards
20712059
if (iterator?.return) {
@@ -2084,10 +2072,7 @@ async function executeStreamIterator(
20842072
(value) => [value],
20852073
(error) => {
20862074
asyncPayloadRecord.errors.push(error);
2087-
exeContext.publisher.filterSubsequentPayloads(
2088-
path,
2089-
asyncPayloadRecord,
2090-
);
2075+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
20912076
return null;
20922077
},
20932078
);
@@ -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)