|
1 |
| -import { CleanupFn } from '@isograph/disposable-types'; |
| 1 | +import { CleanupFn, type ItemCleanupPair } from '@isograph/disposable-types'; |
2 | 2 | import {
|
3 | 3 | getParentRecordKey,
|
4 | 4 | insertIfNotExists,
|
@@ -196,6 +196,7 @@ function readData<TReadFromStore>(
|
196 | 196 | storeRecord,
|
197 | 197 | root,
|
198 | 198 | variables,
|
| 199 | + nestedRefetchQueries, |
199 | 200 | networkRequest,
|
200 | 201 | (ast, root) =>
|
201 | 202 | readData(
|
@@ -644,8 +645,8 @@ export function readLinkedFieldData(
|
644 | 645 | storeRecord: StoreRecord,
|
645 | 646 | root: Link,
|
646 | 647 | variables: Variables,
|
| 648 | + nestedRefetchQueries: RefetchQueryNormalizationArtifactWrapper[], |
647 | 649 | networkRequest: PromiseWrapper<void, any>,
|
648 |
| - |
649 | 650 | readData: <TReadFromStore>(
|
650 | 651 | ast: ReaderAst<TReadFromStore>,
|
651 | 652 | root: Link,
|
@@ -726,7 +727,7 @@ export function readLinkedFieldData(
|
726 | 727 | variables: generateChildVariableMap(
|
727 | 728 | variables,
|
728 | 729 | // TODO this is wrong
|
729 |
| - // should use field.condition.variables |
| 730 | + // should use field.arguments |
730 | 731 | // but it doesn't exist
|
731 | 732 | [],
|
732 | 733 | ),
|
@@ -797,6 +798,104 @@ export function readLinkedFieldData(
|
797 | 798 | };
|
798 | 799 | }
|
799 | 800 | 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 | + } |
800 | 899 | const data = readData(field.selections, targetId);
|
801 | 900 | if (data.kind === 'MissingData') {
|
802 | 901 | return {
|
|
0 commit comments