I have a rails app and I use gem GraphQL. The app contain this type in:
app/graphql/types/category_type.rb
module Types
  class CategoryType < Types::BaseObject
    field :id, ID, null: false
    field :name, String, null: false
    field :weight, Integer, null: false
    field :parent_id, Integer
    field :categories, [Types::CategoryType]
    field :shoes, [Types::ShoeType]
    def categories
      object.categories.all
    end
    def shoes()
        object.shoes.all
    end
  end
end
and this query in
app/graphql/types/query_type.rb
field :category, CategoryType, "Find shoes by category" do
      argument :name, String
      argument :order_by, String
    end
    def category(name:, order_by:)
      @category = Category.find_by(name: name)
      if order_by == "name"
          @category.shoes = @category.shoes.order(name: :asc)
      elsif order_by == "popularity"
          @category.shoes = @category.shoes.order(popularity: :asc)
      end
      @category
    end
This line of code: @category.shoes = @category.shoes.order(name: :asc) does absolutely nothing.
How can I query one category and have all the shoes belonging to that category ordered by my custom order option?
I have tried to do this in
app/graphql/types/category_type.rb
field :shoes, [ShoeType], null: true do
      argument :order_by, String, required: false
    end
...
def shoes(order_by: nil)
      if order_by == "popularity"
        object.shoes.all.order(order: :asc)
      elsif order_by == "name"
        object.shoes.all.order(name: :asc)
      else
        object.shoes.all
      end
    end
app/graphql/types/query_type.rb
field :category, CategoryType, "Find shoes by category" do
      argument :name, String
    end
    def category(name:)
      @category = Category.find_by(name: name)
      @category
    end
But when I query with this:
query { category (name: "Casual") {name shoes(order_by: "popularity"){name} } }
I get this error:
"message": "Field 'shoes' doesn't accept argument 'order_by'"
 
                        
Ok, I found the solution! In reality what I was trying to do in the second part of the question was right, I have an error in my query, instead of writing:
I should write: