Skip to content

Commit 5e130c9

Browse files
committed
feat(read): switch PayloadError arrays to nullable PayloadErrors
1 parent ab5be31 commit 5e130c9

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

libs/isograph-react/src/core/read.ts

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import {
2323
getOrLoadIsographArtifact,
2424
IsographEnvironment,
2525
type DataTypeValue,
26-
type PayloadError,
26+
type PayloadErrors,
2727
type StoreLink,
2828
type StoreRecord,
29-
type WithErrors,
29+
type WithErrors
3030
} from './IsographEnvironment';
3131
import { logMessage } from './logging';
3232
import { maybeMakeNetworkRequest } from './makeNetworkRequest';
@@ -53,7 +53,7 @@ import { Arguments } from './util';
5353
export type WithEncounteredRecords<T> = {
5454
readonly encounteredRecords: EncounteredIds;
5555
readonly item: ExtractData<T>;
56-
readonly errors: PayloadError[];
56+
readonly errors: PayloadErrors | null;
5757
};
5858

5959
export function readButDoNotEvaluate<
@@ -131,7 +131,7 @@ export function readButDoNotEvaluate<
131131
export type ReadDataResultSuccess<Data> = {
132132
readonly kind: 'Success';
133133
readonly data: Data;
134-
readonly errors: PayloadError[];
134+
readonly errors: PayloadErrors | null;
135135
};
136136

