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

pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement