Skip to content
19 changes: 17 additions & 2 deletions Sources/Apollo/GraphQLQueryWatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo
}

func fetch(cachePolicy: CachePolicy) {
// Cancel anything already in flight before starting a new fetch
fetching?.cancel()
// Cancel any fetch already in flight before starting a new fetch, if the new fetch includes the server.
// Without this extra condition, the store subscription can entirely cancel a server fetch if a dependent key is modified
// while it is in flight.
if cachePolicy.alwaysIncludesServerFetch {
fetching?.cancel()
}
fetching = client?.fetch(query: query, cachePolicy: cachePolicy, context: &context, queue: .main) { [weak self] result in
guard let `self` = self else { return }

Expand Down Expand Up @@ -70,3 +74,14 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo
}
}
}

private extension CachePolicy {
var alwaysIncludesServerFetch: Bool {
switch self {
case .fetchIgnoringCacheCompletely, .fetchIgnoringCacheData, .returnCacheDataAndFetch:
return true
case .returnCacheDataDontFetch, .returnCacheDataElseFetch:
Comment thread
designatednerd marked this conversation as resolved.
Outdated
return false
}
}
}