@isograph/react 0.0.0-main-905800be → 0.0.0-main-0e7d8883

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/read.ts ADDED
@@ -0,0 +1,336 @@
1
+ import { getParentRecordKey, onNextChange } from './cache';
2
+ import { getOrCreateCachedComponent } from './componentCache';
3
+ import { RefetchQueryArtifactWrapper } from './entrypoint';
4
+ import { assertLink, FragmentReference, ReaderAst } from './index';
5
+ import {
6
+ DataId,
7
+ defaultMissingFieldHandler,
8
+ IsographEnvironment,
9
+ } from './IsographEnvironment';
10
+
11
+ export function read<TReadFromStore extends Object, TResolverResult>(
12
+ environment: IsographEnvironment,
13
+ fragmentReference: FragmentReference<TReadFromStore, TResolverResult>,
14
+ ): TResolverResult {
15
+ const variant = fragmentReference.readerArtifact.variant;
16
+ if (variant.kind === 'Eager') {
17
+ const data = readData(
18
+ environment,
19
+ fragmentReference.readerArtifact.readerAst,
20
+ fragmentReference.root,
21
+ fragmentReference.variables ?? {},
22
+ fragmentReference.nestedRefetchQueries,
23
+ );
24
+ if (data.kind === 'MissingData') {
25
+ throw onNextChange(environment);
26
+ } else {
27
+ // @ts-expect-error This not properly typed yet
28
+ return fragmentReference.readerArtifact.resolver(data.data);
29
+ }
30
+ } else if (variant.kind === 'Component') {
31
+ // @ts-ignore
32
+ return getOrCreateCachedComponent(
33
+ environment,
34
+ fragmentReference.root,
35
+ variant.componentName,
36
+ fragmentReference.readerArtifact,
37
+ fragmentReference.variables ?? {},
38
+ fragmentReference.nestedRefetchQueries,
39
+ );
40
+ }
41
+ // Why can't Typescript realize that this is unreachable??
42
+ throw new Error('This is unreachable');
43
+ }
44
+
45
+ export function readButDoNotEvaluate<TReadFromStore extends Object>(
46
+ environment: IsographEnvironment,
47
+ reference: FragmentReference<TReadFromStore, unknown>,
48
+ ): TReadFromStore {
49
+ const response = readData(
50
+ environment,
51
+ reference.readerArtifact.readerAst,
52
+ reference.root,
53
+ reference.variables ?? {},
54
+ reference.nestedRefetchQueries,
55
+ );
56
+ if (typeof window !== 'undefined' && window.__LOG) {
57
+ console.log('done reading', { response });
58
+ }
59
+ if (response.kind === 'MissingData') {
60
+ throw onNextChange(environment);
61
+ } else {
62
+ return response.data;
63
+ }
64
+ }
65
+
66
+ type ReadDataResult<TReadFromStore> =
67
+ | {
68
+ kind: 'Success';
69
+ data: TReadFromStore;
70
+ }
71
+ | {
72
+ kind: 'MissingData';
73
+ reason: string;
74
+ nestedReason?: ReadDataResult<unknown>;
75
+ };
76
+
77
+ function readData<TReadFromStore>(
78
+ environment: IsographEnvironment,
79
+ ast: ReaderAst<TReadFromStore>,
80
+ root: DataId,
81
+ variables: { [index: string]: string },
82
+ nestedRefetchQueries: RefetchQueryArtifactWrapper[],
83
+ ): ReadDataResult<TReadFromStore> {
84
+ let storeRecord = environment.store[root];
85
+ if (storeRecord === undefined) {
86
+ return { kind: 'MissingData', reason: 'No record for root ' + root };
87
+ }
88
+
89
+ if (storeRecord === null) {
90
+ return { kind: 'Success', data: null as any };
91
+ }
92
+
93
+ let target: { [index: string]: any } = {};
94
+
95
+ for (const field of ast) {
96
+ switch (field.kind) {
97
+ case 'Scalar': {
98
+ const storeRecordName = getParentRecordKey(field, variables);
99
+ const value = storeRecord[storeRecordName];
100
+ // TODO consider making scalars into discriminated unions. This probably has
101
+ // to happen for when we handle errors.
102
+ if (value === undefined) {
103
+ return {
104
+ kind: 'MissingData',
105
+ reason: 'No value for ' + storeRecordName + ' on root ' + root,
106
+ };
107
+ }
108
+ target[field.alias ?? field.fieldName] = value;
109
+ break;
110
+ }
111
+ case 'Linked': {
112
+ const storeRecordName = getParentRecordKey(field, variables);
113
+ const value = storeRecord[storeRecordName];
114
+ if (Array.isArray(value)) {
115
+ const results = [];
116
+ for (const item of value) {
117
+ const link = assertLink(item);
118
+ if (link === undefined) {
119
+ return {
120
+ kind: 'MissingData',
121
+ reason:
122
+ 'No link for ' +
123
+ storeRecordName +
124
+ ' on root ' +
125
+ root +
126
+ '. Link is ' +
127
+ JSON.stringify(item),
128
+ };
129
+ } else if (link === null) {
130
+ results.push(null);
131
+ continue;
132
+ }
133
+ const result = readData(
134
+ environment,
135
+ field.selections,
136
+ link.__link,
137
+ variables,
138
+ nestedRefetchQueries,
139
+ );
140
+ if (result.kind === 'MissingData') {
141
+ return {
142
+ kind: 'MissingData',
143
+ reason:
144
+ 'Missing data for ' +
145
+ storeRecordName +
146
+ ' on root ' +
147
+ root +
148
+ '. Link is ' +
149
+ JSON.stringify(item),
150
+ nestedReason: result,
151
+ };
152
+ }
153
+ results.push(result.data);
154
+ }
155
+ target[field.alias ?? field.fieldName] = results;
156
+ break;
157
+ }
158
+ let link = assertLink(value);
159
+ if (link === undefined) {
160
+ // TODO make this configurable, and also generated and derived from the schema
161
+ const missingFieldHandler =
162
+ environment.missingFieldHandler ?? defaultMissingFieldHandler;
163
+ const altLink = missingFieldHandler(
164
+ storeRecord,
165
+ root,
166
+ field.fieldName,
167
+ field.arguments,
168
+ variables,
169
+ );
170
+ if (altLink === undefined) {
171
+ return {
172
+ kind: 'MissingData',
173
+ reason:
174
+ 'No link for ' +
175
+ storeRecordName +
176
+ ' on root ' +
177
+ root +
178
+ '. Link is ' +
179
+ JSON.stringify(value),
180
+ };
181
+ } else {
182
+ link = altLink;
183
+ }
184
+ } else if (link === null) {
185
+ target[field.alias ?? field.fieldName] = null;
186
+ break;
187
+ }
188
+ const targetId = link.__link;
189
+ const data = readData(
190
+ environment,
191
+ field.selections,
192
+ targetId,
193
+ variables,
194
+ nestedRefetchQueries,
195
+ );
196
+ if (data.kind === 'MissingData') {
197
+ return {
198
+ kind: 'MissingData',
199
+ reason: 'Missing data for ' + storeRecordName + ' on root ' + root,
200
+ nestedReason: data,
201
+ };
202
+ }
203
+ target[field.alias ?? field.fieldName] = data.data;
204
+ break;
205
+ }
206
+ case 'RefetchField': {
207
+ const data = readData(
208
+ environment,
209
+ field.readerArtifact.readerAst,
210
+ root,
211
+ variables,
212
+ // Refetch fields just read the id, and don't need refetch query artifacts
213
+ [],
214
+ );
215
+ if (typeof window !== 'undefined' && window.__LOG) {
216
+ console.log('refetch field data', data, field);
217
+ }
218
+ if (data.kind === 'MissingData') {
219
+ return {
220
+ kind: 'MissingData',
221
+ reason: 'Missing data for ' + field.alias + ' on root ' + root,
222
+ nestedReason: data,
223
+ };
224
+ } else {
225
+ const refetchQueryIndex = field.refetchQuery;
226
+ if (refetchQueryIndex == null) {
227
+ throw new Error('refetchQuery is null in RefetchField');
228
+ }
229
+ const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
230
+ const refetchQueryArtifact = refetchQuery.artifact;
231
+ const allowedVariables = refetchQuery.allowedVariables;
232
+
233
+ target[field.alias] = field.readerArtifact.resolver(
234
+ environment,
235
+ // resolvers for refetch fields take 3 args, and this is not reflected in types
236
+ refetchQueryArtifact,
237
+ // @ts-expect-error
238
+ {
239
+ ...data.data,
240
+ // TODO continue from here
241
+ // variables need to be filtered for what we need just for the refetch query
242
+ ...filterVariables(variables, allowedVariables),
243
+ },
244
+ );
245
+ }
246
+ break;
247
+ }
248
+ case 'MutationField': {
249
+ const data = readData(
250
+ environment,
251
+ field.readerArtifact.readerAst,
252
+ root,
253
+ variables,
254
+ // Refetch fields just read the id, and don't need refetch query artifacts
255
+ [],
256
+ );
257
+ if (typeof window !== 'undefined' && window.__LOG) {
258
+ console.log('refetch field data', data, field);
259
+ }
260
+ if (data.kind === 'MissingData') {
261
+ return {
262
+ kind: 'MissingData',
263
+ reason: 'Missing data for ' + field.alias + ' on root ' + root,
264
+ nestedReason: data,
265
+ };
266
+ } else {
267
+ const refetchQueryIndex = field.refetchQuery;
268
+ if (refetchQueryIndex == null) {
269
+ throw new Error('refetchQuery is null in MutationField');
270
+ }
271
+ const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
272
+ const refetchQueryArtifact = refetchQuery.artifact;
273
+ const allowedVariables = refetchQuery.allowedVariables;
274
+
275
+ target[field.alias] = field.readerArtifact.resolver(
276
+ environment,
277
+ refetchQueryArtifact,
278
+ // @ts-expect-error
279
+ data.data,
280
+ filterVariables(variables, allowedVariables),
281
+ );
282
+ }
283
+ break;
284
+ }
285
+ case 'Resolver': {
286
+ const usedRefetchQueries = field.usedRefetchQueries;
287
+ const resolverRefetchQueries = usedRefetchQueries.map(
288
+ (index) => nestedRefetchQueries[index],
289
+ );
290
+
291
+ const variant = field.readerArtifact.variant;
292
+ if (variant.kind === 'Eager') {
293
+ const data = readData(
294
+ environment,
295
+ field.readerArtifact.readerAst,
296
+ root,
297
+ variables,
298
+ resolverRefetchQueries,
299
+ );
300
+ if (data.kind === 'MissingData') {
301
+ return {
302
+ kind: 'MissingData',
303
+ reason: 'Missing data for ' + field.alias + ' on root ' + root,
304
+ nestedReason: data,
305
+ };
306
+ } else {
307
+ // @ts-expect-error
308
+ target[field.alias] = field.readerArtifact.resolver(data.data);
309
+ }
310
+ } else if (variant.kind === 'Component') {
311
+ target[field.alias] = getOrCreateCachedComponent(
312
+ environment,
313
+ root,
314
+ variant.componentName,
315
+ field.readerArtifact,
316
+ variables,
317
+ resolverRefetchQueries,
318
+ );
319
+ }
320
+ break;
321
+ }
322
+ }
323
+ }
324
+ return { kind: 'Success', data: target as any };
325
+ }
326
+
327
+ function filterVariables(
328
+ variables: { [index: string]: string },
329
+ allowedVariables: string[],
330
+ ): { [index: string]: string } {
331
+ const result: { [index: string]: string } = {};
332
+ for (const key of allowedVariables) {
333
+ result[key] = variables[key];
334
+ }
335
+ return result;
336
+ }
@@ -0,0 +1,20 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { FragmentReference } from './index';
3
+ import { useIsographEnvironment } from './IsographEnvironmentProvider';
4
+ import { subscribe } from './cache';
5
+ import { read } from './read';
6
+
7
+ export function useResult<TReadFromStore extends Object, TResolverResult>(
8
+ fragmentReference: FragmentReference<TReadFromStore, TResolverResult>,
9
+ ): TResolverResult {
10
+ const environment = useIsographEnvironment();
11
+
12
+ const [, setState] = useState<object | void>();
13
+ useEffect(() => {
14
+ return subscribe(environment, () => {
15
+ return setState({});
16
+ });
17
+ }, []);
18
+
19
+ return read(environment, fragmentReference);
20
+ }