@isograph/react 0.0.0-main-dc9ca2f6 → 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.
@@ -1,8 +1,6 @@
1
- import { ReactNode } from 'react';
2
- import * as React from 'react';
1
+ /// <reference types="react" />
3
2
  import { ParentCache } from '@isograph/isograph-react-disposable-state';
4
- import { NormalizationAst } from './index';
5
- export declare const IsographEnvironmentContext: React.Context<IsographEnvironment | null>;
3
+ import { RetainedQuery } from './garbageCollection';
6
4
  type ComponentName = string;
7
5
  type StringifiedArgs = string;
8
6
  type ComponentCache = {
@@ -16,10 +14,6 @@ export type Subscriptions = Set<() => void>;
16
14
  type SuspenseCache = {
17
15
  [index: string]: ParentCache<any>;
18
16
  };
19
- export type RetainedQuery = {
20
- normalizationAst: NormalizationAst;
21
- variables: {};
22
- };
23
17
  export type IsographEnvironment = {
24
18
  store: IsographStore;
25
19
  networkFunction: IsographNetworkFunction;
@@ -51,18 +45,13 @@ export type IsographStore = {
51
45
  [index: DataId]: StoreRecord | null;
52
46
  __ROOT: StoreRecord;
53
47
  };
54
- export type IsographEnvironmentProviderProps = {
55
- environment: IsographEnvironment;
56
- children: ReactNode;
57
- };
58
- export declare function IsographEnvironmentProvider({ environment, children, }: IsographEnvironmentProviderProps): React.JSX.Element;
59
- export declare function useIsographEnvironment(): IsographEnvironment;
60
48
  export declare function createIsographEnvironment(store: IsographStore, networkFunction: IsographNetworkFunction, missingFieldHandler?: MissingFieldHandler): IsographEnvironment;
61
49
  export declare function createIsographStore(): {
62
50
  __ROOT: {};
63
51
  };
64
- export declare function garbageCollectEnvironment(environment: IsographEnvironment): void;
65
- type DidUnretainSomeQuery = boolean;
66
- export declare function unretainQuery(environment: IsographEnvironment, retainedQuery: RetainedQuery): DidUnretainSomeQuery;
67
- export declare function retainQuery(environment: IsographEnvironment, queryToRetain: RetainedQuery): void;
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;
68
57
  export {};
@@ -1,47 +1,7 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.retainQuery = exports.unretainQuery = exports.garbageCollectEnvironment = 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);
3
+ exports.defaultMissingFieldHandler = exports.createIsographStore = exports.createIsographEnvironment = exports.ROOT_ID = void 0;
31
4
  exports.ROOT_ID = '__ROOT';
32
- function IsographEnvironmentProvider({ environment, children, }) {
33
- return (React.createElement(exports.IsographEnvironmentContext.Provider, { value: environment }, children));
34
- }
35
- exports.IsographEnvironmentProvider = IsographEnvironmentProvider;
36
- function useIsographEnvironment() {
37
- const context = (0, react_1.useContext)(exports.IsographEnvironmentContext);
38
- if (context == null) {
39
- throw new Error('Unexpected null environment context. Make sure to render ' +
40
- 'this component within an IsographEnvironmentProvider component');
41
- }
42
- return context;
43
- }
44
- exports.useIsographEnvironment = useIsographEnvironment;
45
5
  const DEFAULT_GC_BUFFER_SIZE = 10;
46
6
  function createIsographEnvironment(store, networkFunction, missingFieldHandler) {
47
7
  return {
@@ -63,82 +23,14 @@ function createIsographStore() {
63
23
  };
64
24
  }
65
25
  exports.createIsographStore = createIsographStore;
66
- function garbageCollectEnvironment(environment) {
67
- const retainedIds = new Set([exports.ROOT_ID]);
68
- for (const query of environment.retainedQueries) {
69
- recordReachableIds(environment.store, query, retainedIds);
70
- }
71
- for (const query of environment.gcBuffer) {
72
- recordReachableIds(environment.store, query, retainedIds);
73
- }
74
- for (const dataId in environment.store) {
75
- if (!retainedIds.has(dataId)) {
76
- delete environment.store[dataId];
77
- }
78
- }
79
- }
80
- exports.garbageCollectEnvironment = garbageCollectEnvironment;
81
- function recordReachableIds(store, retainedQuery, mutableRetainedIds) {
82
- recordReachableIdsFromRecord(store, store[exports.ROOT_ID], mutableRetainedIds, retainedQuery.normalizationAst, retainedQuery.variables);
83
- }
84
- function getLinkedId(data) {
85
- // @ts-expect-error
86
- if (data.__link != null) {
87
- // @ts-expect-error
88
- return data.__link;
89
- }
90
- else {
91
- throw new Error('Record in an invalid state');
92
- }
93
- }
94
- function recordReachableIdsFromRecord(store, currentRecord, mutableRetainedIds, selections, variables) {
95
- for (const selection of selections) {
96
- switch (selection.kind) {
97
- case 'Linked':
98
- const linkKey = (0, cache_1.getParentRecordKey)(selection, variables !== null && variables !== void 0 ? variables : {});
99
- const linkedFieldOrFields = currentRecord[linkKey];
100
- const ids = [];
101
- if (Array.isArray(linkedFieldOrFields)) {
102
- for (const link of linkedFieldOrFields) {
103
- if (link != null) {
104
- const id = getLinkedId(link);
105
- ids.push(id);
106
- }
107
- }
108
- }
109
- else {
110
- if (linkedFieldOrFields != null) {
111
- const id = getLinkedId(linkedFieldOrFields);
112
- ids.push(id);
113
- }
114
- }
115
- for (const nextRecordId of ids) {
116
- const nextRecord = store[nextRecordId];
117
- if (nextRecord != null) {
118
- mutableRetainedIds.add(nextRecordId);
119
- recordReachableIdsFromRecord(store, nextRecord, mutableRetainedIds, selection.selections, variables);
120
- }
121
- }
122
- continue;
123
- case 'Scalar':
124
- continue;
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 };
125
33
  }
126
34
  }
127
35
  }
128
- function unretainQuery(environment, retainedQuery) {
129
- environment.retainedQueries.delete(retainedQuery);
130
- environment.gcBuffer.push(retainedQuery);
131
- if (environment.gcBuffer.length > environment.gcBufferSize) {
132
- environment.gcBuffer.shift();
133
- return true;
134
- }
135
- return false;
136
- }
137
- exports.unretainQuery = unretainQuery;
138
- function retainQuery(environment, queryToRetain) {
139
- environment.retainedQueries.add(queryToRetain);
140
- // TODO can we remove this query from the buffer somehow?
141
- // We are relying on === equality, but we really should be comparing
142
- // id + variables
143
- }
144
- exports.retainQuery = retainQuery;
36
+ exports.defaultMissingFieldHandler = defaultMissingFieldHandler;
@@ -0,0 +1,10 @@
1
+ import * as React from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { type IsographEnvironment } from './IsographEnvironment';
4
+ export declare const IsographEnvironmentContext: React.Context<IsographEnvironment | null>;
5
+ export type IsographEnvironmentProviderProps = {
6
+ environment: IsographEnvironment;
7
+ children: ReactNode;
8
+ };
9
+ export declare function IsographEnvironmentProvider({ environment, children, }: IsographEnvironmentProviderProps): React.JSX.Element;
10
+ export declare function useIsographEnvironment(): IsographEnvironment;
@@ -0,0 +1,42 @@
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.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.IsographEnvironmentContext = void 0;
27
+ const React = __importStar(require("react"));
28
+ const react_1 = require("react");
29
+ exports.IsographEnvironmentContext = (0, react_1.createContext)(null);
30
+ function IsographEnvironmentProvider({ environment, children, }) {
31
+ return (React.createElement(exports.IsographEnvironmentContext.Provider, { value: environment }, children));
32
+ }
33
+ exports.IsographEnvironmentProvider = IsographEnvironmentProvider;
34
+ function useIsographEnvironment() {
35
+ const context = (0, react_1.useContext)(exports.IsographEnvironmentContext);
36
+ if (context == null) {
37
+ throw new Error('Unexpected null environment context. Make sure to render ' +
38
+ 'this component within an IsographEnvironmentProvider component');
39
+ }
40
+ return context;
41
+ }
42
+ exports.useIsographEnvironment = useIsographEnvironment;
package/dist/cache.js CHANGED
@@ -4,6 +4,7 @@ exports.SECOND_SPLIT_KEY = exports.FIRST_SPLIT_KEY = exports.getParentRecordKey
4
4
  const react_disposable_state_1 = require("@isograph/react-disposable-state");
5
5
  const PromiseWrapper_1 = require("./PromiseWrapper");
6
6
  const IsographEnvironment_1 = require("./IsographEnvironment");
7
+ const garbageCollection_1 = require("./garbageCollection");
7
8
  function getOrCreateCache(environment, index, factory) {
8
9
  if (typeof window !== 'undefined' && window.__LOG) {
9
10
  console.log('getting cache for', {
@@ -69,7 +70,7 @@ function makeNetworkRequest(environment, artifact, variables) {
69
70
  kind: 'UndisposedComplete',
70
71
  retainedQuery,
71
72
  };
72
- (0, IsographEnvironment_1.retainQuery)(environment, retainedQuery);
73
+ (0, garbageCollection_1.retainQuery)(environment, retainedQuery);
73
74
  }
74
75
  // TODO return null
75
76
  return networkResponse;
@@ -79,9 +80,9 @@ function makeNetworkRequest(environment, artifact, variables) {
79
80
  wrapper,
80
81
  () => {
81
82
  if (status.kind === 'UndisposedComplete') {
82
- const didUnretainSomeQuery = (0, IsographEnvironment_1.unretainQuery)(environment, status.retainedQuery);
83
+ const didUnretainSomeQuery = (0, garbageCollection_1.unretainQuery)(environment, status.retainedQuery);
83
84
  if (didUnretainSomeQuery) {
84
- (0, IsographEnvironment_1.garbageCollectEnvironment)(environment);
85
+ (0, garbageCollection_1.garbageCollectEnvironment)(environment);
85
86
  }
86
87
  }
87
88
  status = {
@@ -0,0 +1,32 @@
1
+ import { Arguments } from './index';
2
+ import { ReaderArtifact } from './reader';
3
+ export type IsographEntrypoint<TReadFromStore extends Object, TResolverResult> = {
4
+ kind: 'Entrypoint';
5
+ queryText: string;
6
+ normalizationAst: NormalizationAst;
7
+ readerArtifact: ReaderArtifact<TReadFromStore, TResolverResult>;
8
+ nestedRefetchQueries: RefetchQueryArtifactWrapper[];
9
+ };
10
+ export type NormalizationAstNode = NormalizationScalarField | NormalizationLinkedField;
11
+ export type NormalizationAst = NormalizationAstNode[];
12
+ export type NormalizationScalarField = {
13
+ kind: 'Scalar';
14
+ fieldName: string;
15
+ arguments: Arguments | null;
16
+ };
17
+ export type NormalizationLinkedField = {
18
+ kind: 'Linked';
19
+ fieldName: string;
20
+ arguments: Arguments | null;
21
+ selections: NormalizationAst;
22
+ };
23
+ export type RefetchQueryArtifact = {
24
+ kind: 'RefetchQuery';
25
+ queryText: string;
26
+ normalizationAst: NormalizationAst;
27
+ };
28
+ export type RefetchQueryArtifactWrapper = {
29
+ artifact: RefetchQueryArtifact;
30
+ allowedVariables: string[];
31
+ };
32
+ export declare function assertIsEntrypoint<TReadFromStore extends Object, TResolverResult>(value: IsographEntrypoint<TReadFromStore, TResolverResult> | ((_: any) => any) | any): asserts value is IsographEntrypoint<TReadFromStore, TResolverResult>;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertIsEntrypoint = void 0;
4
+ function assertIsEntrypoint(value) {
5
+ if (typeof value === 'function')
6
+ throw new Error('Not a string');
7
+ }
8
+ exports.assertIsEntrypoint = assertIsEntrypoint;
@@ -0,0 +1,11 @@
1
+ import { IsographEnvironment } from './IsographEnvironment';
2
+ import { NormalizationAst } from './index';
3
+ export type RetainedQuery = {
4
+ normalizationAst: NormalizationAst;
5
+ variables: {};
6
+ };
7
+ type DidUnretainSomeQuery = boolean;
8
+ export declare function unretainQuery(environment: IsographEnvironment, retainedQuery: RetainedQuery): DidUnretainSomeQuery;
9
+ export declare function retainQuery(environment: IsographEnvironment, queryToRetain: RetainedQuery): void;
10
+ export declare function garbageCollectEnvironment(environment: IsographEnvironment): void;
11
+ export {};
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.garbageCollectEnvironment = exports.retainQuery = exports.unretainQuery = void 0;
4
+ const IsographEnvironment_1 = require("./IsographEnvironment");
5
+ const index_1 = require("./index");
6
+ const cache_1 = require("./cache");
7
+ function unretainQuery(environment, retainedQuery) {
8
+ environment.retainedQueries.delete(retainedQuery);
9
+ environment.gcBuffer.push(retainedQuery);
10
+ if (environment.gcBuffer.length > environment.gcBufferSize) {
11
+ environment.gcBuffer.shift();
12
+ return true;
13
+ }
14
+ return false;
15
+ }
16
+ exports.unretainQuery = unretainQuery;
17
+ function retainQuery(environment, queryToRetain) {
18
+ environment.retainedQueries.add(queryToRetain);
19
+ // TODO can we remove this query from the buffer somehow?
20
+ // We are relying on === equality, but we really should be comparing
21
+ // id + variables
22
+ }
23
+ exports.retainQuery = retainQuery;
24
+ function garbageCollectEnvironment(environment) {
25
+ const retainedIds = new Set([IsographEnvironment_1.ROOT_ID]);
26
+ for (const query of environment.retainedQueries) {
27
+ recordReachableIds(environment.store, query, retainedIds);
28
+ }
29
+ for (const query of environment.gcBuffer) {
30
+ recordReachableIds(environment.store, query, retainedIds);
31
+ }
32
+ for (const dataId in environment.store) {
33
+ if (!retainedIds.has(dataId)) {
34
+ delete environment.store[dataId];
35
+ }
36
+ }
37
+ }
38
+ exports.garbageCollectEnvironment = garbageCollectEnvironment;
39
+ function recordReachableIds(store, retainedQuery, mutableRetainedIds) {
40
+ recordReachableIdsFromRecord(store, store[IsographEnvironment_1.ROOT_ID], mutableRetainedIds, retainedQuery.normalizationAst, retainedQuery.variables);
41
+ }
42
+ function recordReachableIdsFromRecord(store, currentRecord, mutableRetainedIds, selections, variables) {
43
+ for (const selection of selections) {
44
+ switch (selection.kind) {
45
+ case 'Linked':
46
+ const linkKey = (0, cache_1.getParentRecordKey)(selection, variables !== null && variables !== void 0 ? variables : {});
47
+ const linkedFieldOrFields = currentRecord[linkKey];
48
+ const ids = [];
49
+ if (Array.isArray(linkedFieldOrFields)) {
50
+ for (const maybeLink of linkedFieldOrFields) {
51
+ const link = (0, index_1.assertLink)(maybeLink);
52
+ if (link != null) {
53
+ ids.push(link.__link);
54
+ }
55
+ }
56
+ }
57
+ else {
58
+ const link = (0, index_1.assertLink)(linkedFieldOrFields);
59
+ if (link != null) {
60
+ ids.push(link.__link);
61
+ }
62
+ }
63
+ for (const nextRecordId of ids) {
64
+ const nextRecord = store[nextRecordId];
65
+ if (nextRecord != null) {
66
+ mutableRetainedIds.add(nextRecordId);
67
+ recordReachableIdsFromRecord(store, nextRecord, mutableRetainedIds, selection.selections, variables);
68
+ }
69
+ }
70
+ continue;
71
+ case 'Scalar':
72
+ continue;
73
+ }
74
+ }
75
+ }
package/dist/index.d.ts CHANGED
@@ -1,85 +1,16 @@
1
- import { DataId, IsographEnvironment, Link, StoreRecord } from './IsographEnvironment';
1
+ import { DataId, DataTypeValue, IsographEnvironment, Link } from './IsographEnvironment';
2
+ import { ReaderArtifact } from './reader';
3
+ import { IsographEntrypoint, RefetchQueryArtifactWrapper } from './entrypoint';
4
+ export { retainQuery, unretainQuery, type RetainedQuery, garbageCollectEnvironment, } from './garbageCollection';
2
5
  export { type PromiseWrapper } from './PromiseWrapper';
3
6
  export { makeNetworkRequest, subscribe } from './cache';
4
- 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';
7
+ export { ROOT_ID, type DataId, type DataTypeValue, type IsographEnvironment, type IsographNetworkFunction, type IsographStore, type Link, type StoreRecord, createIsographEnvironment, createIsographStore, defaultMissingFieldHandler, } from './IsographEnvironment';
8
+ export { IsographEnvironmentProvider, useIsographEnvironment, type IsographEnvironmentProviderProps, } from './IsographEnvironmentProvider';
5
9
  export { useImperativeReference } from './useImperativeReference';
6
10
  export { EntrypointReader } from './EntrypointReader';
7
- export type IsographEntrypoint<TReadFromStore extends Object, TResolverResult> = {
8
- kind: 'Entrypoint';
9
- queryText: string;
10
- normalizationAst: NormalizationAst;
11
- readerArtifact: ReaderArtifact<TReadFromStore, TResolverResult>;
12
- nestedRefetchQueries: RefetchQueryArtifactWrapper[];
13
- };
14
- export type ReaderArtifact<TReadFromStore extends Object, TResolverResult> = {
15
- kind: 'ReaderArtifact';
16
- readerAst: ReaderAst<TReadFromStore>;
17
- resolver: (data: TReadFromStore, runtimeProps: any) => TResolverResult;
18
- variant: ReaderResolverVariant;
19
- };
20
- export type ReaderAstNode = ReaderScalarField | ReaderLinkedField | ReaderResolverField | ReaderRefetchField | ReaderMutationField;
21
- export type ReaderAst<TReadFromStore> = ReaderAstNode[];
11
+ export { type ReaderArtifact, ReaderAst, ReaderAstNode, ReaderLinkedField, ReaderMutationField, ReaderRefetchField, ReaderResolverField, ReaderResolverVariant, ReaderScalarField, } from './reader';
12
+ export { NormalizationAst, NormalizationAstNode, NormalizationLinkedField, NormalizationScalarField, IsographEntrypoint, assertIsEntrypoint, RefetchQueryArtifact, RefetchQueryArtifactWrapper, } from './entrypoint';
22
13
  export type ExtractSecondParam<T extends (arg1: any, arg2: any) => any> = T extends (arg1: any, arg2: infer P) => any ? P : never;
23
- export type ReaderScalarField = {
24
- kind: 'Scalar';
25
- fieldName: string;
26
- alias: string | null;
27
- arguments: Arguments | null;
28
- };
29
- export type ReaderLinkedField = {
30
- kind: 'Linked';
31
- fieldName: string;
32
- alias: string | null;
33
- selections: ReaderAst<unknown>;
34
- arguments: Arguments | null;
35
- };
36
- export type ReaderResolverVariant = {
37
- kind: 'Eager';
38
- } | {
39
- kind: 'Component';
40
- componentName: string;
41
- };
42
- export type ReaderResolverField = {
43
- kind: 'Resolver';
44
- alias: string;
45
- readerArtifact: ReaderArtifact<any, any>;
46
- arguments: Arguments | null;
47
- usedRefetchQueries: number[];
48
- };
49
- export type ReaderRefetchField = {
50
- kind: 'RefetchField';
51
- alias: string;
52
- readerArtifact: ReaderArtifact<any, any>;
53
- refetchQuery: number;
54
- };
55
- export type ReaderMutationField = {
56
- kind: 'MutationField';
57
- alias: string;
58
- readerArtifact: ReaderArtifact<any, any>;
59
- refetchQuery: number;
60
- };
61
- export type NormalizationAstNode = NormalizationScalarField | NormalizationLinkedField;
62
- export type NormalizationAst = NormalizationAstNode[];
63
- export type NormalizationScalarField = {
64
- kind: 'Scalar';
65
- fieldName: string;
66
- arguments: Arguments | null;
67
- };
68
- export type NormalizationLinkedField = {
69
- kind: 'Linked';
70
- fieldName: string;
71
- arguments: Arguments | null;
72
- selections: NormalizationAst;
73
- };
74
- export type RefetchQueryArtifact = {
75
- kind: 'RefetchQuery';
76
- queryText: string;
77
- normalizationAst: NormalizationAst;
78
- };
79
- export type RefetchQueryArtifactWrapper = {
80
- artifact: RefetchQueryArtifact;
81
- allowedVariables: string[];
82
- };
83
14
  export type Arguments = Argument[];
84
15
  export type Argument = [ArgumentName, ArgumentValue];
85
16
  export type ArgumentName = string;
@@ -110,11 +41,7 @@ export declare function useLazyReference<TEntrypoint>(entrypoint: TEntrypoint |
110
41
  export declare function useResult<TReadFromStore extends Object, TResolverResult>(fragmentReference: FragmentReference<TReadFromStore, TResolverResult>): TResolverResult;
111
42
  export declare function read<TReadFromStore extends Object, TResolverResult>(environment: IsographEnvironment, fragmentReference: FragmentReference<TReadFromStore, TResolverResult>): TResolverResult;
112
43
  export declare function readButDoNotEvaluate<TReadFromStore extends Object>(environment: IsographEnvironment, reference: FragmentReference<TReadFromStore, unknown>): TReadFromStore;
113
- export declare function defaultMissingFieldHandler(_storeRecord: StoreRecord, _root: DataId, fieldName: string, arguments_: {
114
- [index: string]: any;
115
- } | null, variables: {
116
- [index: string]: any;
117
- } | null): Link | undefined;
44
+ export declare function assertLink(link: DataTypeValue): Link | null;
118
45
  export type IsographComponentProps<TDataType, TOtherProps = Object> = {
119
46
  data: TDataType;
120
47
  } & TOtherProps;
package/dist/index.js CHANGED
@@ -1,36 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultMissingFieldHandler = exports.readButDoNotEvaluate = exports.read = exports.useResult = exports.useLazyReference = exports.EntrypointReader = exports.useImperativeReference = exports.createIsographStore = exports.createIsographEnvironment = exports.useIsographEnvironment = exports.IsographEnvironmentProvider = exports.ROOT_ID = exports.IsographEnvironmentContext = exports.subscribe = exports.makeNetworkRequest = void 0;
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");
7
7
  const IsographEnvironment_1 = require("./IsographEnvironment");
8
8
  const react_1 = require("react");
9
+ const IsographEnvironmentProvider_1 = require("./IsographEnvironmentProvider");
10
+ const entrypoint_1 = require("./entrypoint");
11
+ var garbageCollection_1 = require("./garbageCollection");
12
+ Object.defineProperty(exports, "retainQuery", { enumerable: true, get: function () { return garbageCollection_1.retainQuery; } });
13
+ Object.defineProperty(exports, "unretainQuery", { enumerable: true, get: function () { return garbageCollection_1.unretainQuery; } });
14
+ Object.defineProperty(exports, "garbageCollectEnvironment", { enumerable: true, get: function () { return garbageCollection_1.garbageCollectEnvironment; } });
9
15
  var cache_2 = require("./cache");
10
16
  Object.defineProperty(exports, "makeNetworkRequest", { enumerable: true, get: function () { return cache_2.makeNetworkRequest; } });
11
17
  Object.defineProperty(exports, "subscribe", { enumerable: true, get: function () { return cache_2.subscribe; } });
12
18
  var IsographEnvironment_2 = require("./IsographEnvironment");
13
- Object.defineProperty(exports, "IsographEnvironmentContext", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentContext; } });
14
19
  Object.defineProperty(exports, "ROOT_ID", { enumerable: true, get: function () { return IsographEnvironment_2.ROOT_ID; } });
15
- Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return IsographEnvironment_2.IsographEnvironmentProvider; } });
16
- Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.useIsographEnvironment; } });
17
20
  Object.defineProperty(exports, "createIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironment_2.createIsographEnvironment; } });
18
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; } });
23
+ var IsographEnvironmentProvider_2 = require("./IsographEnvironmentProvider");
24
+ Object.defineProperty(exports, "IsographEnvironmentProvider", { enumerable: true, get: function () { return IsographEnvironmentProvider_2.IsographEnvironmentProvider; } });
25
+ Object.defineProperty(exports, "useIsographEnvironment", { enumerable: true, get: function () { return IsographEnvironmentProvider_2.useIsographEnvironment; } });
19
26
  var useImperativeReference_1 = require("./useImperativeReference");
