Skip to content

Commit

Permalink
feat: async query pager fetch with cache policy
Browse files Browse the repository at this point in the history
  • Loading branch information
pondorasti committed Jul 27, 2024
1 parent e89e65d commit 007b07c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 6 additions & 2 deletions Sources/ApolloPagination/AsyncGraphQLQueryPager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,12 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
}

/// Fetches the first page.
public func fetch() async {
await pager.fetch()
/// - Parameters:
/// - cachePolicy: The Apollo `CachePolicy` to use. Defaults to `returnCacheDataAndFetch`.
public func fetch(
cachePolicy: CachePolicy = .returnCacheDataAndFetch
) async {
await pager.fetch(cachePolicy: cachePolicy)
}

/// Resets pagination state and cancels in-flight updates from the pager.
Expand Down
12 changes: 6 additions & 6 deletions Sources/ApolloPagination/AsyncGraphQLQueryPagerCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public protocol AsyncPagerType {
func loadNext(cachePolicy: CachePolicy) async throws
func loadAll(fetchFromInitialPage: Bool) async throws
func refetch(cachePolicy: CachePolicy) async
func fetch() async
func fetch(cachePolicy: CachePolicy) async
}

actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQuery: GraphQLQuery>: AsyncPagerType {
Expand Down Expand Up @@ -123,7 +123,7 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
reset()
isLoadingAll = true
group.addTask { [weak self] in
await self?.fetch(cachePolicy: .fetchIgnoringCacheData)
await self?.privateFetch(cachePolicy: .fetchIgnoringCacheData)
}
} else if initialPageResult == nil {
// Otherwise, we have to make sure that we have an `initialPageResult`
Expand Down Expand Up @@ -186,12 +186,12 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
func refetch(cachePolicy: CachePolicy = .fetchIgnoringCacheData) async {
assert(firstPageWatcher != nil, "To create consistent product behaviors, calling `fetch` before calling `refetch` will use cached data while still refreshing.")
reset()
await fetch(cachePolicy: cachePolicy)
await privateFetch(cachePolicy: cachePolicy)
}

func fetch() async {
func fetch(cachePolicy: CachePolicy = .returnCacheDataAndFetch) async {
reset()
await fetch(cachePolicy: .returnCacheDataAndFetch)
await privateFetch(cachePolicy: cachePolicy)
}

/// Cancels any in-flight fetching operations and unsubscribes from the store.
Expand Down Expand Up @@ -223,7 +223,7 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue

// MARK: - Private

private func fetch(cachePolicy: CachePolicy = .returnCacheDataAndFetch) async {
private func privateFetch(cachePolicy: CachePolicy) async {
await execute { [weak self] publisher in
guard let self else { return }
if await self.firstPageWatcher == nil {
Expand Down

0 comments on commit 007b07c

Please sign in to comment.