Skip to content

Commit

Permalink
Added Existential Any requirement (apollographql/apollo-ios-dev#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev authored and gh-action-runner committed Jun 6, 2024
1 parent d8ddfdb commit 8e07319
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 42 deletions.
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.8
// swift-tools-version: 5.9

import PackageDescription

Expand Down Expand Up @@ -30,7 +30,8 @@ let package = Package(
.product(name: "Apollo", package: "apollo-ios"),
.product(name: "ApolloAPI", package: "apollo-ios"),
.product(name: "OrderedCollections", package: "swift-collections"),
]
],
swiftSettings: [.enableUpcomingFeature("ExistentialAny")]
),
]
)
10 changes: 5 additions & 5 deletions Sources/ApolloPagination/AsyncGraphQLQueryPager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
/// Type-erases a query pager, transforming data from a generic type to a specific type, often a view model or array of view models.
public class AsyncGraphQLQueryPager<Model>: Publisher {
public typealias Failure = Never
public typealias Output = Result<(Model, UpdateSource), Error>
public typealias Output = Result<(Model, UpdateSource), any Error>
let _subject: CurrentValueSubject<Output?, Never> = .init(nil)
var publisher: AnyPublisher<Output, Never> { _subject.compactMap({ $0 }).eraseToAnyPublisher() }
@Atomic public var cancellables: Set<AnyCancellable> = []
Expand Down Expand Up @@ -92,7 +92,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
PaginatedQuery: GraphQLQuery,
Element
>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
Expand Down Expand Up @@ -138,7 +138,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
InitialQuery: GraphQLQuery,
PaginatedQuery: GraphQLQuery
>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
Expand All @@ -161,7 +161,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
InitialQuery: GraphQLQuery,
PaginatedQuery: GraphQLQuery
>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
Expand Down Expand Up @@ -248,7 +248,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {

public func receive<S>(
subscriber: S
) where S: Subscriber, Never == S.Failure, Result<(Model, UpdateSource), Error> == S.Input {
) where S: Subscriber, Never == S.Failure, Result<(Model, UpdateSource), any Error> == S.Input {
publisher.subscribe(subscriber)
}
}
Expand Down
28 changes: 14 additions & 14 deletions Sources/ApolloPagination/AsyncGraphQLQueryPagerCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
let initialQuery: InitialQuery
var isLoadingAll: Bool = false
var isFetching: Bool = false
let nextPageResolver: (PaginationInfo) -> PaginatedQuery?
let previousPageResolver: (PaginationInfo) -> PaginatedQuery?
let extractPageInfo: (PageExtractionData<InitialQuery, PaginatedQuery, PaginationOutput<InitialQuery, PaginatedQuery>?>) -> PaginationInfo
var nextPageInfo: PaginationInfo? { nextPageTransformation() }
var previousPageInfo: PaginationInfo? { previousPageTransformation() }
let nextPageResolver: (any PaginationInfo) -> PaginatedQuery?
let previousPageResolver: (any PaginationInfo) -> PaginatedQuery?
let extractPageInfo: (PageExtractionData<InitialQuery, PaginatedQuery, PaginationOutput<InitialQuery, PaginatedQuery>?>) -> any PaginationInfo
var nextPageInfo: (any PaginationInfo)? { nextPageTransformation() }
var previousPageInfo: (any PaginationInfo)? { previousPageTransformation() }

var canLoadPages: (next: Bool, previous: Bool) {
(canLoadNext, canLoadPrevious)
Expand All @@ -42,7 +42,7 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
return ($previousPageVarMap, $initialPageResult, $nextPageVarMap)
}

typealias ResultType = Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), Error>
typealias ResultType = Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), any Error>

@Published var currentValue: ResultType?
private var queuedValue: ResultType?
Expand All @@ -60,8 +60,8 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
/// Maps each query variable set to latest results from internal watchers.
@Published var nextPageVarMap: OrderedDictionary<Set<JSONValue>, PaginatedQuery.Data> = [:]
@Published var previousPageVarMap: OrderedDictionary<Set<JSONValue>, PaginatedQuery.Data> = [:]
private var tasks: Set<Task<Void, Error>> = []
private var taskGroup: ThrowingTaskGroup<Void, Error>?
private var tasks: Set<Task<Void, any Error>> = []
private var taskGroup: ThrowingTaskGroup<Void, any Error>?
private var watcherCallbackQueue: DispatchQueue

