Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers for working with connections #165

Open
frekw opened this issue Aug 14, 2020 · 0 comments
Open

Helpers for working with connections #165

frekw opened this issue Aug 14, 2020 · 0 comments

Comments

@frekw
Copy link
Contributor

frekw commented Aug 14, 2020

Hey!

These are some helpers that I often end up needing when working with Relay connections

defmodule GraphQL.Connection do
  import Absinthe.Resolution, only: [project: 1]

  def fields(info) do
    edge = project(info) |> Enum.find(fn f -> f.name == "edges" end)
    node = ((edge && edge.selections) || []) |> Enum.find(fn f -> f.name == "node" end)

    ((node && node.selections) || [])
    |> Enum.map(fn f -> String.to_atom(f.name) end)
    |> MapSet.new()
  end

  def map(%{edges: edges} = _conn, f) do
    Enum.map(edges, fn %{node: node} -> f.(node) end)
  end

  def update(%{edges: edges} = conn, f) do
    edges =
      Enum.map(edges, fn edge ->
        %{edge | node: f.(edge.node)}
      end)

    %{conn | edges: edges}
  end
end

would it make sense to integrate some additional helpers to make it easier to work with connections into absinthe_relay?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant