2

I’ve got a cursor base paginated relay connection which is ordered by a startDatetime timestamp. The query looks like this;

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}

And the relevant schema in Absinthe:

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

And the Shifts resolver;

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

Where, for the sake of brevity Shifts.unassigned_shifts could be assumed to be;

def unassigned_shifts(user) do
  from shifts in Backend.Shifts.Shift,
    order_by: shifts.start_datetime,
    where: shifts.user_id == ^user.id
end

After the the initial query is rendered, the user can select an arbitrary date that they would like to jump to whist remaining in the paginated list. This thing is huge, so they may want to jump to a date and then scroll back and forth from there.

In subsequent queries I can pass that date in the fetchMore query, but how to I apply that in the Absinthe.Relay.Connection.from_query call in the resolver. Simplistically I’m thinking I need to take the date, turn that into a cursor and pass that cursor into the args that from_query takes, but I’m not sure if I’m on the right track or how to go about it.

I’m reasonably new to Elixir so haven’t been able to work this out from reading the Absinthe source so any pointers would be really helpful.

Thanks!

james2m
  • 1,562
  • 12
  • 15
  • Why since this is not exactly pre-filtering data, why don't you entirely handle this on the frontend? – Peter Malik Dec 26 '21 at 23:44
  • It is pre-filtered I just left that out for simplicity as the query is massive. But irrespective of pre-filtering, this is paginated because there are hundreds of thousands of records, entirely outside the realm of a front end (which is a react-native mobile app). – james2m Dec 28 '21 at 00:52
  • What I would do in this case is create a completely new mutation that takes the date as an argument. Assuming your pagination is based on a timestamp, I would locate where the specific item is in terms of pagination, on the backend (by identifying the specific date between two timestamps). Then, get the cursor corresponding to that timestamp and send it to the frontend where you can play with it. – Peter Malik Dec 28 '21 at 01:00
  • That’s similar to what I’m looking at. I’m going to not create a new query, but instead make the cursors deterministic based on timestamp and an offset (as there could be hundreds of results on a particular date). So I can paginate from the starting point of a cursor derived from the date and possible offset. So jump to date would be a case of requesting the cursor at offset 0 for that date. – james2m Dec 28 '21 at 14:23

0 Answers0