Last active
April 27, 2020 17:16
-
-
Save msroot/f1b764e6eb12106053d93603f3d1c37f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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