diff --git a/config/initializers/graphql.rb b/config/initializers/graphql.rb index 2d257285d..b385f1483 100644 --- a/config/initializers/graphql.rb +++ b/config/initializers/graphql.rb @@ -55,26 +55,14 @@ QueryRoot = GraphQL::ObjectType.define do type created_type description "Find one #{model.model_name.human} by ID" argument :id, !types.ID - resolve -> (object, arguments, context) do - if model.respond_to?(:public_for_api) - model.public_for_api.find(arguments["id"]) - else - model.find(arguments["id"]) - end - end + resolve GraphQL::RootElementResolver.new(model) end end # create an entry filed to retrive a paginated collection connection model.name.underscore.pluralize.to_sym, created_type.connection_type do description "Find all #{model.model_name.human.pluralize}" - resolve -> (object, arguments, context) do - if model.respond_to?(:public_for_api) - model.public_for_api - else - model.all - end - end + resolve GraphQL::RootCollectionResolver.new(model) end end diff --git a/lib/graph_ql/root_collection_resolver.rb b/lib/graph_ql/root_collection_resolver.rb new file mode 100644 index 000000000..fa0e3c9ec --- /dev/null +++ b/lib/graph_ql/root_collection_resolver.rb @@ -0,0 +1,17 @@ +module GraphQL + class RootCollectionResolver + attr_reader :target_model + + def initialize(target_model) + @target_model = target_model + end + + def call(object, arguments, context) + if target_model.respond_to?(:public_for_api) + target_model.public_for_api + else + target_model.all + end + end + end +end diff --git a/lib/graph_ql/root_element_resolver.rb b/lib/graph_ql/root_element_resolver.rb new file mode 100644 index 000000000..05d0b9bfb --- /dev/null +++ b/lib/graph_ql/root_element_resolver.rb @@ -0,0 +1,17 @@ +module GraphQL + class RootElementResolver + attr_reader :target_model + + def initialize(target_model) + @target_model = target_model + end + + def call(object, arguments, context) + if target_model.respond_to?(:public_for_api) + target_model.public_for_api.find(arguments["id"]) + else + target_model.find(arguments["id"]) + end + end + end +end