@isograph/react 0.0.0-main-f524690b → 0.0.0-main-38d73d29
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/IsographEnvironment.d.ts +56 -0
- package/dist/IsographEnvironment.js +65 -0
- package/dist/cache.d.ts +5 -21
- package/dist/cache.js +36 -58
- package/dist/componentCache.d.ts +2 -2
- package/dist/componentCache.js +3 -3
- package/dist/index.d.ts +7 -6
- package/dist/index.js +39 -39
- package/package.json +3 -3
- package/src/IsographEnvironment.tsx +126 -0
- package/src/{cache.ts → cache.tsx} +47 -81
- package/src/componentCache.ts +5 -9
- package/src/index.tsx +66 -53
@@ -0,0 +1,56 @@
|
|
1
|
+
import { ReactNode } from 'react';
|
2
|
+
import * as React from 'react';
|
3
|
+
import { ParentCache } from '@isograph/isograph-react-disposable-state';
|
4
|
+
export declare const IsographEnvironmentContext: React.Context<IsographEnvironment | null>;
|
5
|
+
type ComponentName = string;
|
6
|
+
type StringifiedArgs = string;
|
7
|
+
type ComponentCache = {
|
8
|
+
[key: DataId]: {
|
9
|
+
[key: ComponentName]: {
|
10
|
+
[key: StringifiedArgs]: React.FC<any>;
|
11
|
+
};
|
12
|
+
};
|
13
|
+
};
|
14
|
+
export type Subscriptions = Set<() => void>;
|
15
|
+
type SuspenseCache = {
|
16
|
+
[index: string]: ParentCache<any>;
|
17
|
+
};
|
18
|
+
export type IsographEnvironment = {
|
19
|
+
store: IsographStore;
|
20
|
+
networkFunction: IsographNetworkFunction;
|
21
|
+
missingFieldHandler: MissingFieldHandler | null;
|
22
|
+
componentCache: ComponentCache;
|
23
|
+
subscriptions: Subscriptions;
|
24
|
+
suspenseCache: SuspenseCache;
|
25
|
+
};
|
26
|
+
export type MissingFieldHandler = (storeRecord: StoreRecord, root: DataId, fieldName: string, arguments_: {
|
27
|
+
[index: string]: any;
|
28
|
+
} | null, variables: {
|
29
|
+
[index: string]: any;
|
30
|
+
} | null) => Link | undefined;
|
31
|
+
export type IsographNetworkFunction = (queryText: string, variables: object) => Promise<any>;
|
32
|
+
export type Link = {
|
33
|
+
__link: DataId;
|
34
|
+
};
|
35
|
+
export type DataTypeValue = undefined | number | boolean | string | null | Link | DataTypeValue[];
|
36
|
+
export type StoreRecord = {
|
37
|
+
[index: DataId | string]: DataTypeValue;
|
38
|
+
id?: DataId;
|
39
|
+
};
|
40
|
+
export type DataId = string;
|
41
|
+
export declare const ROOT_ID: DataId & '__ROOT';
|
42
|
+
export type IsographStore = {
|
43
|
+
[index: DataId]: StoreRecord | null;
|
44
|
+
__ROOT: StoreRecord;
|
45
|
+
};
|
46
|
+
export type IsographEnvironmentProviderProps = {
|
47
|
+
environment: IsographEnvironment;
|
48
|
+
children: ReactNode;
|
49
|
+
};
|
50
|
+
export declare function IsographEnvironmentProvider({ environment, children, }: IsographEnvironmentProviderProps): React.JSX.Element;
|
51
|
+
export declare function useIsographEnvironment(): IsographEnvironment;
|
52
|
+
export declare function createIsographEnvironment(store: IsographStore, networkFunction: IsographNetworkFunction, missingFieldHandler?: MissingFieldHandler): IsographEnvironment;
|
53
|
+
export declare function createIsographStore(): {
|
54
|
+
__ROOT: {};
|
55
|
+
};
|
56
|
+
export {};
|
@@ -0,0 +1,65 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
+
exports.createIsographStore = exports.createIsographEnvironment = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = void 0;
|
27
|
+
const react_1 = require("react");
|
28
|
+
const React = __importStar(require("react"));
|
29
|
+
const cache_1 = require("./cache");
|
30
|
+
exports.IsographEnvironmentContext = (0, react_1.createContext)(null);
|
31
|
+
exports.ROOT_ID = '__ROOT';
|
32
|
+
function IsographEnvironmentProvider({ environment, children, }) {
|
33
|
+
const [, setState] = React.useState();
|
34
|
+
React.useEffect(() => {
|
35
|
+
return (0, cache_1.subscribe)(environment, () => setState({}));
|
36
|
+
}, []);
|
37
|
+
return (React.createElement(exports.IsographEnvironmentContext.Provider, { value: environment }, children));
|
38
|
+
}
|
39
|
+
exports.IsographEnvironmentProvider = IsographEnvironmentProvider;
|
40
|
+
function useIsographEnvironment() {
|
41
|
+
const context = (0, react_1.useContext)(exports.IsographEnvironmentContext);
|
42
|
+
if (context == null) {
|
43
|
+
throw new Error('Unexpected null environment context. Make sure to render ' +
|
44
|
+
'this component within an IsographEnvironmentProvider component');
|
45
|
+
}
|
46
|
+
return context;
|
47
|
+
}
|
48
|
+
exports.useIsographEnvironment = useIsographEnvironment;
|
49
|
+
function createIsographEnvironment(store, networkFunction, missingFieldHandler) {
|
50
|
+
return {
|
51
|
+
store,
|
52
|
+
networkFunction,
|
53
|
+
missingFieldHandler: missingFieldHandler !== null && missingFieldHandler !== void 0 ? missingFieldHandler : null,
|
54
|
+
componentCache: {},
|
55
|
+
subscriptions: new Set(),
|
56
|
+
suspenseCache: {},
|
57
|
+
};
|
58
|
+
}
|
59
|
+
exports.createIsographEnvironment = createIsographEnvironment;
|
60
|
+
function createIsographStore() {
|
61
|
+
return {
|
62
|
+
[exports.ROOT_ID]: {},
|
63
|
+
};
|
64
|
+
}
|
65
|
+
exports.createIsographStore = createIsographStore;
|
package/dist/cache.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import { ItemCleanupPair, ParentCache } from '@isograph/react-disposable-state';
|
2
2
|
import { PromiseWrapper } from './PromiseWrapper';
|
3
3
|
import { IsographEntrypoint, NormalizationLinkedField, NormalizationScalarField, ReaderLinkedField, ReaderScalarField } from './index';
|
4
|
+
import { type IsographEnvironment } from './IsographEnvironment';
|
4
5
|
declare global {
|
5
6
|
interface Window {
|
6
7
|
__LOG: boolean;
|
@@ -13,27 +14,10 @@ declare global {
|
|
13
14
|
*/
|
14
15
|
export declare function stableCopy<T>(value: T): T;
|
15
16
|
type IsoResolver = IsographEntrypoint<any, any, any>;
|
16
|
-
export declare function getOrCreateCacheForArtifact<T>(artifact: IsographEntrypoint<any, any, T>, variables: object): ParentCache<PromiseWrapper<T>>;
|
17
|
-
declare
|
18
|
-
export declare function
|
19
|
-
export declare function
|
20
|
-
export type Link = {
|
21
|
-
__link: DataId;
|
22
|
-
};
|
23
|
-
export type DataTypeValue = undefined | number | boolean | string | null | Link | DataTypeValue[];
|
24
|
-
export type StoreRecord = {
|
25
|
-
[index: DataId | string]: DataTypeValue;
|
26
|
-
id?: DataId;
|
27
|
-
};
|
28
|
-
export type DataId = string;
|
29
|
-
export declare const ROOT_ID: DataId & '__ROOT';
|
30
|
-
export declare function getStore(): {
|
31
|
-
[index: string]: StoreRecord | null;
|
32
|
-
__ROOT: StoreRecord;
|
33
|
-
};
|
34
|
-
export declare function clearStore(): void;
|
35
|
-
export declare function subscribe(callback: () => void): () => void;
|
36
|
-
export declare function onNextChange(): Promise<void>;
|
17
|
+
export declare function getOrCreateCacheForArtifact<T>(environment: IsographEnvironment, artifact: IsographEntrypoint<any, any, T>, variables: object): ParentCache<PromiseWrapper<T>>;
|
18
|
+
export declare function makeNetworkRequest<T>(environment: IsographEnvironment, artifact: IsoResolver, variables: object): ItemCleanupPair<PromiseWrapper<T>>;
|
19
|
+
export declare function subscribe(environment: IsographEnvironment, callback: () => void): () => void;
|
20
|
+
export declare function onNextChange(environment: IsographEnvironment): Promise<void>;
|
37
21
|
export declare function getParentRecordKey(astNode: NormalizationLinkedField | NormalizationScalarField | ReaderLinkedField | ReaderScalarField, variables: {
|
38
22
|
[index: string]: string;
|
39
23
|
}): string;
|
package/dist/cache.js
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.SECOND_SPLIT_KEY = exports.FIRST_SPLIT_KEY = exports.getParentRecordKey = exports.onNextChange = exports.subscribe = exports.
|
3
|
+
exports.SECOND_SPLIT_KEY = exports.FIRST_SPLIT_KEY = exports.getParentRecordKey = exports.onNextChange = exports.subscribe = exports.makeNetworkRequest = exports.getOrCreateCacheForArtifact = exports.stableCopy = void 0;
|
4
4
|
const react_disposable_state_1 = require("@isograph/react-disposable-state");
|
5
5
|
const PromiseWrapper_1 = require("./PromiseWrapper");
|
6
|
-
const
|
7
|
-
function getOrCreateCache(index, factory) {
|
6
|
+
const IsographEnvironment_1 = require("./IsographEnvironment");
|
7
|
+
function getOrCreateCache(environment, index, factory) {
|
8
8
|
if (typeof window !== 'undefined' && window.__LOG) {
|
9
9
|
console.log('getting cache for', {
|
10
10
|
index,
|
11
|
-
cache: Object.keys(
|
12
|
-
found: !!
|
11
|
+
cache: Object.keys(environment.suspenseCache),
|
12
|
+
found: !!environment.suspenseCache[index],
|
13
13
|
});
|
14
14
|
}
|
15
|
-
if (
|
16
|
-
|
15
|
+
if (environment.suspenseCache[index] == null) {
|
16
|
+
environment.suspenseCache[index] = new react_disposable_state_1.ParentCache(factory);
|
17
17
|
}
|
18
|
-
return
|
18
|
+
return environment.suspenseCache[index];
|
19
19
|
}
|
20
20
|
/**
|
21
21
|
* Creates a copy of the provided value, ensuring any nested objects have their
|
@@ -39,30 +39,23 @@ function stableCopy(value) {
|
|
39
39
|
return stable;
|
40
40
|
}
|
41
41
|
exports.stableCopy = stableCopy;
|
42
|
-
function getOrCreateCacheForArtifact(artifact, variables) {
|
42
|
+
function getOrCreateCacheForArtifact(environment, artifact, variables) {
|
43
43
|
const cacheKey = artifact.queryText + JSON.stringify(stableCopy(variables));
|
44
|
-
const factory = () => makeNetworkRequest(artifact, variables);
|
45
|
-
return getOrCreateCache(cacheKey, factory);
|
44
|
+
const factory = () => makeNetworkRequest(environment, artifact, variables);
|
45
|
+
return getOrCreateCache(environment, cacheKey, factory);
|
46
46
|
}
|
47
47
|
exports.getOrCreateCacheForArtifact = getOrCreateCacheForArtifact;
|
48
|
-
|
49
|
-
// This is a hack until we store this in context somehow
|
50
|
-
function setNetwork(newNetwork) {
|
51
|
-
network = newNetwork;
|
52
|
-
}
|
53
|
-
exports.setNetwork = setNetwork;
|
54
|
-
function makeNetworkRequest(artifact, variables) {
|
48
|
+
function makeNetworkRequest(environment, artifact, variables) {
|
55
49
|
if (typeof window !== 'undefined' && window.__LOG) {
|
56
50
|
console.log('make network request', artifact, variables);
|
57
51
|
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
const promise = network(artifact.queryText, variables).then((networkResponse) => {
|
52
|
+
const promise = environment
|
53
|
+
.networkFunction(artifact.queryText, variables)
|
54
|
+
.then((networkResponse) => {
|
62
55
|
if (typeof window !== 'undefined' && window.__LOG) {
|
63
56
|
console.log('network response', artifact);
|
64
57
|
}
|
65
|
-
normalizeData(artifact.normalizationAst, networkResponse.data, variables, artifact.nestedRefetchQueries);
|
58
|
+
normalizeData(environment, artifact.normalizationAst, networkResponse.data, variables, artifact.nestedRefetchQueries);
|
66
59
|
return networkResponse.data;
|
67
60
|
});
|
68
61
|
const wrapper = (0, PromiseWrapper_1.wrapPromise)(promise);
|
@@ -75,52 +68,37 @@ function makeNetworkRequest(artifact, variables) {
|
|
75
68
|
return response;
|
76
69
|
}
|
77
70
|
exports.makeNetworkRequest = makeNetworkRequest;
|
78
|
-
|
79
|
-
let store = {
|
80
|
-
__ROOT: {},
|
81
|
-
};
|
82
|
-
function getStore() {
|
83
|
-
return store;
|
84
|
-
}
|
85
|
-
exports.getStore = getStore;
|
86
|
-
function clearStore() {
|
87
|
-
store = {
|
88
|
-
__ROOT: {},
|
89
|
-
};
|
90
|
-
}
|
91
|
-
exports.clearStore = clearStore;
|
92
|
-
function normalizeData(normalizationAst, networkResponse, variables, nestedRefetchQueries) {
|
71
|
+
function normalizeData(environment, normalizationAst, networkResponse, variables, nestedRefetchQueries) {
|
93
72
|
if (typeof window !== 'undefined' && window.__LOG) {
|
94
73
|
console.log('about to normalize', normalizationAst, networkResponse, variables);
|
95
74
|
}
|
96
|
-
normalizeDataIntoRecord(normalizationAst, networkResponse, store.__ROOT,
|
75
|
+
normalizeDataIntoRecord(environment, normalizationAst, networkResponse, environment.store.__ROOT, IsographEnvironment_1.ROOT_ID, variables, nestedRefetchQueries);
|
97
76
|
if (typeof window !== 'undefined' && window.__LOG) {
|
98
|
-
console.log('after normalization', { store });
|
77
|
+
console.log('after normalization', { store: environment.store });
|
99
78
|
}
|
100
|
-
callSubscriptions();
|
79
|
+
callSubscriptions(environment);
|
101
80
|
}
|
102
|
-
function subscribe(callback) {
|
103
|
-
subscriptions.add(callback);
|
104
|
-
return () => subscriptions.delete(callback);
|
81
|
+
function subscribe(environment, callback) {
|
82
|
+
environment.subscriptions.add(callback);
|
83
|
+
return () => environment.subscriptions.delete(callback);
|
105
84
|
}
|
106
85
|
exports.subscribe = subscribe;
|
107
|
-
function onNextChange() {
|
86
|
+
function onNextChange(environment) {
|
108
87
|
return new Promise((resolve) => {
|
109
|
-
const unsubscribe = subscribe(() => {
|
88
|
+
const unsubscribe = subscribe(environment, () => {
|
110
89
|
unsubscribe();
|
111
90
|
resolve();
|
112
91
|
});
|
113
92
|
});
|
114
93
|
}
|
115
94
|
exports.onNextChange = onNextChange;
|
116
|
-
|
117
|
-
|
118
|
-
subscriptions.forEach((callback) => callback());
|
95
|
+
function callSubscriptions(environment) {
|
96
|
+
environment.subscriptions.forEach((callback) => callback());
|
119
97
|
}
|
120
98
|
/**
|
121
99
|
* Mutate targetParentRecord according to the normalizationAst and networkResponseParentRecord.
|
122
100
|
*/
|
123
|
-
function normalizeDataIntoRecord(normalizationAst, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries) {
|
101
|
+
function normalizeDataIntoRecord(environment, normalizationAst, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries) {
|
124
102
|
for (const normalizationNode of normalizationAst) {
|
125
103
|
switch (normalizationNode.kind) {
|
126
104
|
case 'Scalar': {
|
@@ -128,7 +106,7 @@ function normalizeDataIntoRecord(normalizationAst, networkResponseParentRecord,
|
|
128
106
|
break;
|
129
107
|
}
|
130
108
|
case 'Linked': {
|
131
|
-
normalizeLinkedField(normalizationNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries);
|
109
|
+
normalizeLinkedField(environment, normalizationNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries);
|
132
110
|
break;
|
133
111
|
}
|
134
112
|
}
|
@@ -149,7 +127,7 @@ function normalizeScalarField(astNode, networkResponseParentRecord, targetStoreR
|
|
149
127
|
/**
|
150
128
|
* Mutate targetParentRecord with a given linked field ast node.
|
151
129
|
*/
|
152
|
-
function normalizeLinkedField(astNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries) {
|
130
|
+
function normalizeLinkedField(environment, astNode, networkResponseParentRecord, targetParentRecord, targetParentRecordId, variables, nestedRefetchQueries) {
|
153
131
|
const networkResponseKey = getNetworkResponseKey(astNode);
|
154
132
|
const networkResponseData = networkResponseParentRecord[networkResponseKey];
|
155
133
|
const parentRecordKey = getParentRecordKey(astNode, variables);
|
@@ -165,24 +143,24 @@ function normalizeLinkedField(astNode, networkResponseParentRecord, targetParent
|
|
165
143
|
const dataIds = [];
|
166
144
|
for (let i = 0; i < networkResponseData.length; i++) {
|
167
145
|
const networkResponseObject = networkResponseData[i];
|
168
|
-
const newStoreRecordId = normalizeNetworkResponseObject(astNode, networkResponseObject, targetParentRecordId, variables, i, nestedRefetchQueries);
|
146
|
+
const newStoreRecordId = normalizeNetworkResponseObject(environment, astNode, networkResponseObject, targetParentRecordId, variables, i, nestedRefetchQueries);
|
169
147
|
dataIds.push({ __link: newStoreRecordId });
|
170
148
|
}
|
171
149
|
targetParentRecord[parentRecordKey] = dataIds;
|
172
150
|
}
|
173
151
|
else {
|
174
|
-
const newStoreRecordId = normalizeNetworkResponseObject(astNode, networkResponseData, targetParentRecordId, variables, null, nestedRefetchQueries);
|
152
|
+
const newStoreRecordId = normalizeNetworkResponseObject(environment, astNode, networkResponseData, targetParentRecordId, variables, null, nestedRefetchQueries);
|
175
153
|
targetParentRecord[parentRecordKey] = {
|
176
154
|
__link: newStoreRecordId,
|
177
155
|
};
|
178
156
|
}
|
179
157
|
}
|
180
|
-
function normalizeNetworkResponseObject(astNode, networkResponseData, targetParentRecordId, variables, index, nestedRefetchQueries) {
|
158
|
+
function normalizeNetworkResponseObject(environment, astNode, networkResponseData, targetParentRecordId, variables, index, nestedRefetchQueries) {
|
181
159
|
var _a;
|
182
160
|
const newStoreRecordId = getDataIdOfNetworkResponse(targetParentRecordId, networkResponseData, astNode, variables, index);
|
183
|
-
const newStoreRecord = (_a = store[newStoreRecordId]) !== null && _a !== void 0 ? _a : {};
|
184
|
-
store[newStoreRecordId] = newStoreRecord;
|
185
|
-
normalizeDataIntoRecord(astNode.selections, networkResponseData, newStoreRecord, newStoreRecordId, variables, nestedRefetchQueries);
|
161
|
+
const newStoreRecord = (_a = environment.store[newStoreRecordId]) !== null && _a !== void 0 ? _a : {};
|
162
|
+
environment.store[newStoreRecordId] = newStoreRecord;
|
163
|
+
normalizeDataIntoRecord(environment, astNode.selections, networkResponseData, newStoreRecord, newStoreRecordId, variables, nestedRefetchQueries);
|
186
164
|
return newStoreRecordId;
|
187
165
|
}
|
188
166
|
function isScalarOrEmptyArray(data) {
|
package/dist/componentCache.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/// <reference types="react" />
|
2
2
|
import { ReaderArtifact, RefetchQueryArtifactWrapper } from './index';
|
3
|
-
import { DataId } from './
|
4
|
-
export declare function getOrCreateCachedComponent(root: DataId, componentName: string, readerArtifact: ReaderArtifact<any, any, any>, variables: {
|
3
|
+
import { IsographEnvironment, DataId } from './IsographEnvironment';
|
4
|
+
export declare function getOrCreateCachedComponent(environment: IsographEnvironment, 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
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getOrCreateCachedComponent = void 0;
|
4
4
|
const index_1 = require("./index");
|
5
5
|
const cache_1 = require("./cache");
|
6
|
-
|
7
|
-
function getOrCreateCachedComponent(root, componentName, readerArtifact, variables, resolverRefetchQueries) {
|
6
|
+
function getOrCreateCachedComponent(environment, root, componentName, readerArtifact, variables, resolverRefetchQueries) {
|
8
7
|
var _a, _b, _c;
|
8
|
+
const cachedComponentsById = environment.componentCache;
|
9
9
|
const stringifiedArgs = JSON.stringify((0, cache_1.stableCopy)(variables));
|
10
10
|
cachedComponentsById[root] = (_a = cachedComponentsById[root]) !== null && _a !== void 0 ? _a : {};
|
11
11
|
const componentsByName = cachedComponentsById[root];
|
@@ -14,7 +14,7 @@ function getOrCreateCachedComponent(root, componentName, readerArtifact, variabl
|
|
14
14
|
byArgs[stringifiedArgs] =
|
15
15
|
(_c = byArgs[stringifiedArgs]) !== null && _c !== void 0 ? _c : (() => {
|
16
16
|
function Component(additionalRuntimeProps) {
|
17
|
-
const data = (0, index_1.readButDoNotEvaluate)({
|
17
|
+
const data = (0, index_1.readButDoNotEvaluate)(environment, {
|
18
18
|
kind: 'FragmentReference',
|
19
19
|
readerArtifact: readerArtifact,
|
20
20
|
root,
|
package/dist/index.d.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
import { DataId,
|
2
|
-
export {
|
1
|
+
import { DataId, IsographEnvironment, Link, StoreRecord } from './IsographEnvironment';
|
2
|
+
export { makeNetworkRequest, subscribe } from './cache';
|
3
|
+
export { IsographEnvironmentContext, ROOT_ID, type DataId, type DataTypeValue, type IsographEnvironment, IsographEnvironmentProvider, type IsographEnvironmentProviderProps, type IsographNetworkFunction, type IsographStore, type Link, type StoreRecord, useIsographEnvironment, createIsographEnvironment, createIsographStore, } from './IsographEnvironment';
|
3
4
|
export { iso } from './iso';
|
4
5
|
export type IsographEntrypoint<TReadFromStore extends Object, TResolverProps, TResolverResult> = {
|
5
6
|
kind: 'Entrypoint';
|
@@ -102,14 +103,14 @@ type ExtractResolverResult<Type> = Type extends IsographEntrypoint<any, any, inf
|
|
102
103
|
export declare function useLazyReference<TEntrypoint>(entrypoint: TEntrypoint | ((_: any) => any), variables: object): {
|
103
104
|
queryReference: FragmentReference<ExtractTReadFromStore<TEntrypoint>, ExtractResolverProps<TEntrypoint>, ExtractResolverResult<TEntrypoint>>;
|
104
105
|
};
|
105
|
-
export declare function
|
106
|
-
export declare function
|
107
|
-
export declare function
|
106
|
+
export declare function useRead<TReadFromStore extends Object, TResolverProps, TResolverResult>(fragmentReference: FragmentReference<TReadFromStore, TResolverProps, TResolverResult>): TResolverResult;
|
107
|
+
export declare function read<TReadFromStore extends Object, TResolverProps, TResolverResult>(environment: IsographEnvironment, fragmentReference: FragmentReference<TReadFromStore, TResolverProps, TResolverResult>): TResolverResult;
|
108
|
+
export declare function readButDoNotEvaluate<TReadFromStore extends Object>(environment: IsographEnvironment, reference: FragmentReference<TReadFromStore, unknown, unknown>): TReadFromStore;
|
109
|
+
export declare function defaultMissingFieldHandler(_storeRecord: StoreRecord, _root: DataId, fieldName: string, arguments_: {
|
108
110
|
[index: string]: any;
|
109
111
|
} | null, variables: {
|
110
112
|
[index: string]: any;
|
111
113
|
} | null): Link | undefined;
|
112
|
-
export declare function setMissingFieldHandler(handler: typeof defaultMissingFieldHandler): void;
|
113
114
|
export type IsographComponentProps<TDataType, TOtherProps = Object> = {
|
114
115
|
data: TDataType;
|
115
116
|
} & TOtherProps;
|
package/dist/index.js
CHANGED
@@ -1,14 +1,20 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useRead = exports.useLazyReference = exports.iso = exports.createIsographStore = exports.createIsographEnvironment = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = exports.subscribe = exports.makeNetworkRequest = void 0;
|
4
4
|
const cache_1 = require("./cache");
|
5
5
|
const react_disposable_state_1 = require("@isograph/react-disposable-state");
|
6
6
|
const componentCache_1 = require("./componentCache");
|
7
|
+
const IsographEnvironment_1 = require("./IsographEnvironment");
|
7
8
|
var cache_2 = require("./cache");
|
8
|
-
Object.defineProperty(exports, "setNetwork", { enumerable: true, get: function () { return cache_2.setNetwork; } });
|
9
9
|
Object.defineProperty(exports, "makeNetworkRequest", { enumerable: true, get: function () { return cache_2.makeNetworkRequest; } });
|
10
10
|
Object.defineProperty(exports, "subscribe", { enumerable: true, get: function () { return cache_2.subscribe; } });
|
11
|
-
|
11
|
+
var IsographEnvironment_2 = require("./IsographEnvironment");
|
12
|
+
Object.defineProperty(exports, "IsographEnvironmentContext", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentContext; } });
|
13
|
+
Object.defineProperty(exports, "ROOT_ID", { enumerable: true, get: function () { return IsographEnvironment_2.ROOT_ID; } });
|
14
|
+
Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentProvider; } });
|
15
|
+
Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.useIsographEnvironment; } });
|
16
|
+
Object.defineProperty(exports, "createIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographEnvironment; } });
|
17
|
+
Object.defineProperty(exports, "createIsographStore", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographStore; } });
|
12
18
|
var iso_1 = require("./iso");
|
13
19
|
Object.defineProperty(exports, "iso", { enumerable: true, get: function () { return iso_1.iso; } });
|
14
20
|
function assertIsEntrypoint(value) {
|
@@ -20,9 +26,10 @@ function assertIsEntrypoint(value) {
|
|
20
26
|
// We cannot write TEntrypoint extends IsographEntrypoint<never, never, never>, or else
|
21
27
|
// any actual Entrypoint we pass will not be valid.
|
22
28
|
function useLazyReference(entrypoint, variables) {
|
29
|
+
const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
|
23
30
|
assertIsEntrypoint(entrypoint);
|
24
31
|
// Typechecking fails here... TODO investigate
|
25
|
-
const cache = (0, cache_1.getOrCreateCacheForArtifact)(entrypoint, variables);
|
32
|
+
const cache = (0, cache_1.getOrCreateCacheForArtifact)(environment, entrypoint, variables);
|
26
33
|
// TODO add comment explaining why we never use this value
|
27
34
|
// @ts-ignore
|
28
35
|
const data = (0, react_disposable_state_1.useLazyDisposableState)(cache).state;
|
@@ -30,20 +37,25 @@ function useLazyReference(entrypoint, variables) {
|
|
30
37
|
queryReference: {
|
31
38
|
kind: 'FragmentReference',
|
32
39
|
readerArtifact: entrypoint.readerArtifact,
|
33
|
-
root:
|
40
|
+
root: IsographEnvironment_1.ROOT_ID,
|
34
41
|
variables,
|
35
42
|
nestedRefetchQueries: entrypoint.nestedRefetchQueries,
|
36
43
|
},
|
37
44
|
};
|
38
45
|
}
|
39
46
|
exports.useLazyReference = useLazyReference;
|
40
|
-
function
|
47
|
+
function useRead(fragmentReference) {
|
48
|
+
const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
|
49
|
+
return read(environment, fragmentReference);
|
50
|
+
}
|
51
|
+
exports.useRead = useRead;
|
52
|
+
function read(environment, fragmentReference) {
|
41
53
|
var _a, _b;
|
42
54
|
const variant = fragmentReference.readerArtifact.variant;
|
43
55
|
if (variant.kind === 'Eager') {
|
44
|
-
const data = readData(fragmentReference.readerArtifact.readerAst, fragmentReference.root, (_a = fragmentReference.variables) !== null && _a !== void 0 ? _a : {}, fragmentReference.nestedRefetchQueries);
|
56
|
+
const data = readData(environment, fragmentReference.readerArtifact.readerAst, fragmentReference.root, (_a = fragmentReference.variables) !== null && _a !== void 0 ? _a : {}, fragmentReference.nestedRefetchQueries);
|
45
57
|
if (data.kind === 'MissingData') {
|
46
|
-
throw (0, cache_1.onNextChange)();
|
58
|
+
throw (0, cache_1.onNextChange)(environment);
|
47
59
|
}
|
48
60
|
else {
|
49
61
|
return fragmentReference.readerArtifact.resolver(data.data);
|
@@ -51,29 +63,29 @@ function read(fragmentReference) {
|
|
51
63
|
}
|
52
64
|
else if (variant.kind === 'Component') {
|
53
65
|
// @ts-ignore
|
54
|
-
return (0, componentCache_1.getOrCreateCachedComponent)(fragmentReference.root, variant.componentName, fragmentReference.readerArtifact, (_b = fragmentReference.variables) !== null && _b !== void 0 ? _b : {}, fragmentReference.nestedRefetchQueries);
|
66
|
+
return (0, componentCache_1.getOrCreateCachedComponent)(environment, fragmentReference.root, variant.componentName, fragmentReference.readerArtifact, (_b = fragmentReference.variables) !== null && _b !== void 0 ? _b : {}, fragmentReference.nestedRefetchQueries);
|
55
67
|
}
|
56
68
|
// Why can't Typescript realize that this is unreachable??
|
57
69
|
throw new Error('This is unreachable');
|
58
70
|
}
|
59
71
|
exports.read = read;
|
60
|
-
function readButDoNotEvaluate(reference) {
|
72
|
+
function readButDoNotEvaluate(environment, reference) {
|
61
73
|
var _a;
|
62
|
-
const response = readData(reference.readerArtifact.readerAst, reference.root, (_a = reference.variables) !== null && _a !== void 0 ? _a : {}, reference.nestedRefetchQueries);
|
74
|
+
const response = readData(environment, reference.readerArtifact.readerAst, reference.root, (_a = reference.variables) !== null && _a !== void 0 ? _a : {}, reference.nestedRefetchQueries);
|
63
75
|
if (typeof window !== 'undefined' && window.__LOG) {
|
64
76
|
console.log('done reading', { response });
|
65
77
|
}
|
66
78
|
if (response.kind === 'MissingData') {
|
67
|
-
throw (0, cache_1.onNextChange)();
|
79
|
+
throw (0, cache_1.onNextChange)(environment);
|
68
80
|
}
|
69
81
|
else {
|
70
82
|
return response.data;
|
71
83
|
}
|
72
84
|
}
|
73
85
|
exports.readButDoNotEvaluate = readButDoNotEvaluate;
|
74
|
-
function readData(ast, root, variables, nestedRefetchQueries) {
|
75
|
-
var _a, _b, _c, _d;
|
76
|
-
let storeRecord =
|
86
|
+
function readData(environment, ast, root, variables, nestedRefetchQueries) {
|
87
|
+
var _a, _b, _c, _d, _e;
|
88
|
+
let storeRecord = environment.store[root];
|
77
89
|
if (storeRecord === undefined) {
|
78
90
|
return { kind: 'MissingData', reason: 'No record for root ' + root };
|
79
91
|
}
|
@@ -119,7 +131,7 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
119
131
|
results.push(null);
|
120
132
|
continue;
|
121
133
|
}
|
122
|
-
const result = readData(field.selections, link.__link, variables, nestedRefetchQueries);
|
134
|
+
const result = readData(environment, field.selections, link.__link, variables, nestedRefetchQueries);
|
123
135
|
if (result.kind === 'MissingData') {
|
124
136
|
return {
|
125
137
|
kind: 'MissingData',
|
@@ -140,6 +152,7 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
140
152
|
let link = assertLink(value);
|
141
153
|
if (link === undefined) {
|
142
154
|
// TODO make this configurable, and also generated and derived from the schema
|
155
|
+
const missingFieldHandler = (_c = environment.missingFieldHandler) !== null && _c !== void 0 ? _c : defaultMissingFieldHandler;
|
143
156
|
const altLink = missingFieldHandler(storeRecord, root, field.fieldName, field.arguments, variables);
|
144
157
|
if (altLink === undefined) {
|
145
158
|
return {
|
@@ -157,11 +170,11 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
157
170
|
}
|
158
171
|
}
|
159
172
|
else if (link === null) {
|
160
|
-
target[(
|
173
|
+
target[(_d = field.alias) !== null && _d !== void 0 ? _d : field.fieldName] = null;
|
161
174
|
break;
|
162
175
|
}
|
163
176
|
const targetId = link.__link;
|
164
|
-
const data = readData(field.selections, targetId, variables, nestedRefetchQueries);
|
177
|
+
const data = readData(environment, field.selections, targetId, variables, nestedRefetchQueries);
|
165
178
|
if (data.kind === 'MissingData') {
|
166
179
|
return {
|
167
180
|
kind: 'MissingData',
|
@@ -169,11 +182,11 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
169
182
|
nestedReason: data,
|
170
183
|
};
|
171
184
|
}
|
172
|
-
target[(
|
185
|
+
target[(_e = field.alias) !== null && _e !== void 0 ? _e : field.fieldName] = data.data;
|
173
186
|
break;
|
174
187
|
}
|
175
188
|
case 'RefetchField': {
|
176
|
-
const data = readData(field.readerArtifact.readerAst, root, variables,
|
189
|
+
const data = readData(environment, field.readerArtifact.readerAst, root, variables,
|
177
190
|
// Refetch fields just read the id, and don't need refetch query artifacts
|
178
191
|
[]);
|
179
192
|
if (typeof window !== 'undefined' && window.__LOG) {
|
@@ -194,12 +207,12 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
194
207
|
const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
|
195
208
|
const refetchQueryArtifact = refetchQuery.artifact;
|
196
209
|
const allowedVariables = refetchQuery.allowedVariables;
|
197
|
-
target[field.alias] = field.readerArtifact.resolver(refetchQueryArtifact, Object.assign(Object.assign({}, data.data), filterVariables(variables, allowedVariables)));
|
210
|
+
target[field.alias] = field.readerArtifact.resolver(environment, refetchQueryArtifact, Object.assign(Object.assign({}, data.data), filterVariables(variables, allowedVariables)));
|
198
211
|
}
|
199
212
|
break;
|
200
213
|
}
|
201
214
|
case 'MutationField': {
|
202
|
-
const data = readData(field.readerArtifact.readerAst, root, variables,
|
215
|
+
const data = readData(environment, field.readerArtifact.readerAst, root, variables,
|
203
216
|
// Refetch fields just read the id, and don't need refetch query artifacts
|
204
217
|
[]);
|
205
218
|
if (typeof window !== 'undefined' && window.__LOG) {
|
@@ -220,7 +233,7 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
220
233
|
const refetchQuery = nestedRefetchQueries[refetchQueryIndex];
|
221
234
|
const refetchQueryArtifact = refetchQuery.artifact;
|
222
235
|
const allowedVariables = refetchQuery.allowedVariables;
|
223
|
-
target[field.alias] = field.readerArtifact.resolver(refetchQueryArtifact, data.data, filterVariables(variables, allowedVariables));
|
236
|
+
target[field.alias] = field.readerArtifact.resolver(environment, refetchQueryArtifact, data.data, filterVariables(variables, allowedVariables));
|
224
237
|
}
|
225
238
|
break;
|
226
239
|
}
|
@@ -229,7 +242,7 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
229
242
|
const resolverRefetchQueries = usedRefetchQueries.map((index) => nestedRefetchQueries[index]);
|
230
243
|
const variant = field.readerArtifact.variant;
|
231
244
|
if (variant.kind === 'Eager') {
|
232
|
-
const data = readData(field.readerArtifact.readerAst, root, variables, resolverRefetchQueries);
|
245
|
+
const data = readData(environment, field.readerArtifact.readerAst, root, variables, resolverRefetchQueries);
|
233
246
|
if (data.kind === 'MissingData') {
|
234
247
|
return {
|
235
248
|
kind: 'MissingData',
|
@@ -242,7 +255,7 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
242
255
|
}
|
243
256
|
}
|
244
257
|
else if (variant.kind === 'Component') {
|
245
|
-
target[field.alias] = (0, componentCache_1.getOrCreateCachedComponent)(root, variant.componentName, field.readerArtifact, variables, resolverRefetchQueries);
|
258
|
+
target[field.alias] = (0, componentCache_1.getOrCreateCachedComponent)(environment, root, variant.componentName, field.readerArtifact, variables, resolverRefetchQueries);
|
246
259
|
}
|
247
260
|
break;
|
248
261
|
}
|
@@ -250,16 +263,7 @@ function readData(ast, root, variables, nestedRefetchQueries) {
|
|
250
263
|
}
|
251
264
|
return { kind: 'Success', data: target };
|
252
265
|
}
|
253
|
-
|
254
|
-
function missingFieldHandler(storeRecord, root, fieldName, arguments_, variables) {
|
255
|
-
if (customMissingFieldHandler != null) {
|
256
|
-
return customMissingFieldHandler(storeRecord, root, fieldName, arguments_, variables);
|
257
|
-
}
|
258
|
-
else {
|
259
|
-
return defaultMissingFieldHandler(storeRecord, root, fieldName, arguments_, variables);
|
260
|
-
}
|
261
|
-
}
|
262
|
-
function defaultMissingFieldHandler(storeRecord, root, fieldName, arguments_, variables) {
|
266
|
+
function defaultMissingFieldHandler(_storeRecord, _root, fieldName, arguments_, variables) {
|
263
267
|
if (fieldName === 'node' || fieldName === 'user') {
|
264
268
|
const variable = arguments_ === null || arguments_ === void 0 ? void 0 : arguments_['id'];
|
265
269
|
const value = variables === null || variables === void 0 ? void 0 : variables[variable];
|
@@ -270,10 +274,6 @@ function defaultMissingFieldHandler(storeRecord, root, fieldName, arguments_, va
|
|
270
274
|
}
|
271
275
|
}
|
272
276
|
exports.defaultMissingFieldHandler = defaultMissingFieldHandler;
|
273
|
-
function setMissingFieldHandler(handler) {
|
274
|
-
customMissingFieldHandler = handler;
|
275
|
-
}
|
276
|
-
exports.setMissingFieldHandler = setMissingFieldHandler;
|
277
277
|
function assertLink(link) {
|
278
278
|
if (Array.isArray(link)) {
|
279
279
|
throw new Error('Unexpected array');
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@isograph/react",
|
3
|
-
"version": "0.0.0-main-
|
3
|
+
"version": "0.0.0-main-38d73d29",
|
4
4
|
"description": "Use Isograph with React",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -15,8 +15,8 @@
|
|
15
15
|
"prepack": "yarn run test && yarn run compile"
|
16
16
|
},
|
17
17
|
"dependencies": {
|
18
|
-
"@isograph/disposable-types": "0.0.0-main-
|
19
|
-
"@isograph/react-disposable-state": "0.0.0-main-
|
18
|
+
"@isograph/disposable-types": "0.0.0-main-38d73d29",
|
19
|
+
"@isograph/react-disposable-state": "0.0.0-main-38d73d29",
|
20
20
|
"react": "^18.2.0"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|