Skip to content

Commit 9a34b9e

Browse files
committed
don't call .resolver if data has errors
1 parent b7d13c4 commit 9a34b9e

File tree

1 file changed

+85
-65
lines changed
  • libs/isograph-react/src/core

1 file changed

+85
-65
lines changed

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

Lines changed: 85 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
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';
@@ -344,9 +344,17 @@ export function readLoadablySelectedFieldData(
344344
};
345345
}
346346

347+
if (refetchReaderParams.errors) {
348+
return {
349+
kind: 'Success',
350+
errors: refetchReaderParams.errors,
351+
data: null,
352+
};
353+
}
354+
347355
return {
348356
kind: 'Success',
349-
errors: refetchReaderParams.errors,
357+
errors: null,
350358
data: (
351359
args: any,
352360
// TODO get the associated type for FetchOptions from the loadably selected field
@@ -623,6 +631,16 @@ export function readResolverFieldData(
623631
recordLink: data.recordLink,
624632
};
625633
}
634+
if (data.errors) {
635+
// We can't call the resolver when there are errors
636+
// the resolvers are semanticly non null so we can
637+
// return null when there's an error
638+
return {
639+
kind: 'Success',
640+
data: null,
641+
errors: data.errors,
642+
};
643+
}
626644
const firstParameter = {
627645
data: data.data,
628646
parameters: variables,
@@ -637,7 +655,7 @@ export function readResolverFieldData(
637655
};
638656
return {
639657
kind: 'Success',
640-
errors: data.errors,
658+
errors: null,
641659
data: field.readerArtifact.resolver(firstParameter),
642660
};
643661
}
@@ -713,9 +731,8 @@ export function readLinkedFieldData(
713731
const storeRecordName = getParentRecordKey(field, variables);
714732
const value = storeRecord.record[storeRecordName];
715733

716-
let errors: PayloadErrors | null = null;
717-
718734
if (Array.isArray(value)) {
735+
let errors: PayloadErrors | null = null;
719736
const results = [];
720737
for (const item of value) {
721738
const link = assertLink(item);
@@ -779,55 +796,55 @@ export function readLinkedFieldData(
779796
recordLink: data.recordLink,
780797
};
781798
}
782-
if (errors === null) {
783-
errors = data.errors;
784-
} else if (data.errors) {
785-
errors.push(...data.errors);
786-
}
787799

788-
if (refetchQueryIndex === null || data.errors === null) {
789-
// we can't call `condition.resolver` for client pointers when there are errors,
790-
// for inline fragment this is fine
800+
if (data.errors && refetchQueryIndex !== null) {
801+
// we can't call resolver for client pointers when there are errors,
802+
// but for inline fragments this is fine
803+
return {
804+
kind: 'Success',
805+
data: null,
806+
errors: data.errors,
807+
};
808+
}
791809

792-
const readerWithRefetchQueries = {
793-
kind: 'ReaderWithRefetchQueries',
794-
readerArtifact: field.condition,
810+
const readerWithRefetchQueries = {
811+
kind: 'ReaderWithRefetchQueries',
812+
readerArtifact: field.condition,
813+
// TODO this is wrong
814+
// should map field.condition.usedRefetchQueries
815+
// but it doesn't exist
816+
nestedRefetchQueries: [],
817+
} satisfies ReaderWithRefetchQueries<any, any>;
818+
819+
const fragment = {
820+
kind: 'FragmentReference',
821+
readerWithRefetchQueries: wrapResolvedValue(readerWithRefetchQueries),
822+
root,
823+
variables: generateChildVariableMap(
824+
variables,
795825
// TODO this is wrong
796-
// should map field.condition.usedRefetchQueries
826+
// should use field.arguments
797827
// but it doesn't exist
798-
nestedRefetchQueries: [],
799-
} satisfies ReaderWithRefetchQueries<any, any>;
800-
801-
const fragment = {
802-
kind: 'FragmentReference',
803-
readerWithRefetchQueries: wrapResolvedValue(readerWithRefetchQueries),
804-
root,
805-
variables: generateChildVariableMap(
806-
variables,
807-
// TODO this is wrong
808-
// should use field.arguments
809-
// but it doesn't exist
810-
[],
811-
),
812-
networkRequest,
813-
} satisfies FragmentReference<any, any>;
814-
815-
const condition = field.condition.resolver({
816-
data: data.data,
817-
parameters: {},
818-
...(field.condition.hasUpdatable
819-
? {
820-
startUpdate: getOrCreateCachedStartUpdate(
821-
environment,
822-
fragment,
823-
readerWithRefetchQueries.readerArtifact.fieldName,
824-
networkRequestOptions,
825-
),
826-
}
827-
: undefined),
828-
});
829-
link = condition;
830-
}
828+
[],
829+
),
830+
networkRequest,
831+
} satisfies FragmentReference<any, any>;
832+
833+
const condition = field.condition.resolver({
834+
data: data.data,
835+
parameters: {},
836+
...(field.condition.hasUpdatable
837+
? {
838+
startUpdate: getOrCreateCachedStartUpdate(
839+
environment,
840+
fragment,
841+
readerWithRefetchQueries.readerArtifact.fieldName,
842+
networkRequestOptions,
843+
),
844+
}
845+
: undefined),
846+
});
847+
link = condition;
831848
}
832849

833850
if (link === undefined) {
@@ -866,6 +883,7 @@ export function readLinkedFieldData(
866883
link = altLink;
867884
}
868885
} else if (link === null) {
886+
let errors: PayloadErrors | null = null;
869887
if (storeRecord.errors[storeRecordName]) {
870888
errors = storeRecord.errors[storeRecordName];
871889
}
@@ -902,10 +920,12 @@ export function readLinkedFieldData(
902920
recordLink: refetchReaderParams.recordLink,
903921
};
904922
}
905-
if (errors === null) {
906-
errors = refetchReaderParams.errors;
907-
} else if (refetchReaderParams.errors) {
908-
errors.push(...refetchReaderParams.errors);
923+
if (refetchReaderParams.errors) {
924+
return {
925+
kind: 'Success',
926+
data: null,
927+
errors: refetchReaderParams.errors,
928+
};
909929
}
910930

911931
const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
@@ -919,7 +939,7 @@ export function readLinkedFieldData(
919939

920940
return {
921941
kind: 'Success',
922-
errors,
942+
errors: null,
923943
data: (
924944
args,
925945
// TODO get the associated type for FetchOptions from the loadably selected field
@@ -994,15 +1014,7 @@ export function readLinkedFieldData(
9941014
recordLink: data.recordLink,
9951015
};
9961016
}
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-
};
1017+
return data;
10061018
}
10071019

10081020
export type NetworkRequestReaderOptions = {
@@ -1068,6 +1080,14 @@ export function readImperativelyLoadedField(
10681080
};
10691081
}
10701082

1083+
if (data.errors) {
1084+
return {
1085+
kind: 'Success',
1086+
data: null,
1087+
errors: data.errors,
1088+
};
1089+
}
1090+
10711091
const { refetchQueryIndex } = field;
10721092
const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
10731093
if (refetchQuery == null) {
@@ -1082,7 +1102,7 @@ export function readImperativelyLoadedField(
10821102
// use the resolver reader AST to get the resolver parameters.
10831103
return {
10841104
kind: 'Success',
1085-
errors: data.errors,
1105+
errors: null,
10861106
data: (args: any) => [
10871107
// Stable id
10881108
root.__typename + ':' + root.__link + '__' + field.name,

0 commit comments

Comments
 (0)