@isograph/react 0.0.0-main-90264a00 → 0.0.0-main-2fbc0faf

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/dist/read.js ADDED
@@ -0,0 +1,233 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readButDoNotEvaluate = exports.read = void 0;
4
+ const cache_1 = require("./cache");
5
+ const componentCache_1 = require("./componentCache");
6
+ const IsographEnvironment_1 = require("./IsographEnvironment");
7
+ function read(environment, fragmentReference) {
8
+ var _a, _b;
9
+ const variant = fragmentReference.readerArtifact.variant;
10
+ if (variant.kind === 'Eager') {
11
+ const data = readData(environment, fragmentReference.readerArtifact.readerAst, fragmentReference.root, (_a = fragmentReference.variables) !== null && _a !== void 0 ? _a : {}, fragmentReference.nestedRefetchQueries);
12
+ if (data.kind === 'MissingData') {
13
+ throw (0, cache_1.onNextChange)(environment);
14
+ }
15
+ else {
16
+ // @ts-expect-error This not properly typed yet
17
+ return fragmentReference.readerArtifact.resolver(data.data);
18
+ }
19
+ }
20
+ else if (variant.kind === 'Component') {
21
+ // @ts-ignore
22
+ return (0, componentCache_1.getOrCreateCachedComponent)(environment, fragmentReference.root, variant.componentName, fragmentReference.readerArtifact, (_b = fragmentReference.variables) !== null && _b !== void 0 ? _b : {}, fragmentReference.nestedRefetchQueries);
23
+ }
24
+ // Why can't Typescript realize that this is unreachable??
25
+ throw new Error('This is unreachable');
26
+ }
27
+ exports.read = read;
28
+ function readButDoNotEvaluate(environment, reference) {
29
+ var _a;
30
+ const response = readData(environment, reference.readerArtifact.readerAst, reference.root, (_a = reference.variables) !== null && _a !== void 0 ? _a : {}, reference.nestedRefetchQueries);
31
+ if (typeof window !== 'undefined' && window.__LOG) {
32
+ console.log('done reading', { response });
33
+ }
34
+ if (response.kind === 'MissingData') {
35
+ throw (0, cache_1.onNextChange)(environment);
36
+ }
37
+ else {
38
+ return response.data;
39
+ }
40
+ }
41
+ exports.readButDoNotEvaluate = readButDoNotEvaluate;
42
+ function readData(environment, ast, root, variables, nestedRefetchQueries) {
43
+ var _a, _b, _c, _d, _e;
44
+ let storeRecord = environment.store[root];
45
+ if (storeRecord === undefined) {
46
+ return { kind: 'MissingData', reason: 'No record for root ' + root };
47
+ }
48
+ if (storeRecord === null) {
49
+ return { kind: 'Success', data: null };
50
+ }
51
+ let target = {};
52
+ for (const field of ast) {
53
+ switch (field.kind) {
54
+ case 'Scalar': {
55
+ const storeRecordName = (0, cache_1.getParentRecordKey)(field, variables);
56
+ const value = storeRecord[storeRecordName];
57
+ // TODO consider making scalars into discriminated unions. This probably has
58
+ // to happen for when we handle errors.
59
+ if (value === undefined) {
60
+ return {
61
+ kind: 'MissingData',
62
+ reason: 'No value for ' + storeRecordName + ' on root ' + root,
63
+ };
64
+ }
65
+ target[(_a = field.alias) !== null && _a !== void 0 ? _a : field.fieldName] = value;
66
+ break;
67
+ }
68
+ case 'Linked': {
69
+ const storeRecordName = (0, cache_1.getParentRecordKey)(field, variables);
70
+ const value = storeRecord[storeRecordName];
71
+ if (Array.isArray(value)) {
72
+ const results = [];
73
+ for (const item of value) {
74
+ const link = (0, IsographEnvironment_1.assertLink)(item);
75
+ if (link === undefined) {
76
+ return {
77
+ kind: 'MissingData',
78
+ reason: 'No link for ' +
79
+ storeRecordName +
80
+ ' on root ' +
81
+ root +
82
+ '. Link is ' +
83
+ JSON.stringify(item),
84
+ };
85
+ }
86
+ else if (link === null) {
87
+ results.push(null);
88
+ continue;
89
+ }
90
+ const result = readData(environment, field.selections, link.__link, variables, nestedRefetchQueries);
91
+ if (result.kind === 'MissingData') {
92
+ return {
93
+ kind: 'MissingData',
94
+ reason: 'Missing data for ' +
95
+ storeRecordName +
96
+ ' on root ' +
97
+ root +
98
+ '. Link is ' +
99
+ JSON.stringify(item),
100
+ nestedReason: result,
101
+ };
102
+ }
103
+ results.push(result.data);
104
+ }
105
+ target[(_b = field.alias) !== null && _b !== void 0 ? _b : field.fieldName] = results;
106
+ break;
107
+ }
108
+ let link = (0, IsographEnvironment_1.assertLink)(value);
109
+ if (link === undefined) {
110
+ // TODO make this configurable, and also generated and derived from the schema
111
+ const missingFieldHandler = (_c = environment.missingFieldHandler) !== null && _c !== void 0 ? _c : IsographEnvironment_1.defaultMissingFieldHandler;
112
+ const altLink = missingFieldHandler(storeRecord, root, field.fieldName, field.arguments, variables);
113
+ if (altLink === undefined) {
114
+ return {
115
+ kind: 'MissingData',
116
+ reason: 'No link for ' +
117
+ storeRecordName +
118
+ ' on root ' +
119
+ root +
120
+ '. Link is ' +
121
+ JSON.stringify(value),
122
+ };
123
+ }
124
+ else {
125
+ link = altLink;
126
+ }
127
+ }
128
+ else if (link === null) {
129
+ target[(_d = field.alias) !== null && _d !== void 0 ? _d : field.fieldName] = null;
130
+ break;
131
+ }
132
+ const targetId = link.__link;
133
+ const data = readData(environment, field.selections, targetId, variables, nestedRefetchQueries);
134
+ if (data.kind === 'MissingData') {
135
+ return {
136
+ kind: 'MissingData',
137
+ reason: 'Missing data for ' + storeRecordName + ' on root ' + root,
138
+ nestedReason: data,
139
+ };
140
+ }
141
+ target[(_e = field.alias) !== null && _e !== void 0 ? _e : field.fieldName] = data.data;
142
+ break;
143
+ }
144
+ case 'RefetchField': {
145
+ const data = readData(environment, field.readerArtifact.readerAst, root, variables,
146
+ // Refetch fields just read the id, and don't need refetch query artifacts
147
+ []);
148
+ if (typeof window !== 'undefined' && window.__LOG) {
149
+ console.log('refetch field data', data, field);
150
+ }
151
+ if (data.kind === 'MissingData') {
152
+ return {
153
+ kind: 'MissingData',
154
+ reason: 'Missing data for ' + field.alias + ' on root ' + root,
155
+ nestedReason: data,
156
+ };
157
+ }
158
+ else {
159
+ const refetchQueryIndex = field.refetchQuery;
160
+ if (refetchQueryIndex == null) {
161
+ throw new Error('refetchQuery is null in RefetchField');
162
+ }
163
+ const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
164
+ const refetchQueryArtifact = refetchQuery.artifact;
165
+ const allowedVariables = refetchQuery.allowedVariables;
166
+ target[field.alias] = field.readerArtifact.resolver(environment,
167
+ // resolvers for refetch fields take 3 args, and this is not reflected in types
168
+ refetchQueryArtifact, Object.assign(Object.assign({}, data.data), filterVariables(variables, allowedVariables)));
169
+ }
170
+ break;
171
+ }
172
+ case 'MutationField': {
173
+ const data = readData(environment, field.readerArtifact.readerAst, root, variables,
174
+ // Refetch fields just read the id, and don't need refetch query artifacts
175
+ []);
176
+ if (typeof window !== 'undefined' && window.__LOG) {
177
+ console.log('refetch field data', data, field);
178
+ }
179
+ if (data.kind === 'MissingData') {
180
+ return {
181
+ kind: 'MissingData',
182
+ reason: 'Missing data for ' + field.alias + ' on root ' + root,
183
+ nestedReason: data,
184
+ };
185
+ }
186
+ else {
187
+ const refetchQueryIndex = field.refetchQuery;
188
+ if (refetchQueryIndex == null) {
189
+ throw new Error('refetchQuery is null in MutationField');
190
+ }
191
+ const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
192
+ const refetchQueryArtifact = refetchQuery.artifact;
193
+ const allowedVariables = refetchQuery.allowedVariables;
194
+ target[field.alias] = field.readerArtifact.resolver(environment, refetchQueryArtifact,
195
+ // @ts-expect-error
196
+ data.data, filterVariables(variables, allowedVariables));
197
+ }
198
+ break;
199
+ }
200
+ case 'Resolver': {
201
+ const usedRefetchQueries = field.usedRefetchQueries;
202
+ const resolverRefetchQueries = usedRefetchQueries.map((index) => nestedRefetchQueries[index]);
203
+ const variant = field.readerArtifact.variant;
204
+ if (variant.kind === 'Eager') {
205
+ const data = readData(environment, field.readerArtifact.readerAst, root, variables, resolverRefetchQueries);
206
+ if (data.kind === 'MissingData') {
207
+ return {
208
+ kind: 'MissingData',
209
+ reason: 'Missing data for ' + field.alias + ' on root ' + root,
210
+ nestedReason: data,
211
+ };
212
+ }
213
+ else {
214
+ // @ts-expect-error
215
+ target[field.alias] = field.readerArtifact.resolver(data.data);
216
+ }
217
+ }
218
+ else if (variant.kind === 'Component') {
219
+ target[field.alias] = (0, componentCache_1.getOrCreateCachedComponent)(environment, root, variant.componentName, field.readerArtifact, variables, resolverRefetchQueries);
220
+ }
221
+ break;
222
+ }
223
+ }
224
+ }
225
+ return { kind: 'Success', data: target };
226
+ }
227
+ function filterVariables(variables, allowedVariables) {
228
+ const result = {};
229
+ for (const key of allowedVariables) {
230
+ result[key] = variables[key];
231
+ }
232
+ return result;
233
+ }
package/dist/reader.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Arguments } from './index';
1
+ import { Arguments } from './util';
2
2
  export type ReaderArtifact<TReadFromStore extends Object, TResolverResult> = {
3
3
  kind: 'ReaderArtifact';
4
4
  readerAst: ReaderAst<TReadFromStore>;
@@ -1,5 +1,6 @@
1
1
  import { UnassignedState } from '@isograph/react-disposable-state';
2
- import { type IsographEntrypoint, type FragmentReference, ExtractReadFromStore, ExtractResolverResult } from './index';
2
+ import { ExtractReadFromStore, ExtractResolverResult, IsographEntrypoint } from './entrypoint';
3
+ import { FragmentReference } from './FragmentReference';
3
4
  export declare function useImperativeReference<TEntrypoint extends IsographEntrypoint<any, any>>(entrypoint: TEntrypoint): {
4
5
  queryReference: FragmentReference<ExtractReadFromStore<TEntrypoint>, ExtractResolverResult<TEntrypoint>> | UnassignedState;
5
6
  loadQueryReference: (variables: {
@@ -2,19 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useImperativeReference = void 0;
4
4
  const react_disposable_state_1 = require("@isograph/react-disposable-state");
5
- const index_1 = require("./index");
5
+ const IsographEnvironmentProvider_1 = require("./IsographEnvironmentProvider");
6
+ const cache_1 = require("./cache");
7
+ const IsographEnvironment_1 = require("./IsographEnvironment");
6
8
  function useImperativeReference(entrypoint) {
7
9
  const { state, setState } = (0, react_disposable_state_1.useUpdatableDisposableState)();
8
- const environment = (0, index_1.useIsographEnvironment)();
10
+ const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
9
11
  return {
10
12
  queryReference: state,
11
13
  loadQueryReference: (variables) => {
12
- const [_networkRequest, disposeNetworkRequest] = (0, index_1.makeNetworkRequest)(environment, entrypoint, variables);
14
+ const [_networkRequest, disposeNetworkRequest] = (0, cache_1.makeNetworkRequest)(environment, entrypoint, variables);
13
15
  setState([
14
16
  {
15
17
  kind: 'FragmentReference',
16
18
  readerArtifact: entrypoint.readerArtifact,
17
- root: index_1.ROOT_ID,
19
+ root: IsographEnvironment_1.ROOT_ID,
18
20
  variables,
19
21
  nestedRefetchQueries: entrypoint.nestedRefetchQueries,
20
22
  },
@@ -0,0 +1,7 @@
1
+ import { FragmentReference, Variable } from './FragmentReference';
2
+ import { ExtractReadFromStore, ExtractResolverResult } from './entrypoint';
3
+ export declare function useLazyReference<TEntrypoint>(entrypoint: TEntrypoint | ((_: any) => any), variables: {
4
+ [key: string]: Variable;
5
+ }): {
6
+ queryReference: FragmentReference<ExtractReadFromStore<TEntrypoint>, ExtractResolverResult<TEntrypoint>>;
7
+ };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useLazyReference = void 0;
4
+ const IsographEnvironmentProvider_1 = require("./IsographEnvironmentProvider");
5
+ const IsographEnvironment_1 = require("./IsographEnvironment");
6
+ const entrypoint_1 = require("./entrypoint");
7
+ const cache_1 = require("./cache");
8
+ const isograph_react_disposable_state_1 = require("@isograph/isograph-react-disposable-state");
9
+ // Note: we cannot write TEntrypoint extends IsographEntrypoint<any, any, any>, or else
10
+ // if we do not explicitly pass a type, the read out type will be any.
11
+ // We cannot write TEntrypoint extends IsographEntrypoint<never, never, never>, or else
12
+ // any actual Entrypoint we pass will not be valid.
13
+ function useLazyReference(entrypoint, variables) {
14
+ const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
15
+ (0, entrypoint_1.assertIsEntrypoint)(entrypoint);
16
+ const cache = (0, cache_1.getOrCreateCacheForArtifact)(environment, entrypoint, variables);
17
+ // TODO add comment explaining why we never use this value
18
+ // @ts-ignore
19
+ const data = (0, isograph_react_disposable_state_1.useLazyDisposableState)(cache).state;
20
+ return {
21
+ queryReference: {
22
+ kind: 'FragmentReference',
23
+ readerArtifact: entrypoint.readerArtifact,
24
+ root: IsographEnvironment_1.ROOT_ID,
25
+ variables,
26
+ nestedRefetchQueries: entrypoint.nestedRefetchQueries,
27
+ },
28
+ };
29
+ }
30
+ exports.useLazyReference = useLazyReference;
@@ -0,0 +1,2 @@
1
+ import { FragmentReference } from './FragmentReference';
2
+ export declare function useResult<TReadFromStore extends Object, TResolverResult>(fragmentReference: FragmentReference<TReadFromStore, TResolverResult>): TResolverResult;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useResult = void 0;
4
+ const react_1 = require("react");
5
+ const IsographEnvironmentProvider_1 = require("./IsographEnvironmentProvider");
6
+ const cache_1 = require("./cache");
7
+ const read_1 = require("./read");
8
+ function useResult(fragmentReference) {
9
+ const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
10
+ const [, setState] = (0, react_1.useState)();
11
+ (0, react_1.useEffect)(() => {
12
+ return (0, cache_1.subscribe)(environment, () => {
13
+ return setState({});
14
+ });
15
+ }, []);
16
+ return (0, read_1.read)(environment, fragmentReference);
17
+ }
18
+ exports.useResult = useResult;
package/dist/util.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export type ExtractSecondParam<T extends (arg1: any, arg2: any) => any> = T extends (arg1: any, arg2: infer P) => any ? P : never;
2
+ export type Arguments = Argument[];
3
+ export type Argument = [ArgumentName, ArgumentValue];
4
+ export type ArgumentName = string;
5
+ export type ArgumentValue = {
6
+ kind: 'Variable';
7
+ name: string;
8
+ } | {
9
+ kind: 'Literal';
10
+ value: any;
11
+ };
package/dist/util.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isograph/react",
3
- "version": "0.0.0-main-90264a00",
3
+ "version": "0.0.0-main-2fbc0faf",
4
4
  "description": "Use Isograph with React",
5
5
  "homepage": "https://isograph.dev",
6
6
  "main": "dist/index.js",
@@ -16,8 +16,8 @@
16
16
  "prepack": "yarn run test && yarn run compile"
17
17
  },
18
18
  "dependencies": {
19
- "@isograph/disposable-types": "0.0.0-main-90264a00",
20
- "@isograph/react-disposable-state": "0.0.0-main-90264a00",
19
+ "@isograph/disposable-types": "0.0.0-main-2fbc0faf",
20
+ "@isograph/react-disposable-state": "0.0.0-main-2fbc0faf",
21
21
  "react": "^18.2.0"
22
22
  },
23
23
  "devDependencies": {
@@ -1,10 +1,7 @@
1
1
  import * as React from 'react';
2
- import {
3
- ExtractReadFromStore,
4
- IsographEntrypoint,
5
- type FragmentReference,
6
- useResult,
7
- } from './index';
2
+ import { ExtractReadFromStore, IsographEntrypoint } from './entrypoint';
3
+ import { FragmentReference } from './FragmentReference';
4
+ import { useResult } from './useResult';
8
5
 
9
6
  export function EntrypointReader<
10
7
  TProps extends Record<any, any>,
@@ -0,0 +1,18 @@
1
+ import { DataId } from './IsographEnvironment';
2
+ import { RefetchQueryArtifactWrapper } from './entrypoint';
3
+ import { ReaderArtifact } from './reader';
4
+
5
+ // TODO type this better
6
+ export type Variable = any;
7
+
8
+ export type FragmentReference<
9
+ TReadFromStore extends Object,
10
+ TResolverResult,
11
+ > = {
12
+ kind: 'FragmentReference';
13
+ readerArtifact: ReaderArtifact<TReadFromStore, TResolverResult>;
14
+ root: DataId;
15
+ variables: { [index: string]: Variable } | null;
16
+ // TODO: We should instead have ReaderAst<TResolverProps>
17
+ nestedRefetchQueries: RefetchQueryArtifactWrapper[];
18
+ };
@@ -113,3 +113,16 @@ export function defaultMissingFieldHandler(
113
113
  }
114
114
  }
115
115
  }
116
+
117
+ export function assertLink(link: DataTypeValue): Link | null {
118
+ if (Array.isArray(link)) {
119
+ throw new Error('Unexpected array');
120
+ }
121
+ if (link == null) {
122
+ return null;
123
+ }
124
+ if (typeof link === 'object') {
125
+ return link;
126
+ }
127
+ throw new Error('Invalid link');
128
+ }
package/src/cache.tsx CHANGED
@@ -4,17 +4,6 @@ import {
4
4
  ParentCache,
5
5
  } from '@isograph/react-disposable-state';
6
6
  import { PromiseWrapper, wrapPromise } from './PromiseWrapper';
7
- import {
8
- Argument,
9
- ArgumentValue,
10
- IsographEntrypoint,
11
- NormalizationAst,
12
- NormalizationLinkedField,
13
- NormalizationScalarField,
14
- ReaderLinkedField,
15
- ReaderScalarField,
16
- RefetchQueryArtifactWrapper,
17
- } from './index';
18
7
  import {
19
8
  DataId,
20
9
  ROOT_ID,
@@ -28,6 +17,15 @@ import {
28
17
  retainQuery,
29
18
  unretainQuery,
30
19
  } from './garbageCollection';
20
+ import {
21
+ IsographEntrypoint,
22
+ NormalizationAst,
23
+ NormalizationLinkedField,
24
+ NormalizationScalarField,
25
+ RefetchQueryArtifactWrapper,
26
+ } from './entrypoint';
27
+ import { ReaderLinkedField, ReaderScalarField } from './reader';
28
+ import { Argument, ArgumentValue } from './util';
31
29
 
32
30
  declare global {
33
31
  interface Window {
@@ -1,10 +1,8 @@
1
- import {
2
- ReaderArtifact,
3
- RefetchQueryArtifactWrapper,
4
- readButDoNotEvaluate,
5
- } from './index';
6
1
  import { stableCopy } from './cache';
2
+ import { RefetchQueryArtifactWrapper } from './entrypoint';
7
3
  import { IsographEnvironment, DataId } from './IsographEnvironment';
4
+ import { readButDoNotEvaluate } from './read';
5
+ import { ReaderArtifact } from './reader';
8
6
 
9
7
  export function getOrCreateCachedComponent(
10
8
  environment: IsographEnvironment,
package/src/entrypoint.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Arguments } from './index';
2
1
  import { ReaderArtifact } from './reader';
2
+ import { Arguments } from './util';
3
3
 
4
4
  // This type should be treated as an opaque type.
5
5
  export type IsographEntrypoint<
@@ -57,3 +57,8 @@ export function assertIsEntrypoint<
57
57
  ): asserts value is IsographEntrypoint<TReadFromStore, TResolverResult> {
58
58
  if (typeof value === 'function') throw new Error('Not a string');
59
59
  }
60
+
61
+ export type ExtractReadFromStore<Type> =
62
+ Type extends IsographEntrypoint<infer X, any> ? X : never;
63
+ export type ExtractResolverResult<Type> =
64
+ Type extends IsographEntrypoint<any, infer X> ? X : never;
@@ -4,9 +4,10 @@ import {
4
4
  IsographStore,
5
5
  ROOT_ID,
6
6
  StoreRecord,
7
+ assertLink,
7
8
  } from './IsographEnvironment';
8
- import { NormalizationAst, assertLink } from './index';
9
9
  import { getParentRecordKey } from './cache';
10
+ import { NormalizationAst } from './entrypoint';
10
11
 
11
12
  export type RetainedQuery = {
12
13
  normalizationAst: NormalizationAst;