How to create a Relay edge?

Say my schema defines a todo object and a todos Relay connection. Right now there are methods like Absinthe.Relay.Connection.from_query to help create resolve that connection. And it works great.

My question is if I have a Relay mutation payload field :add_todo which returns a todo edge, is there any helper method for building that edge? Or we can do this by hand?

1 Like

It seems strange to answer a create mutation with a relay edge instead of a todo item.

Could You show your resolver?

Thanks for the reply. But I don’t see why it’s strange. :smile: The official relay example actually does this. That seems the only way to add to a connection?

I actually figured out a way, assuming the new todo is added to the end:

def some_resolver(_, args, _) do
  todo = SomeContext.create_todo(args)
  count = Repo.aggregate Todo, :count, :id 
  offset = count - 1
  cursor = Absinthe.Relay.Connection.offset_to_cursor(offset)

  {:ok, %{todoEdge: %{cursor: cursor, todo: todo}}
1 Like