Skip to content

Instantly share code, notes, and snippets.

@msroot
Last active April 27, 2020 17:16
Show Gist options
  • Save msroot/f1b764e6eb12106053d93603f3d1c37f to your computer and use it in GitHub Desktop.
Save msroot/f1b764e6eb12106053d93603f3d1c37f to your computer and use it in GitHub Desktop.
require 'search_object'
require 'search_object/plugin/graphql'
module Resolvers
class BaseSearchResolver
include ::SearchObject.module(:graphql)
ORDER_BY_DIRECTION_INPUT = begin
Class.new(Types::BaseEnum) do
graphql_name "OrderByDirection"
%w(desc asc).map { |e|
value e.upcase, value: e
}
end
end
scope { [] }
def self.add_order_by_for(klass)
input = create_order_by_input(klass)
option(:order_by, type: input) { |scope, value|
if value
value = value.to_h
scope.order("#{value[:field]} #{value[:direction]}")
end
}
end
private
def self.create_order_by_input klass
fields = Class.new(Types::BaseEnum) do
graphql_name "#{klass}OrderByField"
klass.attribute_names.map { |e|
value e.upcase, value: e
}
end
input = Class.new(Types::BaseInputObject) do
argument :field, fields, required: true
argument :direction, ORDER_BY_DIRECTION_INPUT, required: true
graphql_name "#{klass}OrderByInput"
end
input
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment