2

I'm trying to find out how to use arguments on nested items in my query using Absinthe.

What I try to accomplish is this:

{
  users(order: ASC) {
    id
    email
    clients(order: DESC) {
      id
      email
    }
  }
}

Here's my Schema and Type:

  query do
    @desc "Get all users"
    field :users, list_of(:user) do
      arg :order, type: :sort_order, default_value: :asc
      resolve &Resolvers.users/2
    end
  end

  @desc "A user"
  object :user do
    field :id, :id
    field :email, :string
    field :clients, list_of(:user)
  end  

And the resolver:

  def users(_, args, _) do
    args
    |> Enum.reduce(User, fn
      {:order, order}, query ->
        query |> order_by({^order, :email})
    end)
    |> Repo.all |> Repo.preload([:clients])
  end

So my question is how and where should I place the sorting argument for the clients? With the above example I get an error:

"message": "Unknown argument \"order\" on field \"clients\" of type \"User\"."
1
  • This Q & A might have something to help you. Commented Oct 10, 2017 at 14:45

1 Answer 1

5

Put the argument under the clients field:

object :user do
  field :id, :id
  field :email, :string
  field :clients, list_of(:user) do
    arg :order, type: :sort_order, default_value: :asc
    resolve &Resolvers.clients/2
  end
end 

Then write a resolver to handle the sorting:

def clients(user, %{order: clients_order}, _) do
  sorted_clients = user.clients # TODO sort those clients into the desired order
  {:ok, sorted_clients}
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.