x4K3PqNH

x4K3PqNH

Reuse Absinthe.Relay connection

Hi guys,

I have a GraphQL API via Absinthe where the relay/pagination part was implemented manually. Want to switch now to Absinthe.Relay and can’t figure out how to reuse a connection.

In a direct approach I define a connection into a file, the file is imported in the schema via import_types/1. Further I import the connection into every object it’s needed via import_fields/1 and everything works fine. If I switch to Absinthe.Relay and use the same steps, the connection is not available under the field, but I don’t get any compile warnings/errors.
I did put the connection inside an object so I have a name to import in import_fields/1, but without the object what name/atom should I use in the import_fields/1 to import the connection directly, if that’s even possible ?

Here’s what I think are the relevant parts in the current implementation (stripped to the bare minimum):

defmodule Api.Users.Types do
  use Absinthe.Schema.Notation

  object :group do
    interface(:node)
    ...
  end


  object :user_edge do
    interface(:edge)

    field(:cursor, non_null(:string))
    field(:node, :user)
  end


  object :user_connection do
    interface(:connection)

    field(:edges, list_of(:user_edge))
    field(:nodes, list_of(:user))

  end


  object :user_edge_connection do
    field(:users, non_null(:user_connection)) do

      ... arg(...) & resolve(...)

    end
  end
end



defmodule Api.Schema do
  use Absinthe.Schema
  
  import_types(Api.Users.Types)
  import_types(Api.Groups.Types)
  import_types(Api.Organization.Types)
  
  query(...)
end



defmodule Api.Groups.Types do
  use Absinthe.Schema.Notation

  object :group do
    ...
    import_fields(:user_edge_connection)
  end
end


defmodule Api.Organization.Types do
  use Absinthe.Schema.Notation

  object :organization do
    ...
    import_fields(:user_edge_connection)
  end
end

And here is the Absinthe.Relay way:

defmodule Api.Users.Types do
    use Absinthe.Schema.Notation                                                                                                                      
    use Absinthe.Relay.Schema.Notation, :modern 
    
    node object :group do 
      ...
    end
    
    
    connection node_type: :group
   
    object :user_edge_connection do
      connection field :user_connection, node_type: :user do     
        ... arg(...) & resolve(...)
      end
    end
end


defmodule Api.Schema do
  use Absinthe.Schema
  
  import_types(Api.Users.Types)
  import_types(Api.Groups.Types)
  import_types(Api.Organization.Types)
  
  query(...)
end


defmodule Api.Groups.Types do
  use Absinthe.Schema.Notation

  object :group do
    ...
    import_fields(:user_edge_connection)
  end
end


defmodule Api.Organization.Types do
  use Absinthe.Schema.Notation

  object :organization do
    ...
    import_fields(:user_edge_connection)
  end
end

  

We have connections that are reused in many parts, and if we need to define the connection and it’s arguments over and over again, in the long run it’s a high change they will be inconsistent.

Unfortunately did not found anything in the docs or any other resources online pointing me in the right direction here.

Thanks

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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

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
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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement