houdini 1.0.0-next.1 → 1.0.0-next.3

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.
Files changed (224) hide show
  1. package/build/cmd/generate.d.ts +0 -1
  2. package/build/cmd-cjs/index.js +1400 -2836
  3. package/build/cmd-esm/index.js +1400 -2836
  4. package/build/codegen/generators/artifacts/fieldKey.d.ts +1 -1
  5. package/build/codegen/generators/artifacts/index.d.ts +1 -1
  6. package/build/codegen/generators/artifacts/indexFile.d.ts +1 -1
  7. package/build/codegen/generators/artifacts/inputs.d.ts +2 -2
  8. package/build/codegen/generators/artifacts/operations.d.ts +2 -2
  9. package/build/codegen/generators/artifacts/selection.d.ts +1 -1
  10. package/build/codegen/generators/artifacts/utils.d.ts +1 -1
  11. package/build/codegen/generators/definitions/enums.d.ts +1 -1
  12. package/build/codegen/generators/definitions/index.d.ts +1 -1
  13. package/build/codegen/generators/indexFile/index.d.ts +1 -1
  14. package/build/codegen/generators/persistedQueries/index.d.ts +1 -1
  15. package/build/codegen/generators/runtime/graphqlFunction.d.ts +2 -0
  16. package/build/codegen/generators/runtime/index.d.ts +1 -1
  17. package/build/codegen/generators/runtime/injectPlugins.d.ts +7 -0
  18. package/build/codegen/generators/runtime/pluginIndex.d.ts +5 -0
  19. package/build/codegen/generators/typescript/addReferencedInputTypes.d.ts +1 -1
  20. package/build/codegen/generators/typescript/documentTypes.d.ts +2 -0
  21. package/build/codegen/generators/typescript/{imperativeCache.d.ts → imperativeTypeDef.d.ts} +1 -1
  22. package/build/codegen/generators/typescript/index.d.ts +1 -1
  23. package/build/codegen/generators/typescript/inlineType.d.ts +1 -1
  24. package/build/codegen/generators/typescript/typeReference.d.ts +1 -1
  25. package/build/codegen/generators/typescript/types.d.ts +1 -1
  26. package/build/codegen/index.d.ts +1 -1
  27. package/build/codegen/transforms/addID.d.ts +1 -1
  28. package/build/codegen/transforms/composeQueries.d.ts +2 -2
  29. package/build/codegen/transforms/fragmentVariables.d.ts +3 -2
  30. package/build/codegen/transforms/list.d.ts +1 -1
  31. package/build/codegen/transforms/paginate.d.ts +1 -1
  32. package/build/codegen/transforms/schema.d.ts +1 -1
  33. package/build/codegen/transforms/typename.d.ts +1 -1
  34. package/build/codegen/utils/commonjs.d.ts +2 -0
  35. package/build/codegen/utils/flattenSelections.d.ts +2 -2
  36. package/build/codegen/utils/moduleExport.d.ts +1 -1
  37. package/build/codegen/utils/objectIdentificationSelection.d.ts +1 -1
  38. package/build/codegen/validators/noIDAlias.d.ts +1 -1
  39. package/build/codegen/validators/typeCheck.d.ts +1 -1
  40. package/build/codegen-cjs/index.js +1320 -2779
  41. package/build/codegen-esm/index.js +1320 -2779
  42. package/build/lib/config.d.ts +9 -4
  43. package/build/lib/fs.d.ts +1 -1
  44. package/build/lib/graphql.d.ts +1 -1
  45. package/build/lib/imports.d.ts +1 -1
  46. package/build/lib/types.d.ts +3 -3
  47. package/build/lib/walk.d.ts +8 -5
  48. package/build/lib-cjs/index.js +155 -1856
  49. package/build/lib-esm/index.js +155 -1856
  50. package/build/runtime/cache/cache.d.ts +10 -8
  51. package/build/runtime/cache/gc.d.ts +1 -1
  52. package/build/runtime/cache/lists.d.ts +3 -2
  53. package/build/runtime/cache/schema.d.ts +2 -2
  54. package/build/runtime/cache/stuff.d.ts +2 -2
  55. package/build/runtime/cache/subscription.d.ts +19 -11
  56. package/build/runtime/client/documentStore.d.ts +87 -0
  57. package/build/runtime/client/index.d.ts +25 -0
  58. package/build/runtime/client/plugins/cache.d.ts +8 -0
  59. package/build/runtime/client/plugins/fetch.d.ts +37 -0
  60. package/build/runtime/client/plugins/fetchParams.d.ts +9 -0
  61. package/build/runtime/client/plugins/index.d.ts +7 -0
  62. package/build/runtime/client/plugins/injectedPlugins.d.ts +3 -0
  63. package/build/runtime/client/plugins/mutation.d.ts +1 -0
  64. package/build/runtime/client/plugins/query.d.ts +2 -0
  65. package/build/runtime/client/plugins/subscription.d.ts +18 -0
  66. package/build/runtime/client/plugins/throwOnError.d.ts +7 -0
  67. package/build/runtime/client/utils/documentPlugins.d.ts +3 -0
  68. package/build/runtime/client/utils/index.d.ts +1 -0
  69. package/build/runtime/generated.d.ts +1 -0
  70. package/build/runtime/imports/config.d.ts +3 -0
  71. package/build/runtime/index.d.ts +3 -2
  72. package/build/runtime/lib/config.d.ts +26 -25
  73. package/build/runtime/lib/index.d.ts +1 -2
  74. package/build/runtime/lib/scalars.d.ts +6 -5
  75. package/build/runtime/lib/selection.d.ts +1 -1
  76. package/build/runtime/lib/store.d.ts +19 -0
  77. package/build/runtime/lib/types.d.ts +22 -9
  78. package/build/runtime/public/cache.d.ts +20 -7
  79. package/build/runtime/public/list.d.ts +2 -2
  80. package/build/runtime/public/record.d.ts +18 -18
  81. package/build/runtime/public/tests/test.d.ts +44 -1
  82. package/build/runtime/public/types.d.ts +8 -0
  83. package/build/runtime-cjs/cache/cache.d.ts +10 -8
  84. package/build/runtime-cjs/cache/cache.js +5 -11
  85. package/build/runtime-cjs/cache/gc.d.ts +1 -1
  86. package/build/runtime-cjs/cache/lists.d.ts +3 -2
  87. package/build/runtime-cjs/cache/lists.js +5 -2
  88. package/build/runtime-cjs/cache/schema.d.ts +2 -2
  89. package/build/runtime-cjs/cache/stuff.d.ts +2 -2
  90. package/build/runtime-cjs/cache/subscription.d.ts +19 -11
  91. package/build/runtime-cjs/cache/subscription.js +95 -56
  92. package/build/runtime-cjs/client/documentStore.d.ts +87 -0
  93. package/build/runtime-cjs/client/documentStore.js +360 -0
  94. package/build/runtime-cjs/client/index.d.ts +25 -0
  95. package/build/runtime-cjs/client/index.js +87 -0
  96. package/build/runtime-cjs/client/plugins/cache.d.ts +8 -0
  97. package/build/runtime-cjs/client/plugins/cache.js +99 -0
  98. package/build/runtime-cjs/client/plugins/fetch.d.ts +37 -0
  99. package/build/runtime-cjs/{lib/networkUtils.js → client/plugins/fetch.js} +95 -3
  100. package/build/runtime-cjs/client/plugins/fetchParams.d.ts +9 -0
  101. package/build/runtime-cjs/client/plugins/fetchParams.js +40 -0
  102. package/build/runtime-cjs/client/plugins/index.d.ts +7 -0
  103. package/build/runtime-cjs/client/plugins/index.js +24 -0
  104. package/build/runtime-cjs/client/plugins/injectedPlugins.d.ts +3 -0
  105. package/build/runtime-cjs/{lib/errors.js → client/plugins/injectedPlugins.js} +7 -15
  106. package/build/runtime-cjs/client/plugins/mutation.d.ts +1 -0
  107. package/build/runtime-cjs/client/plugins/mutation.js +86 -0
  108. package/build/runtime-cjs/client/plugins/query.d.ts +2 -0
  109. package/build/runtime-cjs/client/plugins/query.js +83 -0
  110. package/build/runtime-cjs/client/plugins/subscription.d.ts +18 -0
  111. package/build/runtime-cjs/client/plugins/subscription.js +98 -0
  112. package/build/runtime-cjs/client/plugins/throwOnError.d.ts +7 -0
  113. package/build/runtime-cjs/client/plugins/throwOnError.js +47 -0
  114. package/build/runtime-cjs/client/utils/documentPlugins.d.ts +3 -0
  115. package/build/runtime-cjs/client/utils/documentPlugins.js +56 -0
  116. package/build/runtime-cjs/client/utils/index.d.ts +1 -0
  117. package/build/runtime-cjs/client/utils/index.js +18 -0
  118. package/build/runtime-cjs/generated.d.ts +1 -0
  119. package/build/runtime-cjs/imports/config.d.ts +3 -0
  120. package/build/runtime-cjs/imports/config.js +26 -0
  121. package/build/runtime-cjs/index.d.ts +3 -2
  122. package/build/runtime-cjs/index.js +1 -0
  123. package/build/runtime-cjs/lib/config.d.ts +26 -25
  124. package/build/runtime-cjs/lib/config.js +5 -4
  125. package/build/runtime-cjs/lib/index.d.ts +1 -2
  126. package/build/runtime-cjs/lib/index.js +1 -2
  127. package/build/runtime-cjs/lib/scalars.d.ts +6 -5
  128. package/build/runtime-cjs/lib/scalars.js +20 -24
  129. package/build/runtime-cjs/lib/selection.d.ts +1 -1
  130. package/build/runtime-cjs/lib/store.d.ts +19 -0
  131. package/build/runtime-cjs/lib/store.js +81 -0
  132. package/build/runtime-cjs/lib/types.d.ts +22 -9
  133. package/build/runtime-cjs/public/cache.d.ts +20 -7
  134. package/build/runtime-cjs/public/cache.js +29 -32
  135. package/build/runtime-cjs/public/list.d.ts +2 -2
  136. package/build/runtime-cjs/public/list.js +6 -28
  137. package/build/runtime-cjs/public/record.d.ts +18 -18
  138. package/build/runtime-cjs/public/record.js +23 -189
  139. package/build/runtime-cjs/public/tests/test.d.ts +44 -1
  140. package/build/runtime-cjs/public/tests/test.js +27 -2
  141. package/build/runtime-cjs/public/types.d.ts +8 -0
  142. package/build/runtime-esm/cache/cache.d.ts +10 -8
  143. package/build/runtime-esm/cache/cache.js +5 -11
  144. package/build/runtime-esm/cache/gc.d.ts +1 -1
  145. package/build/runtime-esm/cache/lists.d.ts +3 -2
  146. package/build/runtime-esm/cache/lists.js +5 -2
  147. package/build/runtime-esm/cache/schema.d.ts +2 -2
  148. package/build/runtime-esm/cache/stuff.d.ts +2 -2
  149. package/build/runtime-esm/cache/subscription.d.ts +19 -11
  150. package/build/runtime-esm/cache/subscription.js +95 -56
  151. package/build/runtime-esm/client/documentStore.d.ts +87 -0
  152. package/build/runtime-esm/client/documentStore.js +336 -0
  153. package/build/runtime-esm/client/index.d.ts +25 -0
  154. package/build/runtime-esm/client/index.js +58 -0
  155. package/build/runtime-esm/client/plugins/cache.d.ts +8 -0
  156. package/build/runtime-esm/client/plugins/cache.js +69 -0
  157. package/build/runtime-esm/client/plugins/fetch.d.ts +37 -0
  158. package/build/runtime-esm/client/plugins/fetch.js +151 -0
  159. package/build/runtime-esm/client/plugins/fetchParams.d.ts +9 -0
  160. package/build/runtime-esm/client/plugins/fetchParams.js +16 -0
  161. package/build/runtime-esm/client/plugins/index.d.ts +7 -0
  162. package/build/runtime-esm/client/plugins/index.js +7 -0
  163. package/build/runtime-esm/client/plugins/injectedPlugins.d.ts +3 -0
  164. package/build/runtime-esm/client/plugins/injectedPlugins.js +5 -0
  165. package/build/runtime-esm/client/plugins/mutation.d.ts +1 -0
  166. package/build/runtime-esm/client/plugins/mutation.js +56 -0
  167. package/build/runtime-esm/client/plugins/query.d.ts +2 -0
  168. package/build/runtime-esm/client/plugins/query.js +53 -0
  169. package/build/runtime-esm/client/plugins/subscription.d.ts +18 -0
  170. package/build/runtime-esm/client/plugins/subscription.js +74 -0
  171. package/build/runtime-esm/client/plugins/throwOnError.d.ts +7 -0
  172. package/build/runtime-esm/client/plugins/throwOnError.js +23 -0
  173. package/build/runtime-esm/client/utils/documentPlugins.d.ts +3 -0
  174. package/build/runtime-esm/client/utils/documentPlugins.js +32 -0
  175. package/build/runtime-esm/client/utils/index.d.ts +1 -0
  176. package/build/runtime-esm/client/utils/index.js +1 -0
  177. package/build/runtime-esm/generated.d.ts +1 -0
  178. package/build/runtime-esm/imports/config.d.ts +3 -0
  179. package/build/runtime-esm/imports/config.js +4 -0
  180. package/build/runtime-esm/index.d.ts +3 -2
  181. package/build/runtime-esm/index.js +1 -0
  182. package/build/runtime-esm/lib/config.d.ts +26 -25
  183. package/build/runtime-esm/lib/config.js +5 -4
  184. package/build/runtime-esm/lib/index.d.ts +1 -2
  185. package/build/runtime-esm/lib/index.js +1 -2
  186. package/build/runtime-esm/lib/scalars.d.ts +6 -5
  187. package/build/runtime-esm/lib/scalars.js +20 -24
  188. package/build/runtime-esm/lib/selection.d.ts +1 -1
  189. package/build/runtime-esm/lib/store.d.ts +19 -0
  190. package/build/runtime-esm/lib/store.js +57 -0
  191. package/build/runtime-esm/lib/types.d.ts +22 -9
  192. package/build/runtime-esm/public/cache.d.ts +20 -7
  193. package/build/runtime-esm/public/cache.js +28 -30
  194. package/build/runtime-esm/public/list.d.ts +2 -2
  195. package/build/runtime-esm/public/list.js +6 -28
  196. package/build/runtime-esm/public/record.d.ts +18 -18
  197. package/build/runtime-esm/public/record.js +22 -185
  198. package/build/runtime-esm/public/tests/test.d.ts +44 -1
  199. package/build/runtime-esm/public/tests/test.js +26 -1
  200. package/build/runtime-esm/public/types.d.ts +8 -0
  201. package/build/test/index.d.ts +4 -3
  202. package/build/test-cjs/index.js +1334 -2766
  203. package/build/test-esm/index.js +1334 -2766
  204. package/build/vite/ast.d.ts +1 -1
  205. package/build/vite/houdini.d.ts +1 -1
  206. package/build/vite/imports.d.ts +3 -3
  207. package/build/vite/index.d.ts +1 -1
  208. package/build/vite/schema.d.ts +1 -1
  209. package/build/vite-cjs/index.js +1395 -2821
  210. package/build/vite-esm/index.js +1395 -2821
  211. package/package.json +1 -1
  212. package/build/runtime/lib/errors.d.ts +0 -3
  213. package/build/runtime/lib/network.d.ts +0 -79
  214. package/build/runtime/lib/networkUtils.d.ts +0 -8
  215. package/build/runtime-cjs/lib/errors.d.ts +0 -3
  216. package/build/runtime-cjs/lib/network.d.ts +0 -79
  217. package/build/runtime-cjs/lib/network.js +0 -200
  218. package/build/runtime-cjs/lib/networkUtils.d.ts +0 -8
  219. package/build/runtime-esm/lib/errors.d.ts +0 -3
  220. package/build/runtime-esm/lib/errors.js +0 -11
  221. package/build/runtime-esm/lib/network.d.ts +0 -79
  222. package/build/runtime-esm/lib/network.js +0 -170
  223. package/build/runtime-esm/lib/networkUtils.d.ts +0 -8
  224. package/build/runtime-esm/lib/networkUtils.js +0 -60
