我有一个按 startDatetime 时间戳排序的游标基础分页中继连接。查询如下所示;
query UnassignedShiftsQuery($first: Int, $cursor: String) {
user {
id
unassignedShifts(first: $first, after: $cursor) {
edges {
__typename
cursor
node {
...OpenShiftFragment
}
}
pageInfo {
...PageInfoFragment
}
__typename
}
}
}
${OPEN_SHIFT_FRAGMENT}
${PAGE_INFO_FRAGMENT}
以及苦艾酒中的相关模式:
connection(node_type: :open_shift, non_null_edges: true, non_null_edge: true) do
edge do
field :node, non_null(:open_shift)
end
end
object :user do
field :id, non_null(:string)
connection field :unassigned_shifts, node_type: :open_shift, non_null_connection: true do resolve(&Resolvers.Shifts.list_shifts/3)
end
end
object :open_shift do
field :id, non_null(:string)
field :start_datetime, non_null(:datetime)
end
还有 Shifts 解析器;
def list_shifts(_parent, args, %{context: %{current_user: user}}) do
Absinthe.Relay.Connection.from_query(
Shifts.unassigned_shifts(user),
&get_preloaded_shifts/1,
args
)
end
defp get_preloaded_shifts(query) do
query
|> Repo.all()
|> Repo.preload([:shift_type])
end
为简洁起见,可以假设 Shifts.unassigned_shifts 在哪里;
def unassigned_shifts(user) do
from shifts in Backend.Shifts.Shift,
order_by: shifts.start_datetime,
where: shifts.user_id == ^user.id
end
在呈现初始查询后,用户可以选择他们想跳转到分页列表中剩余的任意日期。这个东西很大,所以他们可能想跳到一个日期,然后从那里来回滚动。
在随后的查询中,我可以在 fetchMore 查询中传递该日期,但如何在解析器的 Absinthe.Relay.Connection.from_query 调用中应用该日期。简单地说,我认为我需要获取日期,将其转换为游标并将该游标传递给 from_query 采用的参数,但我不确定我是否走在正确的轨道上或如何去做。
我对 Elixir 相当陌生,所以无法通过阅读 Absinthe 的源代码来解决这个问题,所以任何指针都会很有帮助。
谢谢!