lib/graphiti_graphql/runner.rb in graphiti_graphql-0.1.2 vs lib/graphiti_graphql/runner.rb in graphiti_graphql-0.1.3
- old
+ new
@@ -36,11 +36,11 @@
end
end
def render(json, selection_name)
payload = if find_one?(selection_name)
- {selection_name.to_sym => json.values[0][0]}
+ {selection_name.to_sym => json.values[0][:nodes][0]}
else
json
end
{data: payload}
end
@@ -106,19 +106,23 @@
if !pbt
resource = schema_resource_for_selection(selection, parent_resource)
gather_filters(params, selection, variables_hash, chained_name)
gather_sorts(params, selection, variables_hash, chained_name)
gather_pages(params, selection, variables_hash, chained_name)
+ gather_stats(params, selection, variables_hash, chained_name)
end
params[:include] ||= []
params[:include] << chained_name if chained_name
- fragments = selection.selections.select { |s|
+ nodes = selection.selections.find { |s| s.respond_to?(:name) && s.name == "nodes" }
+ children = nodes ? nodes.children : selection.selections
+
+ fragments = children.select { |s|
s.is_a?(GraphQL::Language::Nodes::InlineFragment)
}
- non_fragments = selection.selections - fragments
+ non_fragments = children - fragments
if pbt
# Only id/_type possible here
fields, extra_fields, sideload_selections = [], [], []
fields = non_fragments.map { |s| s.name.underscore }
@@ -221,11 +225,13 @@
field_name = attr_arg.name.underscore
filter_param_name = [chained_name, field_name].compact.join(".")
attr_arg.value.arguments.each do |operator_arg|
value = operator_arg.value
- if value.respond_to?(:name) # is a variable
+ if value.is_a?(GraphQL::Language::Nodes::Enum)
+ value = value.name
+ elsif value.respond_to?(:name) # is a variable
value = variable_hash[operator_arg.value.name]
end
f[filter_param_name] = {operator_arg.name.underscore => value}
end
end
@@ -295,9 +301,26 @@
end
if pages.present?
params[:page] ||= {}
params[:page].merge!(pages)
+ end
+ end
+
+ def gather_stats(params, selection, variable_hash, chained_name = nil)
+ stats = selection.children.find { |c| c.name == "stats" }
+ nodes = selection.children.find { |c| c.name == "nodes" }
+
+ if stats
+ stat_param = {}
+ stats.children.each do |stat_node|
+ stat_name = stat_node.name
+ calculations = stat_node.children.map(&:name)
+ stat_param[stat_name.to_sym] = calculations.join(",")
+ end
+ stat_param = {chained_name => stat_param} if chained_name
+ params[:stats] = stat_param
+ params[:page] = {size: 0} unless nodes
end
end
end
end