/// Designated Initializer
Expand All @@ -72,7 +72,7 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
/// - nextPageResolver: The resolver that can derive the query for loading more. This can be a different query than the `initialQuery`.
/// - onError: The callback when there is an error.
init<P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, PaginationOutput<InitialQuery, PaginatedQuery>?>) -> P,
Expand Down Expand Up @@ -168,7 +168,7 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
}

func subscribe(
onUpdate: @escaping (Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), Error>) -> Void
onUpdate: @escaping (Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), any Error>) -> Void
) -> AnyCancellable {
$currentValue.compactMap({ $0 })
.sink { [weak self] result in
Expand Down Expand Up @@ -287,7 +287,7 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
private func onFetch<DataType: RootSelectionSet>(
fetchType: FetchType,
cachePolicy: CachePolicy,
result: Result<GraphQLResult<DataType>, Error>,
result: Result<GraphQLResult<DataType>, any Error>,
publisher: CurrentValueSubject<Void, Never>
) {
switch result {
Expand All @@ -311,7 +311,7 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
shouldUpdate = true
}

var value: Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), Error>?
var value: Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), any Error>?
var output: ([PaginatedQuery.Data], InitialQuery.Data, [PaginatedQuery.Data])?
switch fetchType {
case .initial:
Expand Down Expand Up @@ -360,15 +360,15 @@ actor AsyncGraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQue
}
}

