1515 * limitations under the License.
1616 */
1717
18- import { DataConnect } from '../api' ;
18+ import { DataConnect , ExecuteQueryOptions , QueryFetchPolicy } from '../api' ;
1919import {
2020 DataConnectSubscription ,
2121 OnErrorSubscription ,
@@ -162,22 +162,22 @@ export class QueryManager {
162162 return unsubscribe ;
163163 }
164164 executeQuery < Data , Variables > (
165- queryRef : QueryRef < Data , Variables >
165+ queryRef : QueryRef < Data , Variables > ,
166+ options ?: ExecuteQueryOptions
166167 ) : QueryPromise < Data , Variables > {
167168 if ( queryRef . refType !== QUERY_STR ) {
168169 throw new DataConnectError (
169170 Code . INVALID_ARGUMENT ,
170- `ExecuteQuery can only execute query operation `
171+ `ExecuteQuery can only execute query operations `
171172 ) ;
172173 }
173174 const key = encoderImpl ( {
174175 name : queryRef . name ,
175176 variables : queryRef . variables ,
176177 refType : QUERY_STR
177178 } ) ;
178- // TODO: Check if the cache is stale
179- // TODO: isStale doesn't exist when parsing the raw JSON. Fix the parsing.
180- if ( this . cache . containsResultTree ( key ) && ! this . cache . getResultTree ( key ) ?. isStale ( ) ) {
179+
180+ if ( options . fetchPolicy !== QueryFetchPolicy . serverOnly && this . cache . containsResultTree ( key ) && ! this . cache . getResultTree ( key ) . isStale ( ) ) {
181181 const cacheResult : Data = JSON . parse ( this . cache . getResultJSON ( key ) ) ;
182182 const result : QueryResult < Data , Variables > = {
183183 ...cacheResult ,
@@ -191,11 +191,15 @@ export class QueryManager {
191191
192192 return Promise . resolve ( result ) ;
193193 } else {
194- logDebug (
195- `No Cache found for query ${
196- queryRef . name
197- } with variables ${ JSON . stringify ( queryRef . variables ) } . Calling executeQuery`
198- ) ;
194+ if ( options . fetchPolicy === QueryFetchPolicy . serverOnly ) {
195+ logDebug ( `Skipping cache for fetch policy "serverOnly"` ) ;
196+ } else {
197+ logDebug (
198+ `No Cache found for query ${
199+ queryRef . name
200+ } with variables ${ JSON . stringify ( queryRef . variables ) } . Calling executeQuery`
201+ ) ;
202+ }
199203 }
200204 const result = this . transport . invokeQuery < Data , Variables > (
201205 queryRef . name ,
@@ -219,8 +223,7 @@ export class QueryManager {
219223 }
220224 if ( this . cache . containsResultTree ( key ) ) {
221225 this . cache . getResultTree ( key ) . updateAccessed ( ) ;
222- }
223- const impactedQueries = this . cache . update ( key , result . data as ServerValues ) ;
226+ } const impactedQueries = this . cache . update ( key , result . data as ServerValues ) ;
224227 this . publishCacheResultsToSubscribers ( impactedQueries ) ;
225228 return result ;
226229 } ,
0 commit comments