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 1 commit
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
Prev Previous commit
Next Next commit
Remove convenience functions
  • Loading branch information
Iron-Ham committed Feb 14, 2024
commit 53054e52ed998b259451f1f6915e6a5f2821ec17
138 changes: 82 additions & 56 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
Loading
Loading