private func nextPageTransformation() -> PaginationInfo? {
private func nextPageTransformation() -> (any PaginationInfo)? {
let currentValue = try? currentValue?.get().0
guard let last = nextPageVarMap.values.last else {
return initialPageResult.flatMap { extractPageInfo(.initial($0, currentValue)) }
}
return extractPageInfo(.paginated(last, currentValue))
}

private func previousPageTransformation() -> PaginationInfo? {
private func previousPageTransformation() -> (any PaginationInfo)? {
let currentValue = try? currentValue?.get().0
guard let first = previousPageVarMap.values.last else {
return initialPageResult.flatMap { extractPageInfo(.initial($0, currentValue)) }
Expand Down
24 changes: 12 additions & 12 deletions Sources/ApolloPagination/GraphQLQueryPager+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public extension GraphQLQueryPager {
/// Convenience initializer for creating a pager that has a single query and does not
/// transform output responses.
convenience init<InitialQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -28,7 +28,7 @@ public extension GraphQLQueryPager {
/// Convenience initializer for creating a pager that has a single query and
/// transforms output responses.
convenience init<InitialQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -50,7 +50,7 @@ public extension GraphQLQueryPager {
/// Convenience initializer for creating a pager that has a single query and
/// transforms output responses into a collection.
convenience init<InitialQuery: GraphQLQuery, T, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -73,7 +73,7 @@ public extension GraphQLQueryPager {
/// Convenience initializer for creating a multi-query pager that does not
/// transform output responses.
convenience init<InitialQuery: GraphQLQuery, PaginatedQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractInitialPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -96,7 +96,7 @@ public extension GraphQLQueryPager {

/// Convenience initializer for creating a multi-query pager that transforms output responses.
convenience init<InitialQuery: GraphQLQuery, PaginatedQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractInitialPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -122,7 +122,7 @@ public extension GraphQLQueryPager {
/// Convenience initializer for creating a multi-query pager that
/// transforms output responses into collections
convenience init<InitialQuery: GraphQLQuery, PaginatedQuery: GraphQLQuery, T, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractInitialPageInfo: @escaping (InitialQuery.Data) -> P,
Expand Down Expand Up @@ -154,7 +154,7 @@ public extension AsyncGraphQLQueryPager {
/// Convenience initializer for creating a pager that has a single query and does not
/// transform output responses.
convenience init<InitialQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -173,7 +173,7 @@ public extension AsyncGraphQLQueryPager {
/// Convenience initializer for creating a pager that has a single query and
/// transforms output responses.
convenience init<InitialQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -195,7 +195,7 @@ public extension AsyncGraphQLQueryPager {
/// Convenience initializer for creating a pager that has a single query and
/// transforms output responses into a collection.
convenience init<InitialQuery: GraphQLQuery, T, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -218,7 +218,7 @@ public extension AsyncGraphQLQueryPager {
/// Convenience initializer for creating a multi-query pager that does not
/// transform output responses.
convenience init<InitialQuery: GraphQLQuery, PaginatedQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractInitialPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -242,7 +242,7 @@ public extension AsyncGraphQLQueryPager {
/// Convenience initializer for creating a multi-query pager that
/// transforms output responses.
convenience init<InitialQuery: GraphQLQuery, PaginatedQuery: GraphQLQuery, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractInitialPageInfo: @escaping (InitialQuery.Data) -> P,
Expand All @@ -268,7 +268,7 @@ public extension AsyncGraphQLQueryPager {
/// Convenience initializer for creating a multi-query pager that
/// transforms output responses into collections
convenience init<InitialQuery: GraphQLQuery, PaginatedQuery: GraphQLQuery, T, P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractInitialPageInfo: @escaping (InitialQuery.Data) -> P,
Expand Down
10 changes: 5 additions & 5 deletions Sources/ApolloPagination/GraphQLQueryPager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
/// Type-erases a query pager, transforming data from a generic type to a specific type, often a view model or array of view models.
public class GraphQLQueryPager<Model>: Publisher {
public typealias Failure = Never
public typealias Output = Result<(Model, UpdateSource), Error>
public typealias Output = Result<(Model, UpdateSource), any Error>
let _subject: CurrentValueSubject<Output?, Never> = .init(nil)
var publisher: AnyPublisher<Output, Never> { _subject.compactMap { $0 }.eraseToAnyPublisher() }
public var cancellables: Set<AnyCancellable> = []
Expand Down Expand Up @@ -76,7 +76,7 @@ public class GraphQLQueryPager<Model>: Publisher {
InitialQuery: GraphQLQuery,
PaginatedQuery: GraphQLQuery
>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
Expand All @@ -98,7 +98,7 @@ public class GraphQLQueryPager<Model>: Publisher {
PaginatedQuery: GraphQLQuery,
Element
>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
Expand Down Expand Up @@ -144,7 +144,7 @@ public class GraphQLQueryPager<Model>: Publisher {
InitialQuery: GraphQLQuery,
PaginatedQuery: GraphQLQuery
>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
Expand Down Expand Up @@ -258,7 +258,7 @@ public class GraphQLQueryPager<Model>: Publisher {

public func receive<S>(
subscriber: S
) where S: Subscriber, Never == S.Failure, Result<(Model, UpdateSource), Error> == S.Input {
) where S: Subscriber, Never == S.Failure, Result<(Model, UpdateSource), any Error> == S.Input {
publisher.subscribe(subscriber)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/ApolloPagination/GraphQLQueryPagerCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class GraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQuery: G
private var subscriptions = Subscriptions()
private var completionManager = CompletionManager()

var publisher: AnyPublisher<Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), Error>, Never> {
var publisher: AnyPublisher<Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), any Error>, Never> {
get async { await pager.$currentValue.compactMap { $0 }.eraseToAnyPublisher() }
}

init<P: PaginationInfo>(
client: ApolloClientProtocol,
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, PaginationOutput<InitialQuery, PaginatedQuery>?>) -> P,
Expand Down Expand Up @@ -87,7 +87,7 @@ class GraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQuery: G

/// Allows the caller to subscribe to new pagination results.
/// - Parameter onUpdate: A closure which provides the most recent pagination result. Execution may be on any thread.
func subscribe(onUpdate: @escaping (Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), Error>) -> Void) {
func subscribe(onUpdate: @escaping (Result<(PaginationOutput<InitialQuery, PaginatedQuery>, UpdateSource), any Error>) -> Void) {
Task { [weak self] in
guard let self else { return }
let subscription = await self.pager.subscribe(onUpdate: onUpdate)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloPagination/PaginationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public enum PaginationError: Error {
case noQuery
case cancellation
// Workaround for https://github.com/apple/swift-evolution/blob/f0128e6ed3cbea226c66c8ac630e216dd4140a69/proposals/0413-typed-throws.md
case unknown(Error)
case unknown(any Error)
}

0 comments on commit 8e07319

Please sign in to comment.