Extract QueryRoot resolve functions into its own classes
This commit is contained in:
@@ -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
|
||||
|
||||
17
lib/graph_ql/root_collection_resolver.rb
Normal file
17
lib/graph_ql/root_collection_resolver.rb
Normal 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
|
||||
17
lib/graph_ql/root_element_resolver.rb
Normal file
17
lib/graph_ql/root_element_resolver.rb
Normal 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
|
||||
Reference in New Issue
Block a user