Skip to content

Commit f78e758

Browse files
committed
let publisher filter method use generic predicate
1 parent 0fda94a commit f78e758

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

src/execution/execute.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -754,15 +754,15 @@ function executeField(
754754
return completed.then(undefined, (rawError) => {
755755
const error = locatedError(rawError, fieldNodes, pathToArray(path));
756756
const handledError = handleFieldError(error, returnType, errors);
757-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
757+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
758758
return handledError;
759759
});
760760
}
761761
return completed;
762762
} catch (rawError) {
763763
const error = locatedError(rawError, fieldNodes, pathToArray(path));
764764
const handledError = handleFieldError(error, returnType, errors);
765-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
765+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
766766
return handledError;
767767
}
768768
}
@@ -952,7 +952,7 @@ async function completePromisedValue(
952952
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
953953
const error = locatedError(rawError, fieldNodes, pathToArray(path));
954954
const handledError = handleFieldError(error, returnType, errors);
955-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
955+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
956956
return handledError;
957957
}
958958
}
@@ -1233,10 +1233,7 @@ function completeListItemValue(
12331233
pathToArray(itemPath),
12341234
);
12351235
const handledError = handleFieldError(error, itemType, errors);
1236-
exeContext.publisher.filterSubsequentPayloads(
1237-
itemPath,
1238-
asyncPayloadRecord,
1239-
);
1236+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
12401237
return handledError;
12411238
}),
12421239
);
@@ -1248,7 +1245,7 @@ function completeListItemValue(
12481245
} catch (rawError) {
12491246
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
12501247
const handledError = handleFieldError(error, itemType, errors);
1251-
exeContext.publisher.filterSubsequentPayloads(itemPath, asyncPayloadRecord);
1248+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
12521249
completedResults.push(handledError);
12531250
}
12541251

@@ -1846,7 +1843,7 @@ function executeStreamField(
18461843
(value) => asyncPayloadRecord.addItems([value]),
18471844
(error) => {
18481845
asyncPayloadRecord.errors.push(error);
1849-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
1846+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
18501847
asyncPayloadRecord.addItems(null);
18511848
},
18521849
);
@@ -1873,14 +1870,11 @@ function executeStreamField(
18731870
itemType,
18741871
asyncPayloadRecord.errors,
18751872
);
1876-
exeContext.publisher.filterSubsequentPayloads(
1877-
itemPath,
1878-
asyncPayloadRecord,
1879-
);
1873+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
18801874
}
18811875
} catch (error) {
18821876
asyncPayloadRecord.errors.push(error);
1883-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
1877+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
18841878
asyncPayloadRecord.addItems(null);
18851879
return asyncPayloadRecord;
18861880
}
@@ -1894,20 +1888,14 @@ function executeStreamField(
18941888
itemType,
18951889
asyncPayloadRecord.errors,
18961890
);
1897-
exeContext.publisher.filterSubsequentPayloads(
1898-
itemPath,
1899-
asyncPayloadRecord,
1900-
);
1891+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19011892
return handledError;
19021893
})
19031894
.then(
19041895
(value) => asyncPayloadRecord.addItems([value]),
19051896
(error) => {
19061897
asyncPayloadRecord.errors.push(error);
1907-
exeContext.publisher.filterSubsequentPayloads(
1908-
path,
1909-
asyncPayloadRecord,
1910-
);
1898+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
19111899
asyncPayloadRecord.addItems(null);
19121900
},
19131901
);
@@ -1962,18 +1950,15 @@ async function executeStreamIteratorItem(
19621950
itemType,
19631951
asyncPayloadRecord.errors,
19641952
);
1965-
exeContext.publisher.filterSubsequentPayloads(
1966-
itemPath,
1967-
asyncPayloadRecord,
1968-
);
1953+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19691954
return handledError;
19701955
});
19711956
}
19721957
return { done: false, value: completedItem };
19731958
} catch (rawError) {
19741959
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
19751960
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1976-
exeContext.publisher.filterSubsequentPayloads(itemPath, asyncPayloadRecord);
1961+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19771962
return { done: false, value };
19781963
}
19791964
}
@@ -2016,7 +2001,7 @@ async function executeStreamIterator(
20162001
);
20172002
} catch (error) {
20182003
asyncPayloadRecord.errors.push(error);
2019-
exeContext.publisher.filterSubsequentPayloads(path, asyncPayloadRecord);
2004+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
20202005
asyncPayloadRecord.addItems(null);
20212006
// entire stream has errored and bubbled upwards
20222007
if (iterator?.return) {
@@ -2034,10 +2019,7 @@ async function executeStreamIterator(
20342019
(resolvedItem) => asyncPayloadRecord.addItems([resolvedItem]),
20352020
(error) => {
20362021
asyncPayloadRecord.errors.push(error);
2037-
exeContext.publisher.filterSubsequentPayloads(
2038-
path,
2039-
asyncPayloadRecord,
2040-
);
2022+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
20412023
asyncPayloadRecord.addItems(null);
20422024
},
20432025
);
@@ -2053,6 +2035,28 @@ async function executeStreamIterator(
20532035
}
20542036
}
20552037

2038+
function filterSubsequentPayloads(
2039+
exeContext: ExecutionContext,
2040+
nullPath: Path,
2041+
currentAsyncRecord: AsyncPayloadRecord | undefined,
2042+
): void {
2043+
const nullPathArray = pathToArray(nullPath);
2044+
exeContext.publisher.filter((asyncRecord) => {
2045+
if (asyncRecord === currentAsyncRecord) {
2046+
// don't remove payload from where error originates
2047+
return true;
2048+
}
2049+
for (let i = 0; i < nullPathArray.length; i++) {
2050+
if (asyncRecord.path[i] !== nullPathArray[i]) {
2051+
// asyncRecord points to a path unaffected by this payload
2052+
return true;
2053+
}
2054+
}
2055+
2056+
return false;
2057+
});
2058+
}
2059+
20562060
class DeferredFragmentRecord {
20572061
type: 'defer';
20582062
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)