Flop.Relay.edges_from_result

You're seeing just the function edges_from_result, go back to Flop.Relay module for more information.
Link to this function

edges_from_result(arg, opts \\ [])

View Source (since 0.8.0)

Specs

edges_from_result({[{any(), any()}] | [any()], Flop.Meta.t()}, [Flop.option()]) ::
  [edge()]

Turns a list of query results into Relay edges.

Simple queries

If your query returns a list of maps or structs, the function will return the a list of edges with :cursor and :node as only fields.

iex> flop = %Flop{order_by: [:name]}
iex> meta = %Flop.Meta{flop: flop}
iex> result = {[%Flop.Fruit{name: "Apple", family: "Rosaceae"}], meta}
iex> Flop.Relay.edges_from_result(result)
[
  %{
    cursor: "g3QAAAABZAAEbmFtZW0AAAAFQXBwbGU=",
    node: %Flop.Fruit{name: "Apple", family: "Rosaceae"}
  }
]

Supplying additional edge information

If the query result is a list of 2-tuples, this is interpreted as a tuple of the node information and the edge information. For example, if you have a query like this:

Group
|> where([g], g.id == ^group_id)
|> join(:left, [g], m in assoc(g, :members))
|> select([g, m], {m, map(m, [:role])})

Then your query result looks something like:

[{%Member{id: 242, name: "Carl"}, %{role: :owner}}]

In this case, the members are the nodes, and the maps with the roles is seen as edge information.

[
  %{
    cursor: "AE98RNSTNGN",
    node: %Member{id: 242, name: "Carl"},
    role: :owner
  }
]

Note that in this case, the whole tuple will be passed to the cursor value function, so that the cursor can be based on both node and edge fields.

Here's an example with fruit that overrides the cursor value function:

iex> flop = %Flop{order_by: [:name]}
iex> meta = %Flop.Meta{flop: flop}
iex> items = [{%Flop.Fruit{name: "Apple"}, %{preparation:  :grated}}]
iex> func = fn {fruit, _edge}, order_by -> Map.take(fruit, order_by) end
iex> Flop.Relay.edges_from_result(
...>   {items, meta},
...>   get_cursor_value_func: func
...> )
[
  %{
    cursor: "g3QAAAABZAAEbmFtZW0AAAAFQXBwbGU=",
    node: %Flop.Fruit{name: "Apple"},
    preparation: :grated
  }
]

Options

  • :get_cursor_value_func: 2-arity function that takes an item from the query result and the order_by fields and returns the unencoded cursor value.