20
27
  Object.defineProperty(exports, "useImperativeReference", { enumerable: true, get: function () { return useImperativeReference_1.useImperativeReference; } });
21
28
  var EntrypointReader_1 = require("./EntrypointReader");
22
29
  Object.defineProperty(exports, "EntrypointReader", { enumerable: true, get: function () { return EntrypointReader_1.EntrypointReader; } });
23
- function assertIsEntrypoint(value) {
24
- if (typeof value === 'function')
25
- throw new Error('Not a string');
26
- }
30
+ var entrypoint_2 = require("./entrypoint");
31
+ Object.defineProperty(exports, "assertIsEntrypoint", { enumerable: true, get: function () { return entrypoint_2.assertIsEntrypoint; } });
27
32
  // Note: we cannot write TEntrypoint extends IsographEntrypoint<any, any, any>, or else
28
33
  // if we do not explicitly pass a type, the read out type will be any.
29
34
  // We cannot write TEntrypoint extends IsographEntrypoint<never, never, never>, or else
30
35
  // any actual Entrypoint we pass will not be valid.
31
36
  function useLazyReference(entrypoint, variables) {
32
- const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
33
- assertIsEntrypoint(entrypoint);
37
+ const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
38
+ (0, entrypoint_1.assertIsEntrypoint)(entrypoint);
34
39
  const cache = (0, cache_1.getOrCreateCacheForArtifact)(environment, entrypoint, variables);
35
40
  // TODO add comment explaining why we never use this value
36
41
  // @ts-ignore
@@ -47,7 +52,7 @@ function useLazyReference(entrypoint, variables) {
47
52
  }
48
53
  exports.useLazyReference = useLazyReference;
49
54
  function useResult(fragmentReference) {
50
- const environment = (0, IsographEnvironment_1.useIsographEnvironment)();
55
+ const environment = (0, IsographEnvironmentProvider_1.useIsographEnvironment)();
51
56
  const [, setState] = (0, react_1.useState)();
52
57
  (0, react_1.useEffect)(() => {
53
58
  return (0, cache_1.subscribe)(environment, () => {
@@ -161,7 +166,7 @@ function readData(environment, ast, root, variables, nestedRefetchQueries) {
161
166
  let link = assertLink(value);
162
167
  if (link === undefined) {
163
168
  // TODO make this configurable, and also generated and derived from the schema
164
- 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;
165
170
  const altLink = missingFieldHandler(storeRecord, root, field.fieldName, field.arguments, variables);
166
171
  if (altLink === undefined) {
167
172
  return {
@@ -277,29 +282,19 @@ function readData(environment, ast, root, variables, nestedRefetchQueries) {
277
282
  }
278
283
  return { kind: 'Success', data: target };
279
284
  }
280
- function defaultMissingFieldHandler(_storeRecord, _root, fieldName, arguments_, variables) {
281
- if (fieldName === 'node' || fieldName === 'user') {
282
- const variable = arguments_ === null || arguments_ === void 0 ? void 0 : arguments_['id'];
283
- const value = variables === null || variables === void 0 ? void 0 : variables[variable];
284
- // TODO can we handle explicit nulls here too? Probably, after wrapping in objects
285
- if (value != null) {
286
- return { __link: value };
287
- }
288
- }
289
- }
290
- exports.defaultMissingFieldHandler = defaultMissingFieldHandler;
291
285
  function assertLink(link) {
292
286
  if (Array.isArray(link)) {
293
287
  throw new Error('Unexpected array');
294
288
  }
289
+ if (link == null) {
290
+ return null;
291
+ }
295
292
  if (typeof link === 'object') {
296
293
  return link;
297
294
  }
298
- if (link === undefined) {
299
- return undefined;
300
- }
301
295
  throw new Error('Invalid link');
302
296
  }
297
+ exports.assertLink = assertLink;
303
298
  function filterVariables(variables, allowedVariables) {
304
299
  const result = {};
305
300
  for (const key of allowedVariables) {
@@ -0,0 +1,47 @@
1
+ import { Arguments } from './index';
2
+ export type ReaderArtifact<TReadFromStore extends Object, TResolverResult> = {
3
+ kind: 'ReaderArtifact';
4
+ readerAst: ReaderAst<TReadFromStore>;
5
+ resolver: (data: TReadFromStore, runtimeProps: any) => TResolverResult;
6
+ variant: ReaderResolverVariant;
7
+ };
8
+ export type ReaderAstNode = ReaderScalarField | ReaderLinkedField | ReaderResolverField | ReaderRefetchField | ReaderMutationField;
9
+ export type ReaderAst<TReadFromStore> = ReaderAstNode[];
10
+ export type ReaderScalarField = {
11
+ kind: 'Scalar';
12
+ fieldName: string;
13
+ alias: string | null;
14
+ arguments: Arguments | null;
15
+ };
16
+ export type ReaderLinkedField = {
17
+ kind: 'Linked';
18
+ fieldName: string;
19
+ alias: string | null;
20
+ selections: ReaderAst<unknown>;
21
+ arguments: Arguments | null;
22
+ };
23
+ export type ReaderResolverVariant = {
24
+ kind: 'Eager';
25
+ } | {
26
+ kind: 'Component';
27
+ componentName: string;
28
+ };
29
+ export type ReaderResolverField = {
30
+ kind: 'Resolver';
31
+ alias: string;
32
+ readerArtifact: ReaderArtifact<any, any>;
33
+ arguments: Arguments | null;
34
+ usedRefetchQueries: number[];
35
+ };
36
+ export type ReaderRefetchField = {
37
+ kind: 'RefetchField';
38
+ alias: string;
39
+ readerArtifact: ReaderArtifact<any, any>;
40
+ refetchQuery: number;
41
+ };
42
+ export type ReaderMutationField = {
43
+ kind: 'MutationField';
44
+ alias: string;
45
+ readerArtifact: ReaderArtifact<any, any>;
46
+ refetchQuery: number;
47
+ };