@@ -18,16 +18,29 @@ class InMemorySubscriptions {
18
18
  const __typename = this.cache._internal_unstable.storage.get(parent, "__typename").value;
19
19
  let targetSelection = getFieldsForType(selection, __typename);
20
20
  for (const fieldSelection of Object.values(targetSelection || {})) {
21
- const { keyRaw, selection: innerSelection, type } = fieldSelection;
21
+ const { keyRaw, selection: innerSelection, type, list, filters } = fieldSelection;
22
22
  const key = evaluateKey(keyRaw, variables);
23
+ let targetSelection2;
24
+ if (innerSelection) {
25
+ const __typename2 = this.cache._internal_unstable.storage.get(parent, "__typename").value;
26
+ targetSelection2 = getFieldsForType(innerSelection, __typename2);
27
+ }
23
28
  this.addFieldSubscription({
24
29
  id: parent,
25
30
  key,
26
- field: fieldSelection,
27
- spec,
28
- parentType: parentType || spec.rootType,
29
- variables
31
+ selection: [spec, targetSelection2]
30
32
  });
33
+ if (list) {
34
+ this.registerList({
35
+ list,
36
+ filters,
37
+ id: parent,
38
+ key,
39
+ variables,
40
+ selection: innerSelection,
41
+ parentType: parentType || spec.rootType
42
+ });
43
+ }
31
44
  if (innerSelection) {
32
45
  const { value: linkedRecord } = this.cache._internal_unstable.storage.get(
33
46
  parent,
@@ -52,11 +65,9 @@ class InMemorySubscriptions {
52
65
  addFieldSubscription({
53
66
  id,
54
67
  key,
55
- field,
56
- spec,
57
- parentType,
58
- variables
68
+ selection
59
69
  }) {
70
+ const spec = selection[0];
60
71
  if (!this.subscribers[id]) {
61
72
  this.subscribers[id] = {};
62
73
  }
@@ -67,8 +78,8 @@ class InMemorySubscriptions {
67
78
  this.keyVersions[key] = /* @__PURE__ */ new Set();
68
79
  }
69
80
  this.keyVersions[key].add(key);
70
- if (!this.subscribers[id][key].map(({ set }) => set).includes(spec.set)) {
71
- this.subscribers[id][key].push(spec);
81
+ if (!this.subscribers[id][key].map(([{ set }]) => set).includes(spec.set)) {
82
+ this.subscribers[id][key].push([spec, selection[1]]);
72
83
  }
73
84
  if (!this.referenceCounts[id]) {
74
85
  this.referenceCounts[id] = {};
@@ -79,61 +90,87 @@ class InMemorySubscriptions {
79
90
  const counts = this.referenceCounts[id][key];
80
91
  counts.set(spec.set, (counts.get(spec.set) || 0) + 1);
81
92
  this.cache._internal_unstable.lifetimes.resetLifetime(id, key);
82
- const { selection, list, filters } = field;
83
- if (selection && list) {
84
- this.cache._internal_unstable.lists.add({
85
- name: list.name,
86
- connection: list.connection,
87
- recordID: id,
88
- recordType: this.cache._internal_unstable.storage.get(id, "__typename")?.value || parentType,
89
- listType: list.type,
90
- key,
91
- selection,
92
- filters: Object.entries(filters || {}).reduce((acc, [key2, { kind, value }]) => {
93
- return {
94
- ...acc,
95
- [key2]: kind !== "Variable" ? value : variables[value]
96
- };
97
- }, {})
98
- });
99
- }
93
+ const { selection: innerSelection } = selection[1]?.[key] ?? {};
94
+ }
95
+ registerList({
96
+ list,
97
+ id,
98
+ key,
99
+ parentType,
100
+ selection,
101
+ filters,
102
+ variables
103
+ }) {
104
+ this.cache._internal_unstable.lists.add({
105
+ name: list.name,
106
+ connection: list.connection,
107
+ recordID: id,
108
+ recordType: this.cache._internal_unstable.storage.get(id, "__typename")?.value || parentType,
109
+ listType: list.type,
110
+ key,
111
+ selection,
112
+ filters: Object.entries(filters || {}).reduce((acc, [key2, { kind, value }]) => {
113
+ return {
114
+ ...acc,
115
+ [key2]: kind !== "Variable" ? value : variables[value]
116
+ };
117
+ }, {})
118
+ });
100
119
  }
101
120
  addMany({
102
121
  parent,
103
- selection,
104
122
  variables,
105
123
  subscribers,
106
124
  parentType
107
125
  }) {
108
- let targetSelection = getFieldsForType(selection, parentType);
109
- for (const fieldSelection of Object.values(targetSelection)) {
110
- const { type: linkedType, keyRaw, selection: innerSelection } = fieldSelection;
111
- const key = evaluateKey(keyRaw, variables);
112
- for (const spec of subscribers) {
126
+ for (const [spec, targetSelection] of subscribers) {
127
+ for (const selection of Object.values(targetSelection ?? {})) {
128
+ const {
129
+ type: linkedType,
130
+ keyRaw,
131
+ selection: innerSelection,
132
+ list,
133
+ filters
134
+ } = selection;
135
+ const key = evaluateKey(keyRaw, variables);
136
+ const fieldSelection = innerSelection ? getFieldsForType(innerSelection, parentType) : void 0;
113
137
  this.addFieldSubscription({
114
138
  id: parent,
115
139
  key,
116
- field: fieldSelection,
117
- spec,
118
- parentType,
119
- variables
140
+ selection: [spec, fieldSelection]
120
141
  });
121
- }
122
- if (innerSelection) {
123
- const { value: link } = this.cache._internal_unstable.storage.get(parent, key);
124
- const children = !Array.isArray(link) ? [link] : flattenList(link);
125
- for (const linkedRecord of children) {
126
- if (!linkedRecord) {
127
- continue;
128
- }
129
- this.addMany({
130
- parent: linkedRecord,
131
- selection: innerSelection,
142
+ if (list) {
143
+ this.registerList({
144
+ list,
145
+ filters,
146
+ id: parent,
147
+ key,
132
148
  variables,
133
- subscribers,
134
- parentType: linkedType
149
+ selection: innerSelection,
150
+ parentType: parentType || spec.rootType
135
151
  });
136
152
  }
153
+ const childSelection = selection.selection;
154
+ if (childSelection) {
155
+ const { value: link } = this.cache._internal_unstable.storage.get(parent, key);
156
+ const children = !Array.isArray(link) ? [link] : flattenList(link);
157
+ for (const linkedRecord of children) {
158
+ if (!linkedRecord) {
159
+ continue;
160
+ }
161
+ const __typename = this.cache._internal_unstable.storage.get(
162
+ linkedRecord,
163
+ "__typename"
164
+ ).value;
165
+ let targetSelection2 = getFieldsForType(childSelection, __typename);
166
+ this.addMany({
167
+ parent: linkedRecord,
168
+ variables,
169
+ subscribers: subscribers.map(([sub]) => [sub, targetSelection2]),
170
+ parentType: linkedType
171
+ });
172
+ }
173
+ }
137
174
  }
138
175
  }
139
176
  }
@@ -143,10 +180,12 @@ class InMemorySubscriptions {
143
180
  remove(id, selection, targets, variables, visited = []) {
144
181
  visited.push(id);
145
182
  const linkedIDs = [];
146
- for (const fieldSelection of Object.values(selection.fields || {})) {
183
+ const __typename = this.cache._internal_unstable.storage.get(id, "__typename").value;
184
+ let targetSelection = getFieldsForType(selection, __typename);
185
+ for (const fieldSelection of Object.values(targetSelection || {})) {
147
186
  const key = evaluateKey(fieldSelection.keyRaw, variables);
148
187
  this.removeSubscribers(id, key, targets);
149
- if (!fieldSelection.selection?.fields) {
188
+ if (!fieldSelection.selection) {
150
189
  continue;
151
190
  }
152
191
  const { value: previousValue } = this.cache._internal_unstable.storage.get(id, key);
@@ -177,14 +216,14 @@ class InMemorySubscriptions {
177
216
  }
178
217
  if (this.subscribers[id]) {
179
218
  this.subscribers[id][fieldName] = this.get(id, fieldName).filter(
180
- ({ set }) => !targets.includes(set)
219
+ ([{ set }]) => !targets.includes(set)
181
220
  );
182
221
  }
183
222
  }
184
223
  removeAllSubscribers(id, targets, visited = []) {
185
224
  visited.push(id);
186
225
  for (const field of Object.keys(this.subscribers[id] || [])) {
187
- const subscribers = targets || this.subscribers[id][field];
226
+ const subscribers = targets || this.subscribers[id][field].map(([spec]) => spec);
188
227
  this.removeSubscribers(id, field, subscribers);
189
228
  const { value, kind } = this.cache._internal_unstable.storage.get(id, field);
190
229
  if (kind === "scalar") {
@@ -0,0 +1,87 @@
1
+ import type { HoudiniClient } from '.';
2
+ import type { Layer } from '../cache/storage';
3
+ import type { ConfigFile } from '../lib/config';
4
+ import { Writable } from '../lib/store';
5
+ import type { CachePolicy, DocumentArtifact, QueryResult, GraphQLObject, SubscriptionSpec } from '../lib/types';
6
+ export declare class DocumentStore<_Data extends GraphQLObject, _Input extends Record<string, any>> extends Writable<QueryResult<_Data, _Input>> {
7
+ #private;
8
+ constructor({ artifact, plugins, pipeline, client, cache, initialValue, fetching, }: {
9
+ artifact: DocumentArtifact;
10
+ plugins?: ClientPlugin[];
11
+ pipeline?: ClientPlugin[];
12
+ client: HoudiniClient;
13
+ cache?: boolean;
14
+ initialValue?: _Data | null;
15
+ fetching?: boolean;
16
+ });
17
+ send({ metadata, session, fetch, variables, policy, stuff, cacheParams, setup, }?: {
18
+ fetch?: Fetch;
19
+ variables?: Record<string, any> | null;
20
+ metadata?: App.Metadata | null;
21
+ session?: App.Session | null;
22
+ policy?: CachePolicy;
23
+ stuff?: Partial<App.Stuff>;
24
+ cacheParams?: ClientPluginContext['cacheParams'];
25
+ setup?: boolean;
26
+ }): Promise<QueryResult<_Data, _Input>>;
27
+ }
28
+ declare function marshalVariables<_Data extends GraphQLObject, _Input extends {}>(ctx: ClientPluginContext): Record<string, any>;
29
+ export type ClientPlugin = () => {
30
+ start?: ClientPluginEnterPhase;
31
+ beforeNetwork?: ClientPluginEnterPhase;
32
+ network?: ClientPluginEnterPhase;
33
+ afterNetwork?: ClientPluginExitPhase;
34
+ end?: ClientPluginExitPhase;
35
+ cleanup?(ctx: ClientPluginContext): void | Promise<void>;
36
+ catch?(ctx: ClientPluginContext, args: ClientPluginErrorHandlers): void | Promise<void>;
37
+ };
38
+ export type Fetch = typeof globalThis.fetch;
39
+ export type ClientPluginContext = {
40
+ config: ConfigFile;
41
+ text: string;
42
+ hash: string;
43
+ artifact: DocumentArtifact;
44
+ policy?: CachePolicy;
45
+ fetch?: Fetch;
46
+ variables?: Record<string, any>;
47
+ metadata?: App.Metadata | null;
48
+ session?: App.Session | null;
49
+ fetchParams?: RequestInit;
50
+ cacheParams?: {
51
+ layer?: Layer;
52
+ notifySubscribers?: SubscriptionSpec[];
53
+ forceNotify?: boolean;
54
+ disableWrite?: boolean;
55
+ disableRead?: boolean;
56
+ applyUpdates?: boolean;
57
+ };
58
+ stuff: App.Stuff;
59
+ };
60
+ type ClientPluginPhase<Handlers> = (ctx: ClientPluginContext, handlers: Handlers) => void | Promise<void>;
61
+ export type ClientPluginEnterPhase = ClientPluginPhase<ClientPluginEnterHandlers>;
62
+ export type ClientPluginExitPhase = ClientPluginPhase<ClientPluginExitHandlers>;
63
+ export type ClientPluginEnterHandlers = {
64
+ initialValue: QueryResult;
65
+ /** A reference to the houdini client to access any configuration values */
66
+ client: HoudiniClient;
67
+ /** Move onto the next step using the provided context. */
68
+ next(ctx: ClientPluginContext): void;
69
+ /** Terminate the current chain */
70
+ resolve(ctx: ClientPluginContext, data: QueryResult): void;
71
+ /** Update the stores state without resolving the promise */
72
+ updateState(updater: (old: QueryResult) => QueryResult): void;
73
+ /** Return true if the variables have changed */
74
+ variablesChanged: (ctx: ClientPluginContext) => boolean;
75
+ /** Returns the marshaled variables for the operation */
76
+ marshalVariables: typeof marshalVariables;
77
+ };
78
+ /** Exit handlers are the same as enter handles but don't need to resolve with a specific value */
79
+ export type ClientPluginExitHandlers = Omit<ClientPluginEnterHandlers, 'resolve'> & {
80
+ resolve: (ctx: ClientPluginContext, data?: QueryResult) => void;
81
+ value: QueryResult;
82
+ };
83
+ /** Exit handlers are the same as enter handles but don't need to resolve with a specific value */
84
+ export type ClientPluginErrorHandlers = ClientPluginEnterHandlers & {
85
+ error: unknown;
86
+ };
87
+ export {};
@@ -0,0 +1,336 @@
1
+ import { getCurrentConfig } from "../lib/config";
2
+ import { deepEquals } from "../lib/deepEquals";
3
+ import { marshalInputs, unmarshalSelection } from "../lib/scalars";
4
+ import { Writable } from "../lib/store";
5
+ import { ArtifactKind } from "../lib/types";
6
+ import { cachePolicyPlugin } from "./plugins";
7
+ const steps = {
8
+ forward: ["start", "beforeNetwork", "network"],
9
+ backwards: ["end", "afterNetwork"]
10
+ };
11
+ class DocumentStore extends Writable {
12
+ #artifact;
13
+ #client;
14
+ #configFile;
15
+ #plugins;
16
+ #lastVariables;
17
+ #lastContext = null;
18
+ constructor({
19
+ artifact,
20
+ plugins,
21
+ pipeline,
22
+ client,
23
+ cache = true,
24
+ initialValue,
25
+ fetching
26
+ }) {
27
+ if (fetching === void 0) {
28
+ fetching = artifact.kind === ArtifactKind.Query;
29
+ }
30
+ const initialState = {
31
+ data: initialValue ?? null,
32
+ errors: null,
33
+ partial: false,
34
+ source: null,
35
+ fetching,
36
+ variables: null
37
+ };
38
+ super(initialState, () => {
39
+ return () => {
40
+ this.#lastVariables = null;
41
+ for (const plugin of this.#plugins) {
42
+ plugin.cleanup?.(this.#lastContext);
43
+ }
44
+ };
45
+ });
46
+ this.#artifact = artifact;
47
+ this.#client = client;
48
+ this.#lastVariables = null;
49
+ this.#configFile = getCurrentConfig();
50
+ this.#plugins = (pipeline ?? [
51
+ cachePolicyPlugin({
52
+ enabled: cache,
53
+ setFetching: (fetching2) => this.update((state) => ({ ...state, fetching: fetching2 }))
54
+ }),
55
+ ...plugins ?? []
56
+ ]).map((factory) => factory());
57
+ }
58
+ async send({
59
+ metadata,
60
+ session,
61
+ fetch = globalThis.fetch,
62
+ variables,
63
+ policy,
64
+ stuff,
65
+ cacheParams,
66
+ setup = false
67
+ } = {}) {
68
+ let context = new ClientPluginContextWrapper({
69
+ config: this.#configFile,
70
+ text: this.#artifact.raw,
71
+ hash: this.#artifact.hash,
72
+ policy: policy ?? this.#artifact.policy,
73
+ variables: {},
74
+ metadata,
75
+ session,
76
+ fetch,
77
+ stuff: {
78
+ inputs: {
79
+ changed: false,
80
+ init: false,
81
+ marshaled: {}
82
+ },
83
+ ...stuff
84
+ },
85
+ artifact: this.#artifact,
86
+ lastVariables: this.#lastVariables,
87
+ cacheParams
88
+ });
89
+ const draft = context.draft();
90
+ draft.variables = variables ?? {};
91
+ context = context.apply(draft, false);
92
+ return await new Promise((resolve, reject) => {
93
+ const state = {
94
+ setup,
95
+ currentStep: 0,
96
+ index: 0,
97
+ promise: {
98
+ resolved: false,
99
+ resolve,
100
+ reject
101
+ },
102
+ context
103
+ };
104
+ this.#step("forward", state);
105
+ });
106
+ }
107
+ #step(direction, ctx, value) {
108
+ const hook = direction === "error" ? "catch" : steps[direction][ctx.currentStep];
109
+ let valid = (i) => i <= this.#plugins.length;
110
+ let step = (i) => i + 1;
111
+ if (["backwards", "error"].includes(direction)) {
112
+ valid = (i) => i >= 0;
113
+ step = (i) => i - 1;
114
+ }
115
+ for (let index = ctx.index; valid(index); index = step(index)) {
116
+ let target = this.#plugins[index]?.[hook];
117
+ if (!target) {
118
+ continue;
119
+ }
120
+ const draft = ctx.context.draft();
121
+ let variablesRefChanged = (newContext) => newContext.variables !== draft.variables;
122
+ const common = {
123
+ initialValue: this.state,
124
+ client: this.#client,
125
+ variablesChanged,
126
+ marshalVariables,
127
+ updateState: this.update.bind(this),
128
+ next: (newContext) => {
129
+ const nextIndex = ["forward", "error"].includes(direction) ? index + 1 : index;
130
+ const nextStep = ["backwards", "error"].includes(direction) ? 0 : ctx.currentStep;
131
+ this.#step("forward", {
132
+ ...ctx,
133
+ index: nextIndex,
134
+ currentStep: nextStep,
135
+ context: ctx.context.apply(newContext, variablesRefChanged(newContext))
136
+ });
137
+ },
138
+ resolve: (newContext, value2) => {
139
+ const nextIndex = direction === "backwards" ? index - 1 : index;
140
+ this.#step(
141
+ "backwards",
142
+ {
143
+ ...ctx,
144
+ index: nextIndex,
145
+ context: ctx.context.apply(newContext, variablesRefChanged(newContext))
146
+ },
147
+ value2
148
+ );
149
+ }
150
+ };
151
+ let handlers;
152
+ if (direction === "forward") {
153
+ handlers = common;
154
+ } else if (direction === "backwards") {
155
+ handlers = {
156
+ ...common,
157
+ value,
158
+ resolve: (ctx2, data2) => {
159
+ return common.resolve(ctx2, data2 ?? value);
160
+ }
161
+ };
162
+ } else if (direction === "error") {
163
+ handlers = {
164
+ ...common,
165
+ error: value
166
+ };
167
+ }
168
+ try {
169
+ const result = target(draft, handlers);
170
+ result?.catch((err) => {
171
+ this.#step("error", { ...ctx, index: index - 1 }, err);
172
+ });
173
+ } catch (err) {
174
+ this.#step("error", { ...ctx, index: index - 1 }, err);
175
+ }
176
+ return;
177
+ }
178
+ if (direction === "forward") {
179
+ if (ctx.setup) {
180
+ return this.#step(
181
+ "backwards",
182
+ {
183
+ ...ctx,
184
+ currentStep: 0,
185
+ index: this.#plugins.length
186
+ },
187
+ this.state
188
+ );
189
+ }
190
+ if (ctx.currentStep <= steps.forward.length - 2) {
191
+ return this.#step("forward", {
192
+ ...ctx,
193
+ currentStep: ctx.currentStep + 1,
194
+ index: 0
195
+ });
196
+ }
197
+ throw new Error(
198
+ "Called next() on last possible plugin. Your chain is missing a plugin that calls resolve()."
199
+ );
200
+ }
201
+ if (direction === "error") {
202
+ if (!ctx.promise.resolved) {
203
+ ctx.promise.reject(value);
204
+ ctx.promise.resolved = true;
205
+ }
206
+ return;
207
+ }
208
+ if (ctx.currentStep > 0) {
209
+ return this.#step(
210
+ "backwards",
211
+ {
212
+ ...ctx,
213
+ currentStep: ctx.currentStep - 1,
214
+ index: this.#plugins.length - 1
215
+ },
216
+ value
217
+ );
218
+ }
219
+ let data = value.data;
220
+ try {
221
+ data = unmarshalSelection(this.#configFile, this.#artifact.selection, data) ?? null;
222
+ } catch {
223
+ }
224
+ const finalValue = {
225
+ ...value,
226
+ data
227
+ };
228
+ if (!ctx.promise.resolved) {
229
+ ctx.promise.resolve(finalValue);
230
+ ctx.promise.resolved = true;
231
+ }
232
+ this.#lastContext = ctx.context.draft();
233
+ this.#lastVariables = this.#lastContext.stuff.inputs.marshaled;
234
+ this.set(finalValue);
235
+ }
236
+ }
237
+ class ClientPluginContextWrapper {
238
+ #context;
239
+ #lastVariables;
240
+ constructor({
241
+ lastVariables,
242
+ ...values
243
+ }) {
244
+ this.#context = values;
245
+ this.#lastVariables = lastVariables;
246
+ }
247
+ get variables() {
248
+ return this.#context.variables;
249
+ }
250
+ draft() {
251
+ const ctx = {
252
+ ...this.#context
253
+ };
254
+ const applyVariables = this.applyVariables.bind(this);
255
+ return {
256
+ ...ctx,
257
+ get stuff() {
258
+ return ctx.stuff;
259
+ },
260
+ set stuff(val) {
261
+ ctx.stuff = val;
262
+ },
263
+ get variables() {
264
+ return ctx.variables ?? {};
265
+ },
266
+ set variables(val) {
267
+ Object.assign(ctx, applyVariables(ctx, { variables: val }));
268
+ }
269
+ };
270
+ }
271
+ applyVariables(source, values) {
272
+ const artifact = source.artifact;
273
+ const ctx = {
274
+ ...source,
275
+ ...values
276
+ };
277
+ const val = values.variables;
278
+ let changed = {};
279
+ for (const [name, value] of Object.entries(val ?? {})) {
280
+ if (value !== source.variables?.[name]) {
281
+ changed[name] = value;
282
+ }
283
+ }
284
+ ctx.stuff = {
285
+ ...ctx.stuff,
286
+ inputs: {
287
+ ...ctx.stuff.inputs
288
+ }
289
+ };
290
+ const firstInit = !ctx.stuff.inputs || !ctx.stuff.inputs.init;
291
+ const hasChanged = Object.keys(changed).length > 0 || firstInit;
292
+ if (artifact.kind !== ArtifactKind.Fragment && hasChanged) {
293
+ const newVariables = {
294
+ ...ctx.stuff.inputs?.marshaled,
295
+ ...marshalInputs({
296
+ artifact,
297
+ input: changed,
298
+ config: source.config
299
+ })
300
+ };
301
+ ctx.stuff.inputs = {
302
+ init: true,
303
+ marshaled: newVariables,
304
+ changed: true
305
+ };
306
+ ctx.variables = val;
307
+ }
308
+ ctx.stuff = {
309
+ ...ctx.stuff,
310
+ inputs: {
311
+ ...ctx.stuff.inputs,
312
+ changed: !deepEquals(ctx.stuff.inputs.marshaled, this.#lastVariables)
313
+ }
314
+ };
315
+ return ctx;
316
+ }
317
+ apply(values, newVariables) {
318
+ if (newVariables) {
319
+ values = this.applyVariables(this.#context, values);
320
+ }
321
+ const wrapper = new ClientPluginContextWrapper({
322
+ ...values,
323
+ lastVariables: this.#lastVariables
324
+ });
325
+ return wrapper;
326
+ }
327
+ }
328
+ function marshalVariables(ctx) {
329
+ return ctx.stuff.inputs?.marshaled ?? {};
330
+ }
331
+ function variablesChanged(ctx) {
332
+ return ctx.stuff.inputs?.changed;
333
+ }
334
+ export {
335
+ DocumentStore
336
+ };
@@ -0,0 +1,25 @@
1
+ /// <reference path="../../../../../houdini.d.ts" />
2
+ import type { DocumentArtifact, GraphQLObject } from '../lib/types';
3
+ import type { ClientPlugin } from './documentStore';
4
+ import { DocumentStore } from './documentStore';
5
+ import { type FetchParamFn, type ThrowOnErrorParams } from './plugins';
6
+ export { DocumentStore, type ClientPlugin } from './documentStore';
7
+ export { fetchPlugin, mutationPlugin, queryPlugin, subscriptionPlugin } from './plugins';
8
+ type ConstructorArgs = {
9
+ url: string;
10
+ fetchParams?: FetchParamFn;
11
+ plugins?: ClientPlugin[];
12
+ pipeline?: ClientPlugin[];
13
+ throwOnError?: ThrowOnErrorParams;
14
+ };
15
+ export declare class HoudiniClient {
16
+ #private;
17
+ url: string;
18
+ constructor({ url, fetchParams, plugins, pipeline, throwOnError }: ConstructorArgs);
19
+ observe<_Data extends GraphQLObject, _Input extends Record<string, any>>({ artifact, cache, initialValue, fetching, }: {
20
+ artifact: DocumentArtifact;
21
+ cache?: boolean;
22
+ initialValue?: _Data | null;
23
+ fetching?: boolean;
24
+ }): DocumentStore<_Data, _Input>;
25
+ }