@@ -31,28 +31,10 @@ export interface QueryResultData {
3131}
3232
3333/**
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
3935 */
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 ;
5638}
5739
5840/**
@@ -143,14 +125,36 @@ export class Cache {
143125 * A map of ([query + variables] --> stubs returned from that query).
144126 * @public
145127 */
146- resultTreeCache = new Map < string , StubDataObject [ ] > ( ) ;
128+ resultTreeCache = new Map < string , StubResultTree > ( ) ;
147129
148130 /**
149131 * A map of ([entity typename + id] --> BackingDataObject for that entity).
150132 * @public
151133 */
152134 bdoCache = new Map < string , BackingDataObject > ( ) ;
153135
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+
154158 /**
155159 * Updates the cache with the results of a query.
156160 * @param queryResult The result of the query.
@@ -159,34 +163,34 @@ export class Cache {
159163 updateCache < Data extends QueryResultData | QueryResultData [ ] , Variables > (
160164 queryResult : QueryResult < Data , Variables >
161165 ) : 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 = { } ;
163171 // key = "movies" or "actor", etc.
164172 // eslint-disable-next-line guard-for-in
165173 for ( const key in queryResult . data ) {
166174 const queryData = queryResult . data [ key ] ;
167175 if ( Array . isArray ( queryData ) ) {
168176 queryData . forEach ( qd => {
169177 const sdo : StubDataObject = {
170- ...Object . entries ( qd )
178+ ...qd
171179 // todo: add in non-cacheable fields
172180 } ;
173- stubDataObjects . push ( sdo ) ;
181+ stubResultTree [ key ] = sdo ;
174182 const bdo : BackingDataObject = this . updateBdoCache ( qd , sdo ) ;
175183 } ) ;
176184 } else {
177185 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...
179187 // todo: add in non-cacheable fields
180188 } ;
181- stubDataObjects . push ( sdo ) ;
189+ stubResultTree [ key ] = sdo ;
182190 const bdo = this . updateBdoCache ( queryData as QueryResultData , sdo ) ; // ! i don't think i should need a type assertion here, yet TS complains without it...
183191 }
184192 }
185- this . updateResultTreeCache (
186- queryResult . ref . name ,
187- queryResult . ref . variables ,
188- stubDataObjects
189- ) ;
193+ this . resultTreeCache . set ( resultTreeCacheKey , stubResultTree ) ;
190194 }
191195
192196 /**
@@ -198,7 +202,7 @@ export class Cache {
198202 data : Data ,
199203 stubDataObject : StubDataObject
200204 ) : BackingDataObject {
201- const bdoCacheKey = makeBdoCacheKey ( data [ '__typename' ] , data [ '__id' ] ) ;
205+ const bdoCacheKey = Cache . makeBdoCacheKey ( data [ '__typename' ] , data [ '__id' ] ) ;
202206 let backingDataObject = this . bdoCache . get ( bdoCacheKey ) ;
203207
204208 if ( backingDataObject ) {
@@ -220,18 +224,4 @@ export class Cache {
220224 }
221225 return backingDataObject ;
222226 }
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- }
237227}
0 commit comments