james2m

james2m

How to jump to a specific date in a cursor based relay paginated connection?

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! :smiley:

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement