@@ -32,19 +32,22 @@ export class Cache {
3232 }
3333
3434 // Main functionality methods
35- async read ( queryStr ) {
35+ async read ( queryStr , queryVars ) {
3636 if ( typeof queryStr !== 'string' )
3737 throw TypeError ( 'input should be a string' ) ;
3838 // destructure the query string into an object
39- const queries = destructureQueries ( queryStr ) . queries ;
39+ const queries = destructureQueries ( queryStr , queryVars ) . queries ;
40+
4041 // breaks out of function if queryStr is a mutation
4142 if ( ! queries ) return undefined ;
43+
4244 const responseObject = { } ;
4345 // iterate through each query in the input queries object
4446 for ( const query in queries ) {
4547 // get the entire str query from the name input query and arguments
4648 const queryHash = queries [ query ] . name . concat ( queries [ query ] . arguments ) ;
4749 const rootQuery = await this . cacheRead ( 'ROOT_QUERY' ) ;
50+
4851 // match in ROOT_QUERY
4952 if ( rootQuery [ queryHash ] ) {
5053 // get the hashs to populate from the existent query in the cache
@@ -56,6 +59,7 @@ export class Cache {
5659 arrayHashes ,
5760 queries [ query ] . fields
5861 ) ;
62+
5963 if ( ! responseObject [ respObjProp ] ) return undefined ;
6064
6165 // no match with ROOT_QUERY return null or ...
@@ -66,8 +70,8 @@ export class Cache {
6670 return { data : responseObject } ;
6771 }
6872
69- async write ( queryStr , respObj , deleteFlag ) {
70- const queryObj = destructureQueries ( queryStr ) ;
73+ async write ( queryStr , respObj , deleteFlag , queryVars ) {
74+ const queryObj = destructureQueries ( queryStr , queryVars ) ;
7175 const resFromNormalize = normalizeResult ( queryObj , respObj , deleteFlag ) ;
7276 // update the original cache with same reference
7377 for ( const hash in resFromNormalize ) {
@@ -85,22 +89,26 @@ export class Cache {
8589
8690 // cache read/write helper methods
8791 async cacheRead ( hash ) {
88- // returns value from either object cache or cache || 'DELETED' || undefined
92+ // returns value from either object cache or cache || 'DELETED' || undefined
8993 if ( this . context === 'client' ) {
94+ console . log ( 'context === client HIT' ) ;
9095 return this . storage [ hash ] ;
9196 } else {
9297 // logic to replace these storage keys if they have expired
9398 if ( hash === 'ROOT_QUERY' || hash === 'ROOT_MUTATION' ) {
9499 const hasRootQuery = await redis . get ( 'ROOT_QUERY' ) ;
100+
95101 if ( ! hasRootQuery ) {
96102 await redis . set ( 'ROOT_QUERY' , JSON . stringify ( { } ) ) ;
97103 }
98104 const hasRootMutation = await redis . get ( 'ROOT_MUTATION' ) ;
105+
99106 if ( ! hasRootMutation ) {
100107 await redis . set ( 'ROOT_MUTATION' , JSON . stringify ( { } ) ) ;
101108 }
102109 }
103110 let hashedQuery = await redis . get ( hash ) ;
111+
104112 // if cacheRead is a miss
105113 if ( hashedQuery === undefined ) return undefined ;
106114 return JSON . parse ( hashedQuery ) ;
@@ -158,8 +166,10 @@ export class Cache {
158166 async populateAllHashes ( allHashesFromQuery , fields ) {
159167 // include the hashname for each hash
160168 if ( ! allHashesFromQuery . length ) return [ ] ;
169+
161170 const hyphenIdx = allHashesFromQuery [ 0 ] . indexOf ( '~' ) ;
162171 const typeName = allHashesFromQuery [ 0 ] . slice ( 0 , hyphenIdx ) ;
172+
163173 return allHashesFromQuery . reduce ( async ( acc , hash ) => {
164174 // for each hash from the input query, build the response object
165175 const readVal = await this . cacheRead ( hash ) ;
@@ -176,11 +186,13 @@ export class Cache {
176186 // add the typename for the type
177187 if ( field === '__typename' ) {
178188 dataObj [ field ] = typeName ;
179- } else dataObj [ field ] = readVal [ field ] ;
189+ } else {
190+ dataObj [ field ] = readVal [ field ] ;
191+ }
180192 } else {
181193 // case where the field from the input query is an array of hashes, recursively invoke populateAllHashes
182194 dataObj [ field ] = await this . populateAllHashes (
183- readVal [ field ] ,
195+ [ readVal [ field ] ] ,
184196 fields [ field ]
185197 ) ;
186198 if ( dataObj [ field ] === undefined ) return undefined ;
0 commit comments