Skip to content

Commit

Permalink
Move out building filtered scope to mixin Common::Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lpichler committed Jun 3, 2021
1 parent 1eb79ff commit b73a9e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 14 additions & 0 deletions lib/insights/api/common/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ def add_filter(requested_comparator, allowed_comparators, key, value)
end
end

def self.build_filtered_scope(scope, api_version, klass_name, filter)
return scope unless filter

openapi_doc = ::Insights::API::Common::OpenApi::Docs.instance[api_version]
openapi_schema_name, = ::Insights::API::Common::GraphQL::Generator.openapi_schema(openapi_doc, klass_name)

action_parameters = ActionController::Parameters.new(filter)
definitions = openapi_doc.definitions

association_attribute_properties = association_attribute_properties(definitions, action_parameters)

new(scope, action_parameters, definitions[openapi_schema_name], association_attribute_properties).apply
end

def timestamp(k, val)
if val.kind_of?(Hash)
val.each do |comparator, value|
Expand Down
13 changes: 2 additions & 11 deletions lib/insights/api/common/graphql/templates/query_type.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,9 @@ QueryType = ::GraphQL::ObjectType.define do
scope = model_class
end

if args[:filter]
openapi_doc = ::Insights::API::Common::OpenApi::Docs.instance["<%= api_version %>"]
openapi_schema_name, _schema = ::Insights::API::Common::GraphQL::Generator.openapi_schema(openapi_doc, klass_name)
association_attribute_properties =
Insights::API::Common::Filter.association_attribute_properties(openapi_doc.definitions, ActionController::Parameters.new(args[:filter]))
scope = ::Insights::API::Common::Filter.new(
scope,
ActionController::Parameters.new(args[:filter]),
openapi_doc.definitions[openapi_schema_name],
association_attribute_properties).apply
end
scope = ::Insights::API::Common::Filter.build_filtered_scope(scope, "<%= api_version %>", klass_name, args[:filter])
scope = ::Insights::API::Common::GraphQL.search_options(scope, args)

if <%= graphql_options[:use_pagination_v2] %> == true
::Insights::API::Common::PaginatedResponseV2.new(
base_query: scope, request: nil, limit: args[:limit], offset: args[:offset], sort_by: args[:sort_by]
Expand Down

0 comments on commit b73a9e1

Please sign in to comment.