您如何在Relay-esk模式(使用SQL数据源)中使用嵌套游标管理有效的数据提取?
例如,我有一个结构如下:
enum BookSortKeys {
ID,
TITLE,
PRICE,
UPDATED_AT,
CREATED_AT
}
enum ReviewSortKeys {
ID,
REVIEW,
UPDATED_AT,
CREATED_AT
}
type Book {
id: ID!
title: String!
description: String
price: Float!
updatedAt: String!
createdAt: String!
reviews("""
Returns the elements that come after the specified cursor.
"""
after: String
"""
Returns the elements that come before the specified cursor.
"""
before: String
"""
Returns up to the first `n` elements from the list.
"""
first: Int
"""
Returns up to the last `n` elements from the list.
"""
last: Int
"""
Reverse the order of the underlying list.
"""
reverse: Boolean = false
"""
Sort the underlying list by the given key.
"""
sortKey: ReviewSortKeys = ID): ReviewConnection!
}
type Query {
books("""
Returns the elements that come after the specified cursor.
"""
after: String
"""
Returns the elements that come before the specified cursor.
"""
before: String
"""
Returns up to the first `n` elements from the list.
"""
first: Int
"""
Returns up to the last `n` elements from the list.
"""
last: Int
"""
Supported filter parameters:
- `title`
- `id`
- `price`
- `description`
- `created_at`
- `updated_at`
"""
query: String
"""
Reverse the order of the underlying list.
"""
reverse: Boolean = false
"""
Sort the underlying list by the given key.
"""
sortKey: BookSortKeys = ID): BookConnection!
}
type ReviewConnection {
pageInfo: PageInfo!
edges: [ReviewEdge!]!
}
type ReviewEdge {
cursor: String!
node: Review!
}
type BookConnection {
pageInfo: PageInfo!
edges: [BookEdge!]!
}
type BookEdge {
cursor: String!
node: Book!
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
}
type Review {
review: String!
id: ID!
updatedAt: String!
createdAt: String!
}
type Mutation {
}
schema {
query: Query
mutation: Mutation
}
我想执行如下查询,并以最有效的方式检索数据。
query GET_BOOKS {
books(first:10, sortKey: PRICE, reverse: true) {
pageInfo {
hasNextPage
hasPreviousPage
}
edges {
cursor
node {
id
title
description
reviews(after:"base64-cursor" first: 5, sortKey: CREATED_AT) {
edges {
node{
review
}
}
}
}
}
}
}
我可以很容易地将顶部查询(书)的所有分页参数转换为sql语句,但是使用嵌套游标,我只能看到2个选项(如上所述)...之前遇到的当前问题实现这些选项的是:
LIMIT
和WHERE createdAt > :after_cursor_val
SELECT * FROM reviews "reviews" WHERE "reviews".bookId IN (...list of book ids batched)
-将添加LIMIT
,ORDER BY
和WHERE createdAt > :cursor
,出乎意料的结果,因为我的结果集是多个“书本ID”中条目的混合?