Skip to content

Commit

Permalink
Support Partial Success
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-Ham committed Jul 15, 2024
1 parent 26903ce commit 5040fbc
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 285 deletions.
37 changes: 17 additions & 20 deletions Tests/ApolloPaginationTests/AsyncGraphQLQueryPagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
let subscription = pager
.compactMap { output -> [ViewModel]? in
guard case .success((let output, _)) = output else { return nil }
let inOrderData = output.previousPages + [output.initialPage] + output.nextPages
let models = inOrderData.flatMap { data in
let models = output.allPages.flatMap { data in
data.hero.friendsConnection.friends.map { friend in ViewModel(name: friend.name) }
}
return models
Expand Down Expand Up @@ -169,9 +168,8 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
return nil
}
} ,
transform: { previous, first, next in
let inOrderData = previous + [first] + next
return inOrderData.flatMap { data in
transform: { output in
output.allPages.flatMap { data in
data.hero.friendsConnection.friends.map { friend in friend.name }
}
}
Expand Down Expand Up @@ -231,9 +229,8 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
return nil
}
},
transform: { previous, first, next in
let inOrderData = previous + [first] + next
return inOrderData.flatMap { data in
transform: { output in
output.allPages.flatMap { data in
data.hero.friendsConnection.friends.map { friend in friend.name }
}
}
Expand Down Expand Up @@ -274,8 +271,10 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
}

let anyPager = createPager().eraseToAnyPager { data in
data.hero.friendsConnection.friends.map {
ViewModel(name: $0.name)
data.allPages.flatMap { data in
data.hero.friendsConnection.friends.map {
ViewModel(name: $0.name)
}
}
}

Expand Down Expand Up @@ -306,11 +305,11 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
}

func test_passesBackSeparateData() async throws {
let anyPager = createPager().eraseToAnyPager { _, initial, next in
if let latestPage = next.last {
let anyPager = createPager().eraseToAnyPager { output in
if let latestPage = output.nextPages.last {
return latestPage.hero.friendsConnection.friends.last?.name
}
return initial.hero.friendsConnection.friends.last?.name
return output.initialPage?.hero.friendsConnection.friends.last?.name
}

let initialExpectation = expectation(description: "Initial")
Expand Down Expand Up @@ -355,16 +354,14 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
}

func test_equatable() async {
let pagerA = AsyncGraphQLQueryPager(pager: createPager(), transform: { previous, initial, next in
let allPages = previous + [initial] + next
return allPages.flatMap { data in
let pagerA = AsyncGraphQLQueryPager(pager: createPager(), transform: { output in
output.allPages.flatMap { data in
data.hero.friendsConnection.friends.map { $0.name }
}
})

let pagerB = AsyncGraphQLQueryPager(pager: createPager(), transform: { previous, initial, next in
let allPages = previous + [initial] + next
return allPages.flatMap { data in
let pagerB = AsyncGraphQLQueryPager(pager: createPager(), transform: { output in
return output.allPages.flatMap { data in
data.hero.friendsConnection.friends.map { $0.name }
}
})
Expand Down Expand Up @@ -404,7 +401,7 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
subscription.cancel()
}

func test_errors_noSuccess() async throws {
func test_errors_noData() async throws {
let pager = AsyncGraphQLQueryPager(pager: createPager())
var expectedResults: [Result<(PaginationOutput<Query, Query>, UpdateSource), any Error>] = []
let serverExpectation = Mocks.Hero.FriendsQuery.expectationForFirstPageErrorsOnly(server: server)
Expand Down
22 changes: 12 additions & 10 deletions Tests/ApolloPaginationTests/BidirectionalPaginationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ final class BidirectionalPaginationTests: XCTestCase, CacheDependentTesting {
results.append(result)
XCTAssertSuccessResult(result) { (output, source) in
XCTAssertTrue(output.nextPages.isEmpty)
XCTAssertEqual(output.initialPage.hero.friendsConnection.friends.count, 1)
XCTAssertEqual(output.initialPage.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.friends.count, 1)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(source, .fetch)
}

Expand Down Expand Up @@ -192,10 +192,12 @@ final class BidirectionalPaginationTests: XCTestCase, CacheDependentTesting {

let (result, _) = try await XCTUnwrapping(try await pager.currentValue?.get())
XCTAssertFalse(result.previousPages.isEmpty)
XCTAssertEqual(result.initialPage.hero.friendsConnection.friends.count, 1)
XCTAssertEqual(result.initialPage?.hero.friendsConnection.friends.count, 1)
XCTAssertFalse(result.nextPages.isEmpty)

let friends = (result.previousPages.first?.hero.friendsConnection.friends ?? []) + result.initialPage.hero.friendsConnection.friends + (result.nextPages.first?.hero.friendsConnection.friends ?? [])
let friends = result.previousPages.flatMap(\.hero.friendsConnection.friends)
+ (result.initialPage?.hero.friendsConnection.friends ?? [])
+ result.nextPages.flatMap(\.hero.friendsConnection.friends)

XCTAssertEqual(Set(friends).count, 3)
}
Expand All @@ -218,8 +220,8 @@ final class BidirectionalPaginationTests: XCTestCase, CacheDependentTesting {
results.append(result)
XCTAssertSuccessResult(result) { (output, source) in
XCTAssertTrue(output.nextPages.isEmpty)
XCTAssertEqual(output.initialPage.hero.friendsConnection.friends.count, 1)
XCTAssertEqual(output.initialPage.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.friends.count, 1)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(source, .fetch)
}

Expand Down Expand Up @@ -298,12 +300,12 @@ final class BidirectionalPaginationTests: XCTestCase, CacheDependentTesting {

let result = try await XCTUnwrapping(try await pager.pager.currentValue?.get().0)
XCTAssertFalse(result.previousPages.isEmpty)
XCTAssertEqual(result.initialPage.hero.friendsConnection.friends.count, 1)
XCTAssertEqual(result.initialPage?.hero.friendsConnection.friends.count, 1)
XCTAssertFalse(result.nextPages.isEmpty)

let friends = (result.previousPages.first?.hero.friendsConnection.friends ?? [])
+ result.initialPage.hero.friendsConnection.friends
+ (result.nextPages.first?.hero.friendsConnection.friends ?? [])
let friends = result.previousPages.flatMap(\.hero.friendsConnection.friends)
+ (result.initialPage?.hero.friendsConnection.friends ?? [])
+ result.nextPages.flatMap(\.hero.friendsConnection.friends)

XCTAssertEqual(Set(friends).count, 3)
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/ApolloPaginationTests/ForwardPaginationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ final class ForwardPaginationTests: XCTestCase, CacheDependentTesting {
results.append(result)
XCTAssertSuccessResult(result) { (output, source) in
XCTAssertTrue(output.nextPages.isEmpty)
XCTAssertEqual(output.initialPage.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(source, .fetch)
}

Expand Down Expand Up @@ -185,8 +185,8 @@ final class ForwardPaginationTests: XCTestCase, CacheDependentTesting {
let result = try await XCTUnwrapping(await pager.currentValue)
XCTAssertSuccessResult(result) { (output, source) in
XCTAssertTrue(output.nextPages.isEmpty)
XCTAssertEqual(output.initialPage.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(source, .fetch)
}

Expand Down Expand Up @@ -228,7 +228,7 @@ final class ForwardPaginationTests: XCTestCase, CacheDependentTesting {
await fulfillment(of: [transactionExpectation, mutationExpectation])
let finalResult = try await XCTUnwrapping(await pager.currentValue)
XCTAssertSuccessResult(finalResult) { (output, _) in
XCTAssertEqual(output.initialPage.hero.name, "C3PO")
XCTAssertEqual(output.initialPage?.hero.name, "C3PO")
XCTAssertEqual(output.nextPages.count, 1)
XCTAssertEqual(output.nextPages.first?.hero.name, "C3PO")
}
Expand Down
40 changes: 21 additions & 19 deletions Tests/ApolloPaginationTests/GraphQLQueryPagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ final class GraphQLQueryPagerTests: XCTestCase {
}

let anyPager = createPager().eraseToAnyPager { data in
data.hero.friendsConnection.friends.map {
ViewModel(name: $0.name)
data.allPages.flatMap { data in
data.hero.friendsConnection.friends.map {
ViewModel(name: $0.name)
}
}
}

Expand Down Expand Up @@ -168,8 +170,10 @@ final class GraphQLQueryPagerTests: XCTestCase {
}

let anyPager = createPager().eraseToAnyPager { data in
data.hero.friendsConnection.friends.map {
ViewModel(name: $0.name)
data.allPages.flatMap { data in
data.hero.friendsConnection.friends.map {
ViewModel(name: $0.name)
}
}
}

Expand Down Expand Up @@ -221,11 +225,11 @@ final class GraphQLQueryPagerTests: XCTestCase {
}

func test_passesBackSeparateData() throws {
let anyPager = createPager().eraseToAnyPager { _, initial, next in
if let latestPage = next.last {
let anyPager = createPager().eraseToAnyPager { output in
if let latestPage = output.nextPages.last {
return latestPage.hero.friendsConnection.friends.last?.name
}
return initial.hero.friendsConnection.friends.last?.name
return output.initialPage?.hero.friendsConnection.friends.last?.name
}

let initialExpectation = expectation(description: "Initial")
Expand Down Expand Up @@ -260,11 +264,11 @@ final class GraphQLQueryPagerTests: XCTestCase {
}

func test_reversePager_loadPrevious() throws {
let anyPager = createReversePager().eraseToAnyPager { previous, initial, _ in
if let latestPage = previous.last {
let anyPager = createReversePager().eraseToAnyPager { output in
if let latestPage = output.previousPages.last {
return latestPage.hero.friendsConnection.friends.first?.name
}
return initial.hero.friendsConnection.friends.first?.name
return output.initialPage?.hero.friendsConnection.friends.first?.name
}

let initialExpectation = expectation(description: "Initial")
Expand Down Expand Up @@ -303,11 +307,11 @@ final class GraphQLQueryPagerTests: XCTestCase {
@available(iOS 16.0, macOS 13.0, *)
func test_pager_reset_calls_callback() async throws {
server.customDelay = .milliseconds(1)
let pager = createPager().eraseToAnyPager { _, initial, next in
if let latestPage = next.last {
let pager = createPager().eraseToAnyPager { output in
if let latestPage = output.nextPages.last {
return latestPage.hero.friendsConnection.friends.last?.name
}
return initial.hero.friendsConnection.friends.last?.name
return output.initialPage?.hero.friendsConnection.friends.last?.name
}
let serverExpectation = Mocks.Hero.FriendsQuery.expectationForFirstPage(server: server)

Expand All @@ -325,16 +329,14 @@ final class GraphQLQueryPagerTests: XCTestCase {
}

func test_equatable() {
let pagerA = GraphQLQueryPager(pager: createPager(), transform: { previous, initial, next in
let allPages = previous + [initial] + next
return allPages.flatMap { data in
let pagerA = GraphQLQueryPager(pager: createPager(), transform: { output in
output.allPages.flatMap { data in
data.hero.friendsConnection.friends.map { $0.name }
}
})

let pagerB = GraphQLQueryPager(pager: createPager(), transform: { previous, initial, next in
let allPages = previous + [initial] + next
return allPages.flatMap { data in
let pagerB = GraphQLQueryPager(pager: createPager(), transform: { output in
output.allPages.flatMap { data in
data.hero.friendsConnection.friends.map { $0.name }
}
})
Expand Down
7 changes: 2 additions & 5 deletions Tests/ApolloPaginationTests/OffsetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ final class OffsetTests: XCTestCase {
case .initial(let data, let output), .paginated(let data, let output):
var totalOffset: Int = 0
if let output {
let pages = (output.previousPages + [output.initialPage] + output.nextPages)
pages.forEach { page in
output.allPages.forEach { page in
totalOffset += page.hero.friends.count
}
}
Expand Down Expand Up @@ -95,9 +94,7 @@ final class OffsetTests: XCTestCase {
let cancellable = pager.map { value in
switch value {
case .success((let output, _)):
let pages = output.previousPages + [output.initialPage] + output.nextPages

let friends = pages.flatMap { data in
let friends = output.allPages.flatMap { data in
data.hero.friends.map { friend in
ViewModel(name: friend.name)
}
Expand Down
36 changes: 6 additions & 30 deletions Tests/ApolloPaginationTests/PagerCoordinator+Erase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,24 @@

extension GraphQLQueryPagerCoordinator {
func eraseToAnyPager<T>(
transform: @escaping ([PaginatedQuery.Data], InitialQuery.Data, [PaginatedQuery.Data]) throws -> T
transform: @escaping (PaginationOutput<InitialQuery, PaginatedQuery>) throws -> T
) -> GraphQLQueryPager<T> {
GraphQLQueryPager(pager: self, transform: transform)
}

func eraseToAnyPager<T, S: RangeReplaceableCollection>(
initialTransform: @escaping (InitialQuery.Data) throws -> S,
nextPageTransform: @escaping (PaginatedQuery.Data) throws -> S
) -> GraphQLQueryPager<S> where T == S.Element {
GraphQLQueryPager(
pager: self,
initialTransform: initialTransform,
pageTransform: nextPageTransform
)
}

func eraseToAnyPager<T, S: RangeReplaceableCollection>(
transform: @escaping (InitialQuery.Data) throws -> S
transform: @escaping (PaginationOutput<InitialQuery, InitialQuery>) throws -> S
) -> GraphQLQueryPager<S> where InitialQuery == PaginatedQuery, T == S.Element {
GraphQLQueryPager(
pager: self,
initialTransform: transform,
pageTransform: transform
transform: transform
)
}
}

extension AsyncGraphQLQueryPagerCoordinator {
nonisolated func eraseToAnyPager<T>(
transform: @escaping ([PaginatedQuery.Data], InitialQuery.Data, [PaginatedQuery.Data]) throws -> T
transform: @escaping (PaginationOutput<InitialQuery, PaginatedQuery>) throws -> T
) -> AsyncGraphQLQueryPager<T> {
AsyncGraphQLQueryPager(
pager: self,
Expand All @@ -40,23 +28,11 @@ extension AsyncGraphQLQueryPagerCoordinator {
}

nonisolated func eraseToAnyPager<T, S: RangeReplaceableCollection>(
initialTransform: @escaping (InitialQuery.Data) throws -> S,
pageTransform: @escaping (PaginatedQuery.Data) throws -> S
) -> AsyncGraphQLQueryPager<S> where T == S.Element {
AsyncGraphQLQueryPager(
pager: self,
initialTransform: initialTransform,
pageTransform: pageTransform
)
}

nonisolated func eraseToAnyPager<T, S: RangeReplaceableCollection>(
transform: @escaping (InitialQuery.Data) throws -> S
transform: @escaping (PaginationOutput<InitialQuery, InitialQuery>) throws -> S
) -> AsyncGraphQLQueryPager<S> where InitialQuery == PaginatedQuery, T == S.Element {
AsyncGraphQLQueryPager(
pager: self,
initialTransform: transform,
pageTransform: transform
transform: transform
)
}
}
4 changes: 2 additions & 2 deletions Tests/ApolloPaginationTests/ReversePaginationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ final class ReversePaginationTests: XCTestCase, CacheDependentTesting {
results.append(result)
XCTAssertSuccessResult(result) { (output, source) in
XCTAssertTrue(output.nextPages.isEmpty)
XCTAssertEqual(output.initialPage.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(source, .fetch)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/ApolloPaginationTests/SubscribeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ final class SubscribeTest: XCTestCase, CacheDependentTesting {
let result = try XCTUnwrap(results.first)
XCTAssertSuccessResult(result) { (output, source) in
XCTAssertTrue(output.nextPages.isEmpty)
XCTAssertEqual(output.initialPage.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.friends.count, 2)
XCTAssertEqual(output.initialPage?.hero.friendsConnection.totalCount, 3)
XCTAssertEqual(source, .fetch)
XCTAssertEqual(results.count, otherResults.count)
}
Expand Down
Loading

0 comments on commit 5040fbc

Please sign in to comment.