137137
type ReadDataResultError = {
@@ -173,13 +173,13 @@ function readData<TReadFromStore>(
173173
return {
174174
kind: 'Success',
175175
data: null as any,
176-
errors: [],
176+
errors: null,
177177
};
178178
}
179179

180180
let target: { [index: string]: any } = {};
181181

182-
const errors: PayloadError[] = [];
182+
let errors: PayloadErrors | null = null;
183183
for (const field of ast) {
184184
switch (field.kind) {
185185
case 'Scalar': {
@@ -188,7 +188,11 @@ function readData<TReadFromStore>(
188188
if (data.kind === 'MissingData') {
189189
return data;
190190
}
191-
errors.push(...data.errors);
191+
if (errors === null) {
192+
errors = data.errors;
193+
} else if (data.errors) {
194+
errors.push(...data.errors);
195+
}
192196
target[field.alias ?? field.fieldName] = data.data;
193197
break;
194198
}
@@ -221,7 +225,11 @@ function readData<TReadFromStore>(
221225
if (data.kind === 'MissingData') {
222226
return data;
223227
}
224-
errors.push(...data.errors);
228+
if (errors === null) {
229+
errors = data.errors;
230+
} else if (data.errors) {
231+
errors.push(...data.errors);
232+
}
225233
target[field.alias ?? field.fieldName] = data.data;
226234
break;
227235
}
@@ -239,7 +247,11 @@ function readData<TReadFromStore>(
239247
if (data.kind === 'MissingData') {
240248
return data;
241249
}
242-
errors.push(...data.errors);
250+
if (errors === null) {
251+
errors = data.errors;
252+
} else if (data.errors) {
253+
errors.push(...data.errors);
254+
}
243255
target[field.alias] = data.data;
244256
break;
245257
}
@@ -257,7 +269,11 @@ function readData<TReadFromStore>(
257269
if (data.kind === 'MissingData') {
258270
return data;
259271
}
260-
errors.push(...data.errors);
272+
if (errors === null) {
273+
errors = data.errors;
274+
} else if (data.errors) {
275+
errors.push(...data.errors);
276+
}
261277
target[field.alias] = data.data;
262278
break;
263279
}
@@ -274,7 +290,11 @@ function readData<TReadFromStore>(
274290
if (data.kind === 'MissingData') {
275291
return data;
276292
}
277-
errors.push(...data.errors);
293+
if (errors === null) {
294+
errors = data.errors;
295+
} else if (data.errors) {
296+
errors.push(...data.errors);
297+
}
278298
target[field.alias] = data.data;
279299
break;
280300
}
@@ -624,7 +644,7 @@ export function readResolverFieldData(
624644
case 'ComponentReaderArtifact': {
625645
return {
626646
kind: 'Success',
627-
errors: [],
647+
errors: null,
628648
data: getOrCreateCachedComponent(
629649
environment,
630650
field.readerArtifact.fieldName,
@@ -652,9 +672,9 @@ export function readScalarFieldData(
652672
const storeRecordName = getParentRecordKey(field, variables);
653673
const value = storeRecord.record[storeRecordName];
654674

655-
const errors: PayloadError[] = [];
675+
let errors: PayloadErrors | null = null;
656676
if (storeRecord.errors[storeRecordName] && value === null) {
657-
errors.push(...storeRecord.errors[storeRecordName]);
677+
errors = storeRecord.errors[storeRecordName];
658678
}
659679

660680
// TODO consider making scalars into discriminated unions. This probably has
@@ -693,10 +713,7 @@ export function readLinkedFieldData(
693713
const storeRecordName = getParentRecordKey(field, variables);
694714
const value = storeRecord.record[storeRecordName];
695715

696-
const errors: PayloadError[] = [];
697-
if (storeRecord.errors[storeRecordName] && value === null) {
698-
errors.push(...storeRecord.errors[storeRecordName]);
699-
}
716+
let errors: PayloadErrors | null = null;
700717

701718
if (Array.isArray(value)) {
702719
const results = [];
@@ -734,7 +751,11 @@ export function readLinkedFieldData(
734751
recordLink: result.recordLink,
735752
};
736753
}
737-
errors.push(...result.errors);
754+
if (errors === null) {
755+
errors = result.errors;
756+
} else if (result.errors) {
757+
errors.push(...result.errors);
758+
}
738759
results.push(result.data);
739760
}
740761
return {
@@ -758,9 +779,13 @@ export function readLinkedFieldData(
758779
recordLink: data.recordLink,
759780
};
760781
}
761-
errors.push(...data.errors);
782+
if (errors === null) {
783+
errors = data.errors;
784+
} else if (data.errors) {
785+
errors.push(...data.errors);
786+
}
762787

763-
if (data.errors.length < 1 && refetchQueryIndex === null) {
788+
if (refetchQueryIndex === null || data.errors === null) {
764789
// we can't call `condition.resolver` for client pointers when there are errors,
765790
// for inline fragment this is fine
766791

@@ -841,6 +866,9 @@ export function readLinkedFieldData(
841866
link = altLink;
842867
}
843868
} else if (link === null) {
869+
if (storeRecord.errors[storeRecordName]) {
870+
errors = storeRecord.errors[storeRecordName];
871+
}
844872
return {
845873
kind: 'Success',
846874
data: null,
@@ -874,7 +902,11 @@ export function readLinkedFieldData(
874902
recordLink: refetchReaderParams.recordLink,
875903
};
876904
}
877-
errors.push(...refetchReaderParams.errors);
905+
if (errors === null) {
906+
errors = refetchReaderParams.errors;
907+
} else if (refetchReaderParams.errors) {
908+
errors.push(...refetchReaderParams.errors);
909+
}
878910

879911
const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
880912
if (refetchQuery == null) {
@@ -962,7 +994,15 @@ export function readLinkedFieldData(
962994
recordLink: data.recordLink,
963995
};
964996
}
965-
return { ...data, errors: errors.concat(data.errors) };
997+
if (errors === null) {
998+
errors = data.errors;
999+
} else if (data.errors) {
1000+
errors.push(...data.errors);
1001+
}
1002+
return {
1003+
...data,
1004+
errors: errors,
1005+
};
9661006
}
9671007

9681008
export type NetworkRequestReaderOptions = {

libs/isograph-react/src/react/useReadAndSubscribe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function useReadAndSubscribe<
4040
setReadOutDataAndRecords,
4141
readerAst,
4242
);
43-
if (readOutDataAndRecords.errors.length) {
43+
if (readOutDataAndRecords.errors) {
4444
const errors = readOutDataAndRecords.errors.map(
4545
(error) => new GraphqlError(error),
4646
);

0 commit comments

Comments
 (0)