@isograph/react 0.0.0-main-1bfd646b → 0.0.0-main-905800be
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 +5 -0
- package/dist/IsographEnvironment.js +12 -1
- package/dist/garbageCollection.js +7 -16
- package/dist/index.d.ts +3 -7
- package/dist/index.js +7 -16
- package/package.json +3 -3
- package/src/IsographEnvironment.tsx +18 -0
- package/src/garbageCollection.ts +7 -18
- package/src/index.tsx +6 -23
@@ -49,4 +49,9 @@ export declare function createIsographEnvironment(store: IsographStore, networkF
|
|
49
49
|
export declare function createIsographStore(): {
|
50
50
|
__ROOT: {};
|
51
51
|
};
|
52
|
+
export declare function defaultMissingFieldHandler(_storeRecord: StoreRecord, _root: DataId, fieldName: string, arguments_: {
|
53
|
+
[index: string]: any;
|
54
|
+
} | null, variables: {
|
55
|
+
[index: string]: any;
|
56
|
+
} | null): Link | undefined;
|
52
57
|
export {};
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.createIsographStore = exports.createIsographEnvironment = exports.ROOT_ID = void 0;
|
3
|
+
exports.defaultMissingFieldHandler = exports.createIsographStore = exports.createIsographEnvironment = exports.ROOT_ID = void 0;
|
4
4
|
exports.ROOT_ID = '__ROOT';
|
5
5
|
const DEFAULT_GC_BUFFER_SIZE = 10;
|
6
6
|
function createIsographEnvironment(store, networkFunction, missingFieldHandler) {
|
@@ -23,3 +23,14 @@ function createIsographStore() {
|
|
23
23
|
};
|
24
24
|
}
|
25
25
|
exports.createIsographStore = createIsographStore;
|
26
|
+
function defaultMissingFieldHandler(_storeRecord, _root, fieldName, arguments_, variables) {
|
27
|
+
if (fieldName === 'node' || fieldName === 'user') {
|
28
|
+
const variable = arguments_ === null || arguments_ === void 0 ? void 0 : arguments_['id'];
|
29
|
+
const value = variables === null || variables === void 0 ? void 0 : variables[variable];
|
30
|
+
// TODO can we handle explicit nulls here too? Probably, after wrapping in objects
|
31
|
+
if (value != null) {
|
32
|
+
return { __link: value };
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
exports.defaultMissingFieldHandler = defaultMissingFieldHandler;
|
@@ -2,6 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.garbageCollectEnvironment = exports.retainQuery = exports.unretainQuery = void 0;
|
4
4
|
const IsographEnvironment_1 = require("./IsographEnvironment");
|
5
|
+
const index_1 = require("./index");
|
5
6
|
const cache_1 = require("./cache");
|
6
7
|
function unretainQuery(environment, retainedQuery) {
|
7
8
|
environment.retainedQueries.delete(retainedQuery);
|
@@ -38,16 +39,6 @@ exports.garbageCollectEnvironment = garbageCollectEnvironment;
|
|
38
39
|
function recordReachableIds(store, retainedQuery, mutableRetainedIds) {
|
39
40
|
recordReachableIdsFromRecord(store, store[IsographEnvironment_1.ROOT_ID], mutableRetainedIds, retainedQuery.normalizationAst, retainedQuery.variables);
|
40
41
|
}
|
41
|
-
function getLinkedId(data) {
|
42
|
-
// @ts-expect-error
|
43
|
-
if (data.__link != null) {
|
44
|
-
// @ts-expect-error
|
45
|
-
return data.__link;
|
46
|
-
}
|
47
|
-
else {
|
48
|
-
throw new Error('Record in an invalid state');
|
49
|
-
}
|
50
|
-
}
|
51
42
|
function recordReachableIdsFromRecord(store, currentRecord, mutableRetainedIds, selections, variables) {
|
52
43
|
for (const selection of selections) {
|
53
44
|
switch (selection.kind) {
|
@@ -56,17 +47,17 @@ function recordReachableIdsFromRecord(store, currentRecord, mutableRetainedIds,
|
|
56
47
|
const linkedFieldOrFields = currentRecord[linkKey];
|
57
48
|
const ids = [];
|
58
49
|
if (Array.isArray(linkedFieldOrFields)) {
|
59
|
-
for (const
|
50
|
+
for (const maybeLink of linkedFieldOrFields) {
|
51
|
+
const link = (0, index_1.assertLink)(maybeLink);
|
60
52
|
if (link != null) {
|
61
|
-
|
62
|
-
ids.push(id);
|
53
|
+
ids.push(link.__link);
|
63
54
|
}
|
64
55
|
}
|
65
56
|
}
|
66
57
|
else {
|
67
|
-
|
68
|
-
|
69
|
-
ids.push(
|
58
|
+
const link = (0, index_1.assertLink)(linkedFieldOrFields);
|
59
|
+
if (link != null) {
|
60
|
+
ids.push(link.__link);
|
70
61
|
}
|
71
62
|
}
|
72
63
|
for (const nextRecordId of ids) {
|
package/dist/index.d.ts
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
import { DataId, IsographEnvironment, Link
|
1
|
+
import { DataId, DataTypeValue, IsographEnvironment, Link } from './IsographEnvironment';
|
2
2
|
import { ReaderArtifact } from './reader';
|
3
3
|
import { IsographEntrypoint, RefetchQueryArtifactWrapper } from './entrypoint';
|
4
4
|
export { retainQuery, unretainQuery, type RetainedQuery, garbageCollectEnvironment, } from './garbageCollection';
|
5
5
|
export { type PromiseWrapper } from './PromiseWrapper';
|
6
6
|
export { makeNetworkRequest, subscribe } from './cache';
|
7
|
-
export { ROOT_ID, type DataId, type DataTypeValue, type IsographEnvironment, type IsographNetworkFunction, type IsographStore, type Link, type StoreRecord, createIsographEnvironment, createIsographStore, } from './IsographEnvironment';
|
7
|
+
export { ROOT_ID, type DataId, type DataTypeValue, type IsographEnvironment, type IsographNetworkFunction, type IsographStore, type Link, type StoreRecord, createIsographEnvironment, createIsographStore, defaultMissingFieldHandler, } from './IsographEnvironment';
|
8
8
|
export { IsographEnvironmentProvider, useIsographEnvironment, type IsographEnvironmentProviderProps, } from './IsographEnvironmentProvider';
|
9
9
|
export { useImperativeReference } from './useImperativeReference';
|
10
10
|
export { EntrypointReader } from './EntrypointReader';
|
@@ -41,11 +41,7 @@ export declare function useLazyReference<TEntrypoint>(entrypoint: TEntrypoint |
|
|
41
41
|
export declare function useResult<TReadFromStore extends Object, TResolverResult>(fragmentReference: FragmentReference<TReadFromStore, TResolverResult>): TResolverResult;
|
42
42
|
export declare function read<TReadFromStore extends Object, TResolverResult>(environment: IsographEnvironment, fragmentReference: FragmentReference<TReadFromStore, TResolverResult>): TResolverResult;
|
43
43
|
export declare function readButDoNotEvaluate<TReadFromStore extends Object>(environment: IsographEnvironment, reference: FragmentReference<TReadFromStore, unknown>): TReadFromStore;
|
44
|
-
export declare function
|
45
|
-
[index: string]: any;
|
46
|
-
} | null, variables: {
|
47
|
-
[index: string]: any;
|
48
|
-
} | null): Link | undefined;
|
44
|
+
export declare function assertLink(link: DataTypeValue): Link | null;
|
49
45
|
export type IsographComponentProps<TDataType, TOtherProps = Object> = {
|
50
46
|
data: TDataType;
|
51
47
|
} & TOtherProps;
|
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.assertLink = exports.readButDoNotEvaluate = exports.read = exports.useResult = exports.useLazyReference = exports.assertIsEntrypoint = exports.EntrypointReader = exports.useImperativeReference = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.defaultMissingFieldHandler = exports.createIsographStore = exports.createIsographEnvironment = exports.ROOT_ID = exports.subscribe = exports.makeNetworkRequest = exports.garbageCollectEnvironment = exports.unretainQuery = exports.retainQuery = 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");
|
@@ -19,6 +19,7 @@ var IsographEnvironment_2 = require("./IsographEnvironment");
|
|
19
19
|
Object.defineProperty(exports, "ROOT_ID", { enumerable: true, get: function () { return IsographEnvironment_2.ROOT_ID; } });
|
20
20
|
Object.defineProperty(exports, "createIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographEnvironment; } });
|
21
21
|
Object.defineProperty(exports, "createIsographStore", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographStore; } });
|
22
|
+
Object.defineProperty(exports, "defaultMissingFieldHandler", { enumerable: true, get: function () { return IsographEnvironment_2.defaultMissingFieldHandler; } });
|
22
23
|
var IsographEnvironmentProvider_2 = require("./IsographEnvironmentProvider");
|
23
24
|
Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return IsographEnvironmentProvider_2.IsographEnvironmentProvider; } });
|
24
25
|
Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironmentProvider_2.useIsographEnvironment; } });
|
@@ -165,7 +166,7 @@ function readData(environment, ast, root, variables, nestedRefetchQueries) {
|
|
165
166
|
let link = assertLink(value);
|
166
167
|
if (link === undefined) {
|
167
168
|
// TODO make this configurable, and also generated and derived from the schema
|
168
|
-
const missingFieldHandler = (_c = environment.missingFieldHandler) !== null && _c !== void 0 ? _c : defaultMissingFieldHandler;
|
169
|
+
const missingFieldHandler = (_c = environment.missingFieldHandler) !== null && _c !== void 0 ? _c : IsographEnvironment_1.defaultMissingFieldHandler;
|
169
170
|
const altLink = missingFieldHandler(storeRecord, root, field.fieldName, field.arguments, variables);
|
170
171
|
if (altLink === undefined) {
|
171
172
|
return {
|
@@ -281,29 +282,19 @@ function readData(environment, ast, root, variables, nestedRefetchQueries) {
|
|
281
282
|
}
|
282
283
|
return { kind: 'Success', data: target };
|
283
284
|
}
|
284
|
-
function defaultMissingFieldHandler(_storeRecord, _root, fieldName, arguments_, variables) {
|
285
|
-
if (fieldName === 'node' || fieldName === 'user') {
|
286
|
-
const variable = arguments_ === null || arguments_ === void 0 ? void 0 : arguments_['id'];
|
287
|
-
const value = variables === null || variables === void 0 ? void 0 : variables[variable];
|
288
|
-
// TODO can we handle explicit nulls here too? Probably, after wrapping in objects
|
289
|
-
if (value != null) {
|
290
|
-
return { __link: value };
|
291
|
-
}
|
292
|
-
}
|
293
|
-
}
|
294
|
-
exports.defaultMissingFieldHandler = defaultMissingFieldHandler;
|
295
285
|
function assertLink(link) {
|
296
286
|
if (Array.isArray(link)) {
|
297
287
|
throw new Error('Unexpected array');
|
298
288
|
}
|
289
|
+
if (link == null) {
|
290
|
+
return null;
|
291
|
+
}
|
299
292
|
if (typeof link === 'object') {
|
300
293
|
return link;
|
301
294
|
}
|
302
|
-
if (link === undefined) {
|
303
|
-
return undefined;
|
304
|
-
}
|
305
295
|
throw new Error('Invalid link');
|
306
296
|
}
|
297
|
+
exports.assertLink = assertLink;
|
307
298
|
function filterVariables(variables, allowedVariables) {
|
308
299
|
const result = {};
|
309
300
|
for (const key of allowedVariables) {
|
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-905800be",
|
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-
|
20
|
-
"@isograph/react-disposable-state": "0.0.0-main-
|
19
|
+
"@isograph/disposable-types": "0.0.0-main-905800be",
|
20
|
+
"@isograph/react-disposable-state": "0.0.0-main-905800be",
|
21
21
|
"react": "^18.2.0"
|
22
22
|
},
|
23
23
|
"devDependencies": {
|
@@ -95,3 +95,21 @@ export function createIsographStore() {
|
|
95
95
|
[ROOT_ID]: {},
|
96
96
|
};
|
97
97
|
}
|
98
|
+
|
99
|
+
export function defaultMissingFieldHandler(
|
100
|
+
_storeRecord: StoreRecord,
|
101
|
+
_root: DataId,
|
102
|
+
fieldName: string,
|
103
|
+
arguments_: { [index: string]: any } | null,
|
104
|
+
variables: { [index: string]: any } | null,
|
105
|
+
): Link | undefined {
|
106
|
+
if (fieldName === 'node' || fieldName === 'user') {
|
107
|
+
const variable = arguments_?.['id'];
|
108
|
+
const value = variables?.[variable];
|
109
|
+
|
110
|
+
// TODO can we handle explicit nulls here too? Probably, after wrapping in objects
|
111
|
+
if (value != null) {
|
112
|
+
return { __link: value };
|
113
|
+
}
|
114
|
+
}
|
115
|
+
}
|
package/src/garbageCollection.ts
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
import {
|
2
2
|
DataId,
|
3
|
-
DataTypeValue,
|
4
3
|
IsographEnvironment,
|
5
4
|
IsographStore,
|
6
5
|
ROOT_ID,
|
7
6
|
StoreRecord,
|
8
7
|
} from './IsographEnvironment';
|
9
|
-
import { NormalizationAst } from './index';
|
8
|
+
import { NormalizationAst, assertLink } from './index';
|
10
9
|
import { getParentRecordKey } from './cache';
|
11
10
|
|
12
11
|
export type RetainedQuery = {
|
@@ -71,16 +70,6 @@ function recordReachableIds(
|
|
71
70
|
);
|
72
71
|
}
|
73
72
|
|
74
|
-
function getLinkedId(data: Exclude<DataTypeValue, null | void>): string {
|
75
|
-
// @ts-expect-error
|
76
|
-
if (data.__link != null) {
|
77
|
-
// @ts-expect-error
|
78
|
-
return data.__link;
|
79
|
-
} else {
|
80
|
-
throw new Error('Record in an invalid state');
|
81
|
-
}
|
82
|
-
}
|
83
|
-
|
84
73
|
function recordReachableIdsFromRecord(
|
85
74
|
store: IsographStore,
|
86
75
|
currentRecord: StoreRecord,
|
@@ -96,16 +85,16 @@ function recordReachableIdsFromRecord(
|
|
96
85
|
|
97
86
|
const ids = [];
|
98
87
|
if (Array.isArray(linkedFieldOrFields)) {
|
99
|
-
for (const
|
88
|
+
for (const maybeLink of linkedFieldOrFields) {
|
89
|
+
const link = assertLink(maybeLink);
|
100
90
|
if (link != null) {
|
101
|
-
|
102
|
-
ids.push(id);
|
91
|
+
ids.push(link.__link);
|
103
92
|
}
|
104
93
|
}
|
105
94
|
} else {
|
106
|
-
|
107
|
-
|
108
|
-
ids.push(
|
95
|
+
const link = assertLink(linkedFieldOrFields);
|
96
|
+
if (link != null) {
|
97
|
+
ids.push(link.__link);
|
109
98
|
}
|
110
99
|
}
|
111
100
|
|
package/src/index.tsx
CHANGED
@@ -13,7 +13,7 @@ import {
|
|
13
13
|
IsographEnvironment,
|
14
14
|
Link,
|
15
15
|
ROOT_ID,
|
16
|
-
|
16
|
+
defaultMissingFieldHandler,
|
17
17
|
} from './IsographEnvironment';
|
18
18
|
import { useEffect, useState } from 'react';
|
19
19
|
import { useIsographEnvironment } from './IsographEnvironmentProvider';
|
@@ -43,6 +43,7 @@ export {
|
|
43
43
|
type StoreRecord,
|
44
44
|
createIsographEnvironment,
|
45
45
|
createIsographStore,
|
46
|
+
defaultMissingFieldHandler,
|
46
47
|
} from './IsographEnvironment';
|
47
48
|
export {
|
48
49
|
IsographEnvironmentProvider,
|
@@ -486,34 +487,16 @@ function readData<TReadFromStore>(
|
|
486
487
|
return { kind: 'Success', data: target as any };
|
487
488
|
}
|
488
489
|
|
489
|
-
export function
|
490
|
-
_storeRecord: StoreRecord,
|
491
|
-
_root: DataId,
|
492
|
-
fieldName: string,
|
493
|
-
arguments_: { [index: string]: any } | null,
|
494
|
-
variables: { [index: string]: any } | null,
|
495
|
-
): Link | undefined {
|
496
|
-
if (fieldName === 'node' || fieldName === 'user') {
|
497
|
-
const variable = arguments_?.['id'];
|
498
|
-
const value = variables?.[variable];
|
499
|
-
|
500
|
-
// TODO can we handle explicit nulls here too? Probably, after wrapping in objects
|
501
|
-
if (value != null) {
|
502
|
-
return { __link: value };
|
503
|
-
}
|
504
|
-
}
|
505
|
-
}
|
506
|
-
|
507
|
-
function assertLink(link: DataTypeValue): Link | undefined | null {
|
490
|
+
export function assertLink(link: DataTypeValue): Link | null {
|
508
491
|
if (Array.isArray(link)) {
|
509
492
|
throw new Error('Unexpected array');
|
510
493
|
}
|
494
|
+
if (link == null) {
|
495
|
+
return null;
|
496
|
+
}
|
511
497
|
if (typeof link === 'object') {
|
512
498
|
return link;
|
513
499
|
}
|
514
|
-
if (link === undefined) {
|
515
|
-
return undefined;
|
516
|
-
}
|
517
500
|
throw new Error('Invalid link');
|
518
501
|
}
|
519
502
|
|