@isograph/react 0.0.0-main-1cd3db6d → 0.0.0-main-2c275831
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/PromiseWrapper.js +1 -1
- package/dist/cache.d.ts +4 -4
- package/dist/cache.js +30 -35
- package/dist/componentCache.d.ts +2 -2
- package/dist/componentCache.js +1 -1
- package/dist/index.d.ts +23 -20
- package/dist/index.js +62 -58
- package/package.json +3 -3
- package/src/PromiseWrapper.ts +1 -1
- package/src/cache.ts +65 -92
- package/src/componentCache.ts +4 -8
- package/src/index.tsx +142 -204
package/dist/PromiseWrapper.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.useReadPromise = exports.wrapPromise = void 0;
|
4
|
-
const NOT_SET = Symbol(
|
4
|
+
const NOT_SET = Symbol('NOT_SET');
|
5
5
|
function wrapPromise(promise) {
|
6
6
|
// TODO confirm suspense works if the promise is already resolved.
|
7
7
|
const wrapper = { promise, value: NOT_SET };
|
package/dist/cache.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { ItemCleanupPair, ParentCache } from
|
2
|
-
import { PromiseWrapper } from
|
3
|
-
import { IsographEntrypoint, NormalizationLinkedField, NormalizationScalarField, ReaderLinkedField, ReaderScalarField } from
|
1
|
+
import { ItemCleanupPair, ParentCache } from '@isograph/react-disposable-state';
|
2
|
+
import { PromiseWrapper } from './PromiseWrapper';
|
3
|
+
import { IsographEntrypoint, NormalizationLinkedField, NormalizationScalarField, ReaderLinkedField, ReaderScalarField } from './index';
|
4
4
|
declare global {
|
5
5
|
interface Window {
|
6
6
|
__LOG: boolean;
|
@@ -26,7 +26,7 @@ export type StoreRecord = {
|
|
26
26
|
id?: DataId;
|
27
27
|
};
|
28
28
|
export type DataId = string;
|
29
|
-
export declare const ROOT_ID: DataId &
|
29
|
+
export declare const ROOT_ID: DataId & '__ROOT';
|
30
30
|
export declare function getStore(): {
|
31
31
|
[index: string]: StoreRecord | null;
|
32
32
|
__ROOT: StoreRecord;
|
package/dist/cache.js
CHANGED
@@ -5,8 +5,8 @@ const react_disposable_state_1 = require("@isograph/react-disposable-state");
|
|
5
5
|
const PromiseWrapper_1 = require("./PromiseWrapper");
|
6
6
|
const cache = {};
|
7
7
|
function getOrCreateCache(index, factory) {
|
8
|
-
if (typeof window !==
|
9
|
-
console.log(
|
8
|
+
if (typeof window !== 'undefined' && window.__LOG) {
|
9
|
+
console.log('getting cache for', {
|
10
10
|
index,
|
11
11
|
cache: Object.keys(cache),
|
12
12
|
found: !!cache[index],
|
@@ -23,7 +23,7 @@ function getOrCreateCache(index, factory) {
|
|
23
23
|
* results.
|
24
24
|
*/
|
25
25
|
function stableCopy(value) {
|
26
|
-
if (!value || typeof value !==
|
26
|
+
if (!value || typeof value !== 'object') {
|
27
27
|
return value;
|
28
28
|
}
|
29
29
|
if (Array.isArray(value)) {
|
@@ -52,15 +52,15 @@ function setNetwork(newNetwork) {
|
|
52
52
|
}
|
53
53
|
exports.setNetwork = setNetwork;
|
54
54
|
function makeNetworkRequest(artifact, variables) {
|
55
|
-
if (typeof window !==
|
56
|
-
console.log(
|
55
|
+
if (typeof window !== 'undefined' && window.__LOG) {
|
56
|
+
console.log('make network request', artifact, variables);
|
57
57
|
}
|
58
58
|
if (network == null) {
|
59
|
-
throw new Error(
|
59
|
+
throw new Error('Network must be set before makeNetworkRequest is called');
|
60
60
|
}
|
61
61
|
const promise = network(artifact.queryText, variables).then((networkResponse) => {
|
62
|
-
if (typeof window !==
|
63
|
-
console.log(
|
62
|
+
if (typeof window !== 'undefined' && window.__LOG) {
|
63
|
+
console.log('network response', artifact);
|
64
64
|
}
|
65
65
|
normalizeData(artifact.normalizationAst, networkResponse.data, variables, artifact.nestedRefetchQueries);
|
66
66
|
return networkResponse.data;
|
@@ -75,7 +75,7 @@ function makeNetworkRequest(artifact, variables) {
|
|
75
75
|
return response;
|
76
76
|
}
|
77
77
|
exports.makeNetworkRequest = makeNetworkRequest;
|
78
|
-
exports.ROOT_ID =
|
78
|
+
exports.ROOT_ID = '__ROOT';
|
79
79
|
let store = {
|
80
80
|
__ROOT: {},
|
81
81
|
};
|
@@ -90,12 +90,12 @@ function clearStore() {
|
|
90
90
|
}
|
91
91
|
exports.clearStore = clearStore;
|
92
92
|
function normalizeData(normalizationAst, networkResponse, variables, nestedRefetchQueries) {
|
93
|
-
if (typeof window !==
|
94
|
-
console.log(
|
93
|
+
if (typeof window !== 'undefined' && window.__LOG) {
|
94
|
+
console.log('about to normalize', normalizationAst, networkResponse, variables);
|
95
95
|
}
|
96
96
|
normalizeDataIntoRecord(normalizationAst, networkResponse, store.__ROOT, exports.ROOT_ID, variables, nestedRefetchQueries);
|
97
|
-
if (typeof window !==
|
98
|
-
console.log(
|
97
|
+
if (typeof window !== 'undefined' && window.__LOG) {
|
98
|
+
console.log('after normalization', { store });
|
99
99
|
}
|
100
100
|
callSubscriptions();
|
101
101
|
}
|
@@ -123,11 +123,11 @@ function callSubscriptions() {
|
|
123
123
|
function normalizeDataIntoRecord(normalizationAst, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries) {
|
124
124
|
for (const normalizationNode of normalizationAst) {
|
125
125
|
switch (normalizationNode.kind) {
|
126
|
-
case
|
126
|
+
case 'Scalar': {
|
127
127
|
normalizeScalarField(normalizationNode, networkResponseParentRecord, targetParentRecord, variables);
|
128
128
|
break;
|
129
129
|
}
|
130
|
-
case
|
130
|
+
case 'Linked': {
|
131
131
|
normalizeLinkedField(normalizationNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries);
|
132
132
|
break;
|
133
133
|
}
|
@@ -138,12 +138,11 @@ function normalizeScalarField(astNode, networkResponseParentRecord, targetStoreR
|
|
138
138
|
const networkResponseKey = getNetworkResponseKey(astNode);
|
139
139
|
const networkResponseData = networkResponseParentRecord[networkResponseKey];
|
140
140
|
const parentRecordKey = getParentRecordKey(astNode, variables);
|
141
|
-
if (networkResponseData == null ||
|
142
|
-
isScalarOrEmptyArray(networkResponseData)) {
|
141
|
+
if (networkResponseData == null || isScalarOrEmptyArray(networkResponseData)) {
|
143
142
|
targetStoreRecord[parentRecordKey] = networkResponseData;
|
144
143
|
}
|
145
144
|
else {
|
146
|
-
throw new Error(
|
145
|
+
throw new Error('Unexpected object array when normalizing scalar');
|
147
146
|
}
|
148
147
|
}
|
149
148
|
/**
|
@@ -158,7 +157,7 @@ function normalizeLinkedField(astNode, networkResponseParentRecord, targetParent
|
|
158
157
|
return;
|
159
158
|
}
|
160
159
|
if (isScalarButNotEmptyArray(networkResponseData)) {
|
161
|
-
throw new Error(
|
160
|
+
throw new Error('Unexpected scalar network response when normalizing a linked field');
|
162
161
|
}
|
163
162
|
if (Array.isArray(networkResponseData)) {
|
164
163
|
// TODO check astNode.plural or the like
|
@@ -191,9 +190,7 @@ function isScalarOrEmptyArray(data) {
|
|
191
190
|
// This is maybe fixed in a new version of Typescript??
|
192
191
|
return data.every((x) => isScalarOrEmptyArray(x));
|
193
192
|
}
|
194
|
-
const isScalarValue = typeof data ===
|
195
|
-
typeof data === "number" ||
|
196
|
-
typeof data === "boolean";
|
193
|
+
const isScalarValue = typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean';
|
197
194
|
return isScalarValue;
|
198
195
|
}
|
199
196
|
function isScalarButNotEmptyArray(data) {
|
@@ -205,9 +202,7 @@ function isScalarButNotEmptyArray(data) {
|
|
205
202
|
// This is maybe fixed in a new version of Typescript??
|
206
203
|
return data.every((x) => isScalarOrEmptyArray(x));
|
207
204
|
}
|
208
|
-
const isScalarValue = typeof data ===
|
209
|
-
typeof data === "number" ||
|
210
|
-
typeof data === "boolean";
|
205
|
+
const isScalarValue = typeof data === 'string' || typeof data === 'number' || typeof data === 'boolean';
|
211
206
|
return isScalarValue;
|
212
207
|
}
|
213
208
|
function getParentRecordKey(astNode, variables) {
|
@@ -223,18 +218,18 @@ function getParentRecordKey(astNode, variables) {
|
|
223
218
|
exports.getParentRecordKey = getParentRecordKey;
|
224
219
|
function getStoreKeyChunkForArgumentValue(argumentValue, variables) {
|
225
220
|
switch (argumentValue.kind) {
|
226
|
-
case
|
221
|
+
case 'Literal': {
|
227
222
|
return argumentValue.value;
|
228
223
|
break;
|
229
224
|
}
|
230
|
-
case
|
225
|
+
case 'Variable': {
|
231
226
|
return variables[argumentValue.name];
|
232
227
|
break;
|
233
228
|
}
|
234
229
|
default: {
|
235
230
|
// TODO configure eslint to allow unused vars starting with _
|
236
231
|
let _ = argumentValue;
|
237
|
-
throw new Error(
|
232
|
+
throw new Error('Unexpected case');
|
238
233
|
}
|
239
234
|
}
|
240
235
|
}
|
@@ -250,17 +245,17 @@ function getNetworkResponseKey(astNode) {
|
|
250
245
|
const [argumentName, argumentValue] = fieldParameter;
|
251
246
|
let argumentValueChunk;
|
252
247
|
switch (argumentValue.kind) {
|
253
|
-
case
|
254
|
-
argumentValueChunk =
|
248
|
+
case 'Literal': {
|
249
|
+
argumentValueChunk = 'l_' + argumentValue.value;
|
255
250
|
break;
|
256
251
|
}
|
257
|
-
case
|
258
|
-
argumentValueChunk =
|
252
|
+
case 'Variable': {
|
253
|
+
argumentValueChunk = 'v_' + argumentValue.name;
|
259
254
|
break;
|
260
255
|
}
|
261
256
|
default: {
|
262
257
|
let _ = argumentValue;
|
263
|
-
throw new Error(
|
258
|
+
throw new Error('Unexpected case');
|
264
259
|
}
|
265
260
|
}
|
266
261
|
networkResponseKey += `${exports.FIRST_SPLIT_KEY}${argumentName}${exports.SECOND_SPLIT_KEY}${argumentValueChunk}`;
|
@@ -269,8 +264,8 @@ function getNetworkResponseKey(astNode) {
|
|
269
264
|
return networkResponseKey;
|
270
265
|
}
|
271
266
|
// an alias might be pullRequests____first___first____after___cursor
|
272
|
-
exports.FIRST_SPLIT_KEY =
|
273
|
-
exports.SECOND_SPLIT_KEY =
|
267
|
+
exports.FIRST_SPLIT_KEY = '____';
|
268
|
+
exports.SECOND_SPLIT_KEY = '___';
|
274
269
|
// Returns a key to look up an item in the store
|
275
270
|
function getDataIdOfNetworkResponse(parentRecordId, dataToNormalize, astNode, variables, index) {
|
276
271
|
// Check whether the dataToNormalize has an id field. If so, that is the key.
|
package/dist/componentCache.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/// <reference types="react" />
|
2
|
-
import { ReaderArtifact, RefetchQueryArtifactWrapper } from
|
3
|
-
import { DataId } from
|
2
|
+
import { ReaderArtifact, RefetchQueryArtifactWrapper } from './index';
|
3
|
+
import { DataId } from './cache';
|
4
4
|
export declare function getOrCreateCachedComponent(root: DataId, componentName: string, readerArtifact: ReaderArtifact<any, any, any>, variables: {
|
5
5
|
[key: string]: string;
|
6
6
|
}, resolverRefetchQueries: RefetchQueryArtifactWrapper[]): import("react").FC<any>;
|
package/dist/componentCache.js
CHANGED
@@ -15,7 +15,7 @@ function getOrCreateCachedComponent(root, componentName, readerArtifact, variabl
|
|
15
15
|
(_c = byArgs[stringifiedArgs]) !== null && _c !== void 0 ? _c : (() => {
|
16
16
|
function Component(additionalRuntimeProps) {
|
17
17
|
const data = (0, index_1.readButDoNotEvaluate)({
|
18
|
-
kind:
|
18
|
+
kind: 'FragmentReference',
|
19
19
|
readerArtifact: readerArtifact,
|
20
20
|
root,
|
21
21
|
variables,
|
package/dist/index.d.ts
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
import { DataId, StoreRecord, Link } from
|
2
|
-
export { setNetwork, makeNetworkRequest, subscribe, DataId, Link, StoreRecord, clearStore, } from
|
1
|
+
import { DataId, StoreRecord, Link } from './cache';
|
2
|
+
export { setNetwork, makeNetworkRequest, subscribe, DataId, Link, StoreRecord, clearStore, } from './cache';
|
3
3
|
export type IsographEntrypoint<TReadFromStore extends Object, TResolverProps, TResolverResult> = {
|
4
|
-
kind:
|
4
|
+
kind: 'Entrypoint';
|
5
5
|
queryText: string;
|
6
6
|
normalizationAst: NormalizationAst;
|
7
7
|
readerArtifact: ReaderArtifact<TReadFromStore, TResolverProps, TResolverResult>;
|
8
8
|
nestedRefetchQueries: RefetchQueryArtifactWrapper[];
|
9
9
|
};
|
10
10
|
export type ReaderArtifact<TReadFromStore extends Object, TResolverProps, TResolverResult> = {
|
11
|
-
kind:
|
11
|
+
kind: 'ReaderArtifact';
|
12
12
|
readerAst: ReaderAst<TReadFromStore>;
|
13
13
|
resolver: (data: TResolverProps) => TResolverResult;
|
14
14
|
variant: ReaderResolverVariant;
|
@@ -16,39 +16,39 @@ export type ReaderArtifact<TReadFromStore extends Object, TResolverProps, TResol
|
|
16
16
|
export type ReaderAstNode = ReaderScalarField | ReaderLinkedField | ReaderResolverField | ReaderRefetchField | ReaderMutationField;
|
17
17
|
export type ReaderAst<TReadFromStore> = ReaderAstNode[];
|
18
18
|
export type ReaderScalarField = {
|
19
|
-
kind:
|
19
|
+
kind: 'Scalar';
|
20
20
|
fieldName: string;
|
21
21
|
alias: string | null;
|
22
22
|
arguments: Arguments | null;
|
23
23
|
};
|
24
24
|
export type ReaderLinkedField = {
|
25
|
-
kind:
|
25
|
+
kind: 'Linked';
|
26
26
|
fieldName: string;
|
27
27
|
alias: string | null;
|
28
28
|
selections: ReaderAst<unknown>;
|
29
29
|
arguments: Arguments | null;
|
30
30
|
};
|
31
31
|
export type ReaderResolverVariant = {
|
32
|
-
kind:
|
32
|
+
kind: 'Eager';
|
33
33
|
} | {
|
34
|
-
kind:
|
34
|
+
kind: 'Component';
|
35
35
|
componentName: string;
|
36
36
|
};
|
37
37
|
export type ReaderResolverField = {
|
38
|
-
kind:
|
38
|
+
kind: 'Resolver';
|
39
39
|
alias: string;
|
40
40
|
readerArtifact: ReaderArtifact<any, any, any>;
|
41
41
|
arguments: Arguments | null;
|
42
42
|
usedRefetchQueries: number[];
|
43
43
|
};
|
44
44
|
export type ReaderRefetchField = {
|
45
|
-
kind:
|
45
|
+
kind: 'RefetchField';
|
46
46
|
alias: string;
|
47
47
|
readerArtifact: ReaderArtifact<any, any, any>;
|
48
48
|
refetchQuery: number;
|
49
49
|
};
|
50
50
|
export type ReaderMutationField = {
|
51
|
-
kind:
|
51
|
+
kind: 'MutationField';
|
52
52
|
alias: string;
|
53
53
|
readerArtifact: ReaderArtifact<any, any, any>;
|
54
54
|
refetchQuery: number;
|
@@ -57,18 +57,18 @@ export type ReaderMutationField = {
|
|
57
57
|
export type NormalizationAstNode = NormalizationScalarField | NormalizationLinkedField;
|
58
58
|
export type NormalizationAst = NormalizationAstNode[];
|
59
59
|
export type NormalizationScalarField = {
|
60
|
-
kind:
|
60
|
+
kind: 'Scalar';
|
61
61
|
fieldName: string;
|
62
62
|
arguments: Arguments | null;
|
63
63
|
};
|
64
64
|
export type NormalizationLinkedField = {
|
65
|
-
kind:
|
65
|
+
kind: 'Linked';
|
66
66
|
fieldName: string;
|
67
67
|
arguments: Arguments | null;
|
68
68
|
selections: NormalizationAst;
|
69
69
|
};
|
70
70
|
export type RefetchQueryArtifact = {
|
71
|
-
kind:
|
71
|
+
kind: 'RefetchQuery';
|
72
72
|
queryText: string;
|
73
73
|
normalizationAst: NormalizationAst;
|
74
74
|
};
|
@@ -80,14 +80,14 @@ export type Arguments = Argument[];
|
|
80
80
|
export type Argument = [ArgumentName, ArgumentValue];
|
81
81
|
export type ArgumentName = string;
|
82
82
|
export type ArgumentValue = {
|
83
|
-
kind:
|
83
|
+
kind: 'Variable';
|
84
84
|
name: string;
|
85
85
|
} | {
|
86
|
-
kind:
|
86
|
+
kind: 'Literal';
|
87
87
|
value: any;
|
88
88
|
};
|
89
89
|
export type FragmentReference<TReadFromStore extends Object, TResolverProps, TResolverResult> = {
|
90
|
-
kind:
|
90
|
+
kind: 'FragmentReference';
|
91
91
|
readerArtifact: ReaderArtifact<TReadFromStore, TResolverProps, TResolverResult>;
|
92
92
|
root: DataId;
|
93
93
|
variables: {
|
@@ -95,9 +95,12 @@ export type FragmentReference<TReadFromStore extends Object, TResolverProps, TRe
|
|
95
95
|
} | null;
|
96
96
|
nestedRefetchQueries: RefetchQueryArtifactWrapper[];
|
97
97
|
};
|
98
|
-
export declare function iso<TResolverParameter
|
99
|
-
|
100
|
-
|
98
|
+
export declare function iso<TResolverParameter>(_queryText: TemplateStringsArray): <TResolverReturn>(x: (param: TResolverParameter) => TResolverReturn) => (param: TResolverParameter) => TResolverReturn;
|
99
|
+
type ExtractTReadFromStore<Type> = Type extends IsographEntrypoint<infer X, any, any> ? X : never;
|
100
|
+
type ExtractResolverProps<Type> = Type extends IsographEntrypoint<any, infer X, any> ? X : never;
|
101
|
+
type ExtractResolverResult<Type> = Type extends IsographEntrypoint<any, any, infer X> ? X : never;
|
102
|
+
export declare function useLazyReference<TEntrypoint>(entrypoint: TEntrypoint | ((_: any) => any), variables: object): {
|
103
|
+
queryReference: FragmentReference<ExtractTReadFromStore<TEntrypoint>, ExtractResolverProps<TEntrypoint>, ExtractResolverResult<TEntrypoint>>;
|
101
104
|
};
|
102
105
|
export declare function read<TReadFromStore extends Object, TResolverProps, TResolverResult>(fragmentReference: FragmentReference<TReadFromStore, TResolverProps, TResolverResult>): TResolverResult;
|
103
106
|
export declare function readButDoNotEvaluate<TReadFromStore extends Object>(reference: FragmentReference<TReadFromStore, unknown, unknown>): TReadFromStore;
|