@@ -31,28 +31,10 @@ export interface QueryResultData {
31
31
}
32
32
33
33
/**
34
- * Creates a unique StrubResultTree cache key for a given query and its variables.
35
- * @param queryName The name of the query.
36
- * @param vars The variables used in the query.
37
- * @returns A unique cache key string.
38
- * @public
34
+ * Interface for a stub result tree, with fields which are stub data objects
39
35
*/
40
- export function makeResultTreeCacheKey (
41
- queryName : string ,
42
- vars : unknown
43
- ) : string {
44
- return queryName + '|' + JSON . stringify ( vars ) ;
45
- }
46
-
47
- /**
48
- * Creates a unique BackingDataObject cache key for a given entity.
49
- * @param typename The typename of the entity being cached.
50
- * @param id The unique id / primary key of this entity.
51
- * @returns A unique cache key string.
52
- * @public
53
- */
54
- export function makeBdoCacheKey ( typename : string , id : unknown ) : string {
55
- return typename + '|' + JSON . stringify ( id ) ;
36
+ interface StubResultTree {
37
+ [ key : string ] : StubDataObject | StubDataObjectList ;
56
38
}
57
39
58
40
/**
@@ -143,14 +125,36 @@ export class Cache {
143
125
* A map of ([query + variables] --> stubs returned from that query).
144
126
* @public
145
127
*/
146
- resultTreeCache = new Map < string , StubDataObject [ ] > ( ) ;
128
+ resultTreeCache = new Map < string , StubResultTree > ( ) ;
147
129
148
130
/**
149
131
* A map of ([entity typename + id] --> BackingDataObject for that entity).
150
132
* @public
151
133
*/
152
134
bdoCache = new Map < string , BackingDataObject > ( ) ;
153
135
136
+ /**
137
+ * Creates a unique StrubResultTree cache key for a given query and its variables.
138
+ * @param queryName The name of the query.
139
+ * @param vars The variables used in the query.
140
+ * @returns A unique cache key string.
141
+ * @public
142
+ */
143
+ static makeResultTreeCacheKey ( queryName : string , vars : unknown ) : string {
144
+ return queryName + '|' + JSON . stringify ( vars ) ;
145
+ }
146
+
147
+ /**
148
+ * Creates a unique BackingDataObject cache key for a given entity.
149
+ * @param typename The typename of the entity being cached.
150
+ * @param id The unique id / primary key of this entity.
151
+ * @returns A unique cache key string.
152
+ * @public
153
+ */
154
+ static makeBdoCacheKey ( typename : string , id : unknown ) : string {
155
+ return typename + '|' + JSON . stringify ( id ) ;
156
+ }
157
+
154
158
/**
155
159
* Updates the cache with the results of a query.
156
160
* @param queryResult The result of the query.
@@ -159,34 +163,34 @@ export class Cache {
159
163
updateCache < Data extends QueryResultData | QueryResultData [ ] , Variables > (
160
164
queryResult : QueryResult < Data , Variables >
161
165
) : void {
162
- const stubDataObjects : StubDataObject [ ] = [ ] ; // ! if this is going to be mapped to the query name | variables, consider the fact that not all queries return a list of objects...
166
+ const resultTreeCacheKey = Cache . makeResultTreeCacheKey (
167
+ queryResult . ref . name ,
168
+ queryResult . ref . variables
169
+ ) ;
170
+ const stubResultTree : StubResultTree = { } ;
163
171
// key = "movies" or "actor", etc.
164
172
// eslint-disable-next-line guard-for-in
165
173
for ( const key in queryResult . data ) {
166
174
const queryData = queryResult . data [ key ] ;
167
175
if ( Array . isArray ( queryData ) ) {
168
176
queryData . forEach ( qd => {
169
177
const sdo : StubDataObject = {
170
- ...Object . entries ( qd )
178
+ ...qd
171
179
// todo: add in non-cacheable fields
172
180
} ;
173
- stubDataObjects . push ( sdo ) ;
181
+ stubResultTree [ key ] = sdo ;
174
182
const bdo : BackingDataObject = this . updateBdoCache ( qd , sdo ) ;
175
183
} ) ;
176
184
} else {
177
185
const sdo : StubDataObject = {
178
- ...Object . entries ( queryData )
186
+ ...( queryData as QueryResultData ) // ! i don't think i should need a type assertion here, yet TS complains without it...
179
187
// todo: add in non-cacheable fields
180
188
} ;
181
- stubDataObjects . push ( sdo ) ;
189
+ stubResultTree [ key ] = sdo ;
182
190
const bdo = this . updateBdoCache ( queryData as QueryResultData , sdo ) ; // ! i don't think i should need a type assertion here, yet TS complains without it...
183
191
}
184
192
}
185
- this . updateResultTreeCache (
186
- queryResult . ref . name ,
187
- queryResult . ref . variables ,
188
- stubDataObjects
189
- ) ;
193
+ this . resultTreeCache . set ( resultTreeCacheKey , stubResultTree ) ;
190
194
}
191
195
192
196
/**
@@ -198,7 +202,7 @@ export class Cache {
198
202
data : Data ,
199
203
stubDataObject : StubDataObject
200
204
) : BackingDataObject {
201
- const bdoCacheKey = makeBdoCacheKey ( data [ '__typename' ] , data [ '__id' ] ) ;
205
+ const bdoCacheKey = Cache . makeBdoCacheKey ( data [ '__typename' ] , data [ '__id' ] ) ;
202
206
let backingDataObject = this . bdoCache . get ( bdoCacheKey ) ;
203
207
204
208
if ( backingDataObject ) {
@@ -220,18 +224,4 @@ export class Cache {
220
224
}
221
225
return backingDataObject ;
222
226
}
223
-
224
- /**
225
- * Update the StubResultTree cache, either adding a new StubResultTree or updating an existing
226
- * StubResultTree
227
- * @param data A single entity from the database.
228
- */
229
- private updateResultTreeCache < Variables > (
230
- queryName : string ,
231
- variables : Variables ,
232
- stubDataObjects : StubDataObject [ ]
233
- ) : void {
234
- const resultTreeCacheKey = makeResultTreeCacheKey ( queryName , variables ) ;
235
- this . resultTreeCache . set ( resultTreeCacheKey , stubDataObjects ) ;
236
- }
237
227
}
0 commit comments