Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ApolloPagination] Remove convenience functions, improved Offset-pagination support #268

Merged
merged 7 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ final class AsyncGraphQLQueryPagerCoordinatorTests: XCTestCase, CacheDependentTe
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Reverse(
hasPrevious: data.hero.friendsConnection.pageInfo.hasPreviousPage,
startCursor: data.hero.friendsConnection.pageInfo.startCursor
Expand Down Expand Up @@ -201,7 +201,7 @@ final class AsyncGraphQLQueryPagerCoordinatorTests: XCTestCase, CacheDependentTe
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down
140 changes: 83 additions & 57 deletions Tests/ApolloPaginationTests/AsyncGraphQLQueryPagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,31 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
}

func test_forwardInit_simple() async throws {
let pager = await AsyncGraphQLQueryPager.makeForwardCursorQueryPager(
client: client,
queryProvider: { pageInfo in
let initialQuery = Query()
initialQuery.__variables = [
"id": "2001",
"first": 2,
"after": GraphQLNullable<String>.null
]
let pager = await AsyncGraphQLQueryPager.makeQueryPager(client: client, initialQuery: initialQuery) { page, direction in
switch direction {
case .next:
let nextQuery = Query()
nextQuery.__variables = [
"id": "2001",
"first": 2,
"after": pageInfo?.endCursor ?? GraphQLNullable<String>.null
"after": page.endCursor
]
return nextQuery
},
extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
case .previous:
return nil
}
)
} extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
}

let serverExpectation = Mocks.Hero.FriendsQuery.expectationForFirstPage(server: server)
await pager.fetch()
Expand All @@ -66,24 +73,31 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
let name: String
}

let pager = await AsyncGraphQLQueryPager.makeForwardCursorQueryPager(
client: client,
queryProvider: { pageInfo in
let initialQuery = Query()
initialQuery.__variables = [
"id": "2001",
"first": 2,
"after": GraphQLNullable<String>.null
]
let pager = await AsyncGraphQLQueryPager.makeQueryPager(client: client, initialQuery: initialQuery) { page, direction in
switch direction {
case .next:
let nextQuery = Query()
nextQuery.__variables = [
"id": "2001",
"first": 2,
"after": pageInfo?.endCursor ?? GraphQLNullable<String>.null
"after": page.endCursor
]
return nextQuery
},
extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
case .previous:
return nil
}
)
} extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
}

let serverExpectation = Mocks.Hero.FriendsQuery.expectationForFirstPage(server: server)
await pager.fetch()
Expand Down Expand Up @@ -116,30 +130,36 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
}

func test_forwardInit_singleQuery_transform() async throws {
let pager = await AsyncGraphQLQueryPager.makeForwardCursorQueryPager(
client: client,
queryProvider: { pageInfo in
let initialQuery = Query()
initialQuery.__variables = [
"id": "2001",
"first": 2,
"after": GraphQLNullable<String>.null
]
let pager = await AsyncGraphQLQueryPager.makeQueryPager(client: client, initialQuery: initialQuery) { page, direction in
switch direction {
case .next:
let nextQuery = Query()
nextQuery.__variables = [
"id": "2001",
"first": 2,
"after": pageInfo?.endCursor ?? GraphQLNullable<String>.null
"after": page.endCursor
]
return nextQuery
},
extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
},
transform: { previous, first, next in
let inOrderData = previous + [first] + next
return inOrderData.flatMap { data in
data.hero.friendsConnection.friends.map { friend in friend.name }
}
case .previous:
return nil
}
)
} extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
} transform: { previous, first, next in
let inOrderData = previous + [first] + next
return inOrderData.flatMap { data in
data.hero.friendsConnection.friends.map { friend in friend.name }
}
}

let serverExpectation = Mocks.Hero.FriendsQuery.expectationForFirstPage(server: server)
await pager.fetch()
Expand All @@ -166,30 +186,36 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
let name: String
}

