Skip to content

Commit 11441a8

Browse files
committed
add client pointers runtime
1 parent 276e7ab commit 11441a8

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

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

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CleanupFn } from '@isograph/disposable-types';
1+
import { CleanupFn, type ItemCleanupPair } from '@isograph/disposable-types';
22
import {
33
getParentRecordKey,
44
insertIfNotExists,
@@ -196,6 +196,7 @@ function readData<TReadFromStore>(
196196
storeRecord,
197197
root,
198198
variables,
199+
nestedRefetchQueries,
199200
networkRequest,
200201
(ast, root) =>
201202
readData(
@@ -644,8 +645,8 @@ export function readLinkedFieldData(
644645
storeRecord: StoreRecord,
645646
root: Link,
646647
variables: Variables,
648+
nestedRefetchQueries: RefetchQueryNormalizationArtifactWrapper[],
647649
networkRequest: PromiseWrapper<void, any>,
648-
649650
readData: <TReadFromStore>(
650651
ast: ReaderAst<TReadFromStore>,
651652
root: Link,
@@ -726,7 +727,7 @@ export function readLinkedFieldData(
726727
variables: generateChildVariableMap(
727728
variables,
728729
// TODO this is wrong
729-
// should use field.condition.variables
730+
// should use field.arguments
730731
// but it doesn't exist
731732
[],
732733
),
@@ -797,6 +798,104 @@ export function readLinkedFieldData(
797798
};
798799
}
799800
const targetId = link;
801+
if (typeof field.refetchQuery === 'number') {
802+
root = link;
803+
const refetchReaderParams = readData(
804+
[
805+
{
806+
kind: 'Scalar',
807+
fieldName: 'id',
808+
alias: null,
809+
arguments: null,
810+
isUpdatable: false,
811+
},
812+
],
813+
root,
814+
);
815+
816+
if (refetchReaderParams.kind === 'MissingData') {
817+
return {
818+
kind: 'MissingData',
819+
reason: 'Missing data for ' + field.alias + ' on root ' + root.__link,
820+
nestedReason: refetchReaderParams,
821+
recordLink: refetchReaderParams.recordLink,
822+
};
823+
}
824+
const refetchQueryIndex = field.refetchQuery;
825+
const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
826+
827+
if (refetchQuery == null) {
828+
throw new Error(
829+
'refetchQuery is null in RefetchField. This is indicative of a bug in Isograph.',
830+
);
831+
}
832+
const refetchQueryArtifact = refetchQuery.artifact;
833+
const allowedVariables = refetchQuery.allowedVariables;
834+
835+
return {
836+
kind: 'Success',
837+
data: (
838+
args: any,
839+
// TODO get the associated type for FetchOptions from the loadably selected field
840+
fetchOptions?: FetchOptions<any>,
841+
) => {
842+
const includeReadOutData = (variables: any, readOutData: any) => {
843+
variables.id = readOutData.id;
844+
return variables;
845+
};
846+
const localVariables = includeReadOutData(
847+
args ?? {},
848+
refetchReaderParams.data,
849+
);
850+
writeQueryArgsToVariables(localVariables, field.arguments, variables);
851+
852+
return [
853+
// Stable id
854+
root.__typename +
855+
':' +
856+
root.__link +
857+
'/' +
858+
field.fieldName +
859+
'/' +
860+
stableStringifyArgs(localVariables),
861+
// Fetcher
862+
(): ItemCleanupPair<FragmentReference<any, any>> | undefined => {
863+
const variables = includeReadOutData(
864+
filterVariables({ ...args, ...localVariables }, allowedVariables),
865+
refetchReaderParams.data,
866+
);
867+
868+
const [networkRequest, disposeNetworkRequest] =
869+
maybeMakeNetworkRequest(
870+
environment,
871+
refetchQueryArtifact,
872+
variables,
873+
fetchOptions,
874+
);
875+
876+
const fragmentReference: FragmentReference<any, any> = {
877+
kind: 'FragmentReference',
878+
readerWithRefetchQueries: wrapResolvedValue({
879+
kind: 'ReaderWithRefetchQueries',
880+
readerArtifact: {
881+
kind: 'EagerReaderArtifact',
882+
fieldName: field.fieldName,
883+
readerAst: field.selections,
884+
resolver: ({ data }) => data,
885+
hasUpdatable: false,
886+
},
887+
nestedRefetchQueries,
888+
}),
889+
root,
890+
variables,
891+
networkRequest,
892+
};
893+
return [fragmentReference, disposeNetworkRequest];
894+
},
895+
];
896+
},
897+
};
898+
}
800899
const data = readData(field.selections, targetId);
801900
if (data.kind === 'MissingData') {
802901
return {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export type ReaderLinkedField = {
113113
boolean | Link | null
114114
> | null;
115115
readonly isUpdatable: boolean;
116+
readonly refetchQuery: number | null;
116117
};
117118

118119
export type ReaderNonLoadableResolverField = {

0 commit comments

Comments
 (0)