Skip to content

Commit 3c27dfb

Browse files
authored
[v2] Fix fetch default cache policies (#777)
1 parent d428907 commit 3c27dfb

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

Sources/Apollo/ApolloClient.swift

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public final class ApolloClient: Sendable {
126126
/// - Returns: A `GraphQLResponse` with the result for the query.
127127
public func fetch<Query: GraphQLQuery>(
128128
query: Query,
129-
cachePolicy: CachePolicy.Query.SingleResponse,
129+
cachePolicy: CachePolicy.Query.SingleResponse = .cacheFirst,
130130
requestConfiguration: RequestConfiguration? = nil
131131
) async throws -> GraphQLResponse<Query>
132132
where Query.ResponseFormat == SingleResponseFormat {
@@ -175,7 +175,7 @@ public final class ApolloClient: Sendable {
175175
/// - Returns: An ``AsyncThrowingStream`` of `GraphQLResponse` results for the query.
176176
public func fetch<Query: GraphQLQuery>(
177177
query: Query,
178-
cachePolicy: CachePolicy.Query.SingleResponse,
178+
cachePolicy: CachePolicy.Query.SingleResponse = .cacheFirst,
179179
requestConfiguration: RequestConfiguration? = nil
180180
) throws -> AsyncThrowingStream<GraphQLResponse<Query>, any Swift.Error>
181181
where Query.ResponseFormat == IncrementalDeferredResponseFormat {
@@ -261,7 +261,7 @@ public final class ApolloClient: Sendable {
261261
/// - Returns: An ``AsyncThrowingStream`` of `GraphQLResponse` results for the query.
262262
public func fetch<Query: GraphQLQuery>(
263263
query: Query,
264-
fetchBehavior: FetchBehavior = FetchBehavior.CacheFirst,
264+
fetchBehavior: FetchBehavior,
265265
requestConfiguration: RequestConfiguration? = nil
266266
) throws -> AsyncThrowingStream<GraphQLResponse<Query>, any Swift.Error> {
267267
return try doInClientContext {
@@ -275,13 +275,9 @@ public final class ApolloClient: Sendable {
275275

276276
// MARK: - Watch Query
277277

278-
// MARK: Watch Query w/Fetch Behavior
279-
280-
/// Watches a `GraphQLQuery` by first fetching an initial result using the provided ``FetchBehavior``. The
281-
/// `resultHandler` is called after the initial fetch and again each time the data for the watched query changes in
282-
/// the local cache of this client's ``ApolloClient/store``.
283-
///
284-
/// This function triggers a fetch on the ``GraphQLQueryWatcher`` prior to returning it.
278+
/// Watches a `GraphQLQuery` by first fetching an initial result from the network or local cache. The `resultHandler`
279+
/// is called after the initial fetch and again each time the data for the watched query changes in the local cache
280+
/// of this client's ``ApolloClient/store``.
285281
///
286282
/// The ``GraphQLQueryWatcher`` returned by this function is notified whenever any of the data the query result
287283
/// depends on changes in the local cache. The ``GraphQLQueryWatcher`` retains the provided `resultHandler` and will
@@ -290,8 +286,8 @@ public final class ApolloClient: Sendable {
290286
///
291287
/// - Parameters:
292288
/// - query: The `GraphQLQuery` to fetch.
293-
/// - fetchBehavior: The ``FetchBehavior`` to use for this request.
294-
/// Determines if fetching will include cache/network fetches.
289+
/// - cachePolicy: A ``CachePolicy/Query/SingleResponse`` ``CachePolicy`` to use for this request.
290+
/// Determines if the initial fetch will include cache/network fetches.
295291
/// - requestConfiguration: A ``RequestConfiguration`` to use for the watcher's initial fetch. Defaults to `nil`.
296292
/// If `nil` the receiver's ``ApolloClient/defaultRequestConfiguration`` will be used.
297293
/// - refetchOnFailedUpdates: Should the watcher perform a network fetch when it's watched objects have changed,
@@ -302,28 +298,23 @@ public final class ApolloClient: Sendable {
302298
/// to stop receiving new results.
303299
public func watch<Query: GraphQLQuery>(
304300
query: Query,
305-
fetchBehavior: FetchBehavior = FetchBehavior.CacheFirst,
301+
cachePolicy: CachePolicy.Query.SingleResponse = .cacheFirst,
306302
requestConfiguration: RequestConfiguration? = nil,
307303
refetchOnFailedUpdates: Bool = true,
308304
resultHandler: @escaping GraphQLQueryWatcher<Query>.ResultHandler
309305
) async -> GraphQLQueryWatcher<Query> {
310-
let watcher = await GraphQLQueryWatcher(
311-
client: self,
306+
return await self.watch(
312307
query: query,
308+
fetchBehavior: cachePolicy.toFetchBehavior(),
309+
requestConfiguration: requestConfiguration,
313310
refetchOnFailedUpdates: refetchOnFailedUpdates,
314311
resultHandler: resultHandler
315312
)
316-
Task {
317-
await watcher.fetch(fetchBehavior: fetchBehavior, requestConfiguration: requestConfiguration)
318-
}
319-
return watcher
320313
}
321314

322-
// MARK: Watch Query - CachePolicy Overloads
323-
324-
/// Watches a `GraphQLQuery` by first fetching an initial result from the network or local cache. The `resultHandler`
325-
/// is called after the initial fetch and again each time the data for the watched query changes in the local cache
326-
/// of this client's ``ApolloClient/store``.
315+
/// Watches a `GraphQLQuery` by first fetching an initial result from from the local cache and then from the network.
316+
/// The `resultHandler` is called after the initial fetch and again each time the data for the watched query changes
317+
/// in the local cache of this client's ``ApolloClient/store``.
327318
///
328319
/// The ``GraphQLQueryWatcher`` returned by this function is notified whenever any of the data the query result
329320
/// depends on changes in the local cache. The ``GraphQLQueryWatcher`` retains the provided `resultHandler` and will
@@ -332,8 +323,8 @@ public final class ApolloClient: Sendable {
332323
///
333324
/// - Parameters:
334325
/// - query: The `GraphQLQuery` to fetch.
335-
/// - cachePolicy: A ``CachePolicy/Query/SingleResponse`` ``CachePolicy`` to use for this request.
336-
/// Determines if the initial fetch will include cache/network fetches.
326+
/// - cachePolicy: A ``CachePolicy`` to use for this request. This function overload only accepts the
327+
/// ``CachePolicy/Query/CacheAndNetwork/cacheAndNetwork`` policy.
337328
/// - requestConfiguration: A ``RequestConfiguration`` to use for the watcher's initial fetch. Defaults to `nil`.
338329
/// If `nil` the receiver's ``ApolloClient/defaultRequestConfiguration`` will be used.
339330
/// - refetchOnFailedUpdates: Should the watcher perform a network fetch when it's watched objects have changed,
@@ -344,7 +335,7 @@ public final class ApolloClient: Sendable {
344335
/// to stop receiving new results.
345336
public func watch<Query: GraphQLQuery>(
346337
query: Query,
347-
cachePolicy: CachePolicy.Query.SingleResponse,
338+
cachePolicy: CachePolicy.Query.CacheAndNetwork,
348339
requestConfiguration: RequestConfiguration? = nil,
349340
refetchOnFailedUpdates: Bool = true,
350341
resultHandler: @escaping GraphQLQueryWatcher<Query>.ResultHandler
@@ -358,9 +349,9 @@ public final class ApolloClient: Sendable {
358349
)
359350
}
360351

361-
/// Watches a `GraphQLQuery` by first fetching an initial result from from the local cache and then from the network.
362-
/// The `resultHandler` is called after the initial fetch and again each time the data for the watched query changes
363-
/// in the local cache of this client's ``ApolloClient/store``.
352+
/// Watches a `GraphQLQuery` by first fetching an initial result from from the local cache. It does not attempt to
353+
/// fetch results from the server. The `resultHandler` is called after the initial fetch and again each time the data
354+
/// for the watched query changes in the local cache of this client's ``ApolloClient/store``.
364355
///
365356
/// The ``GraphQLQueryWatcher`` returned by this function is notified whenever any of the data the query result
366357
/// depends on changes in the local cache. The ``GraphQLQueryWatcher`` retains the provided `resultHandler` and will
@@ -370,7 +361,7 @@ public final class ApolloClient: Sendable {
370361
/// - Parameters:
371362
/// - query: The `GraphQLQuery` to fetch.
372363
/// - cachePolicy: A ``CachePolicy`` to use for this request. This function overload only accepts the
373-
/// ``CachePolicy/Query/CacheAndNetwork/cacheAndNetwork`` policy.
364+
/// ``CachePolicy/Query/CacheOnly/cacheOnly`` policy.
374365
/// - requestConfiguration: A ``RequestConfiguration`` to use for the watcher's initial fetch. Defaults to `nil`.
375366
/// If `nil` the receiver's ``ApolloClient/defaultRequestConfiguration`` will be used.
376367
/// - refetchOnFailedUpdates: Should the watcher perform a network fetch when it's watched objects have changed,
@@ -381,7 +372,7 @@ public final class ApolloClient: Sendable {
381372
/// to stop receiving new results.
382373
public func watch<Query: GraphQLQuery>(
383374
query: Query,
384-
cachePolicy: CachePolicy.Query.CacheAndNetwork,
375+
cachePolicy: CachePolicy.Query.CacheOnly,
385376
requestConfiguration: RequestConfiguration? = nil,
386377
refetchOnFailedUpdates: Bool = true,
387378
resultHandler: @escaping GraphQLQueryWatcher<Query>.ResultHandler
@@ -395,9 +386,13 @@ public final class ApolloClient: Sendable {
395386
)
396387
}
397388

398-
/// Watches a `GraphQLQuery` by first fetching an initial result from from the local cache. It does not attempt to
399-
/// fetch results from the server. The `resultHandler` is called after the initial fetch and again each time the data
400-
/// for the watched query changes in the local cache of this client's ``ApolloClient/store``.
389+
// MARK: Watch Query w/Fetch Behavior
390+
391+
/// Watches a `GraphQLQuery` by first fetching an initial result using the provided ``FetchBehavior``. The
392+
/// `resultHandler` is called after the initial fetch and again each time the data for the watched query changes in
393+
/// the local cache of this client's ``ApolloClient/store``.
394+
///
395+
/// This function triggers a fetch on the ``GraphQLQueryWatcher`` prior to returning it.
401396
///
402397
/// The ``GraphQLQueryWatcher`` returned by this function is notified whenever any of the data the query result
403398
/// depends on changes in the local cache. The ``GraphQLQueryWatcher`` retains the provided `resultHandler` and will
@@ -406,8 +401,8 @@ public final class ApolloClient: Sendable {
406401
///
407402
/// - Parameters:
408403
/// - query: The `GraphQLQuery` to fetch.
409-
/// - cachePolicy: A ``CachePolicy`` to use for this request. This function overload only accepts the
410-
/// ``CachePolicy/Query/CacheOnly/cacheOnly`` policy.
404+
/// - fetchBehavior: The ``FetchBehavior`` to use for this request.
405+
/// Determines if fetching will include cache/network fetches.
411406
/// - requestConfiguration: A ``RequestConfiguration`` to use for the watcher's initial fetch. Defaults to `nil`.
412407
/// If `nil` the receiver's ``ApolloClient/defaultRequestConfiguration`` will be used.
413408
/// - refetchOnFailedUpdates: Should the watcher perform a network fetch when it's watched objects have changed,
@@ -418,18 +413,21 @@ public final class ApolloClient: Sendable {
418413
/// to stop receiving new results.
419414
public func watch<Query: GraphQLQuery>(
420415
query: Query,
421-
cachePolicy: CachePolicy.Query.CacheOnly,
416+
fetchBehavior: FetchBehavior,
422417
requestConfiguration: RequestConfiguration? = nil,
423418
refetchOnFailedUpdates: Bool = true,
424419
resultHandler: @escaping GraphQLQueryWatcher<Query>.ResultHandler
425420
) async -> GraphQLQueryWatcher<Query> {
426-
return await self.watch(
421+
let watcher = await GraphQLQueryWatcher(
422+
client: self,
427423
query: query,
428-
fetchBehavior: cachePolicy.toFetchBehavior(),
429-
requestConfiguration: requestConfiguration,
430424
refetchOnFailedUpdates: refetchOnFailedUpdates,
431425
resultHandler: resultHandler
432426
)
427+
Task {
428+
await watcher.fetch(fetchBehavior: fetchBehavior, requestConfiguration: requestConfiguration)
429+
}
430+
return watcher
433431
}
434432

435433
// MARK: - Perform Mutation

0 commit comments

Comments
 (0)