let pager = await AsyncGraphQLQueryPager.makeForwardCursorQueryPager(
client: client,
queryProvider: { pageInfo in
let initialQuery = Query()
initialQuery.__variables = [
"id": "2001",
"first": 2,
"after": GraphQLNullable<String>.null
]
let pager = await AsyncGraphQLQueryPager.makeQueryPager(client: client, initialQuery: initialQuery) { page, direction in
switch direction {
case .next:
let nextQuery = Query()
nextQuery.__variables = [
"id": "2001",
"first": 2,
"after": pageInfo?.endCursor ?? GraphQLNullable<String>.null
"after": page.endCursor
]
return nextQuery
},
extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
},
transform: { previous, first, next in
let inOrderData = previous + [first] + next
return inOrderData.flatMap { data in
data.hero.friendsConnection.friends.map { friend in friend.name }
}
case .previous:
return nil
}
)
} extractPageInfo: { data in
CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
)
} transform: { previous, first, next in
let inOrderData = previous + [first] + next
return inOrderData.flatMap { data in
data.hero.friendsConnection.friends.map { friend in friend.name }
}
}

let serverExpectation = Mocks.Hero.FriendsQuery.expectationForFirstPage(server: server)
await pager.fetch()
Expand Down Expand Up @@ -317,7 +343,7 @@ final class AsyncGraphQLQueryPagerTests: XCTestCase {
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class BidirectionalPaginationTests: XCTestCase, CacheDependentTesting {
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Bidirectional(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor,
Expand Down
4 changes: 2 additions & 2 deletions Tests/ApolloPaginationTests/ConcurrencyTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ final class ConcurrencyTests: XCTestCase {
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down Expand Up @@ -107,7 +107,7 @@ final class ConcurrencyTests: XCTestCase {
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down
4 changes: 2 additions & 2 deletions Tests/ApolloPaginationTests/ForwardPaginationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ final class ForwardPaginationTests: XCTestCase, CacheDependentTesting {
initialQuery: initialQuery,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down Expand Up @@ -292,7 +292,7 @@ final class ForwardPaginationTests: XCTestCase, CacheDependentTesting {
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down
65 changes: 65 additions & 0 deletions Tests/ApolloPaginationTests/FriendsQuery+TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,68 @@ extension Mocks.Hero.BidirectionalFriendsQuery {
}
}
}

extension Mocks.Hero.OffsetFriendsQuery {
static func expectationForFirstPage(server: MockGraphQLServer) -> XCTestExpectation {
let query = MockQuery<Mocks.Hero.OffsetFriendsQuery>()
query.__variables = ["id": "2001", "offset": 0, "limit": 2]
return server.expect(query) { _ in
let friends: [[String: AnyHashable]] = [
[
"__typename": "Human",
"name": "Luke Skywalker",
"id": "1000",
],
[
"__typename": "Human",
"name": "Han Solo",
"id": "1002",
],
]

let hero: [String: AnyHashable] = [
"__typename": "Droid",
"id": "2001",
"name": "R2-D2",
"friends": friends,
]

let data: [String: AnyHashable] = [
"hero": hero
]

return [
"data": data
]
}
}

static func expectationForLastPage(server: MockGraphQLServer) -> XCTestExpectation {
let query = MockQuery<Mocks.Hero.OffsetFriendsQuery>()
query.__variables = ["id": "2001", "offset": 2, "limit": 2]
return server.expect(query) { _ in
let friends: [[String: AnyHashable]] = [
[
"__typename": "Human",
"name": "Leia Organa",
"id": "1003",
],
]

let hero: [String: AnyHashable] = [
"__typename": "Droid",
"id": "2001",
"name": "R2-D2",
"friends": friends,
]

let data: [String: AnyHashable] = [
"hero": hero
]

return [
"data": data
]
}
}
}
4 changes: 2 additions & 2 deletions Tests/ApolloPaginationTests/GraphQLQueryPagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class GraphQLQueryPagerTests: XCTestCase {
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down Expand Up @@ -69,7 +69,7 @@ final class GraphQLQueryPagerTests: XCTestCase {
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Reverse(
hasPrevious: data.hero.friendsConnection.pageInfo.hasPreviousPage,
startCursor: data.hero.friendsConnection.pageInfo.startCursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ final class GraphQLQueryPagerTestsCoordinator: XCTestCase, CacheDependentTesting
watcherDispatchQueue: .main,
extractPageInfo: { data in
switch data {
case .initial(let data), .paginated(let data):
case .initial(let data, _), .paginated(let data, _):
return CursorBasedPagination.Forward(
hasNext: data.hero.friendsConnection.pageInfo.hasNextPage,
endCursor: data.hero.friendsConnection.pageInfo.endCursor
Expand Down
Loading
Loading