Extract QueryRoot resolve functions into its own classes

This commit is contained in:
Alberto Miedes Garcés
2017-01-07 22:43:42 +01:00
parent 3c73f35416
commit d76e4518e6
3 changed files with 36 additions and 14 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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