insights-api-common 5.0.4 → 5.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27c2d8f13f5a3be044e999f5eede282412bdd00e258b21760f34c154609fa622
4
- data.tar.gz: 46afb339ae074b46703fb268b1bbd8b76f1375ba80196632c8a803f408fcd73e
3
+ metadata.gz: b8a770535b65f1da98fa3196078052c5048bd13dcd4253145aa04afa5d6c1bf9
4
+ data.tar.gz: 13f54d55fe5343eaec0116095014e3782232e836e8e1d610a1e1120ea59b68b8
5
5
  SHA512:
6
- metadata.gz: 3449d374a193f78d3d314156042ef4956a76aebd82ad9fed5481f8932bb103faf19f1f67d9bac0adf61fd4a3570f743f1625a3f37bc095cd6f31499e30117f96
7
- data.tar.gz: 562dc832ad9d50ca6e9f3790ed989cc0a2953e0450eaa11587012e9a96383df4b29491c7cab9b2f650ced30a71ef274d4a46f34e641ac7baf8f0bce7eeb74235
6
+ metadata.gz: d329437271a364dc4d03b31b484400c2ab496b91a17f2e39c80fd57c249a992b7e6764b69724677120fe1de219c315472835809df5001a348fd264bb66a8961c
7
+ data.tar.gz: 93510825434809fde1d60e46b73a8f80f61535d41d77f8ebe2df716e26212d1903a0f9d9911f328bf4e6607c6aaa5a64aea46617ad39a763819f6325cab851d7
@@ -177,6 +177,20 @@ module Insights
177
177
  end
178
178
  end
179
179
 
180
+ def self.build_filtered_scope(scope, api_version, klass_name, filter)
181
+ return scope unless filter
182
+
183
+ openapi_doc = ::Insights::API::Common::OpenApi::Docs.instance[api_version]
184
+ openapi_schema_name, = ::Insights::API::Common::GraphQL::Generator.openapi_schema(openapi_doc, klass_name)
185
+
186
+ action_parameters = ActionController::Parameters.new(filter)
187
+ definitions = openapi_doc.definitions
188
+
189
+ association_attribute_properties = association_attribute_properties(definitions, action_parameters)
190
+
191
+ new(scope, action_parameters, definitions[openapi_schema_name], association_attribute_properties).apply
192
+ end
193
+
180
194
  def timestamp(k, val)
181
195
  if val.kind_of?(Hash)
182
196
  val.each do |comparator, value|
@@ -164,8 +164,16 @@ module Insights
164
164
 
165
165
  graphql_model_type_template = ERB.new(template("model_type"), nil, '<>').result(binding)
166
166
  graphql_namespace.module_eval(graphql_model_type_template)
167
+
168
+ unless graphql_namespace.const_defined?("#{klass_name}AggregateType", false)
169
+ graphql_aggregate_model_type_template = ERB.new(template("aggregate_model_type"), nil, '<>').result(binding)
170
+ graphql_namespace.module_eval(graphql_aggregate_model_type_template)
171
+ end
167
172
  end
168
173
 
174
+ graphql_aggregate_type_template = ERB.new(template("aggregate_type"), nil, '<>').result(binding)
175
+ graphql_namespace.module_eval(graphql_aggregate_type_template)
176
+
169
177
  graphql_query_type_template = ERB.new(template("query_type"), nil, '<>').result(binding)
170
178
  graphql_namespace.module_eval(graphql_query_type_template)
171
179
 
@@ -0,0 +1,8 @@
1
+ <%= klass_name %>AggregateType = ::GraphQL::ObjectType.define do
2
+ name "<%= klass_name %>AggregateType"
3
+ description "A <%= klass_name %>AggregateType to wrap metrics aggregation"
4
+
5
+ field :aggregate, AggregateType do
6
+ resolve ->(object, _args, _ctx) { object }
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ AggregateType = ::GraphQL::ObjectType.define do
2
+ name "AggregateType"
3
+ description "A AggregateType type for aggregation metrics"
4
+
5
+ field :total_count, !types.Int do
6
+ resolve ->(object, _args, _ctx) {
7
+ if object.kind_of?(QueryRelation) # from nested aggregation
8
+ object.count # count is array operation
9
+ else
10
+ object
11
+ end
12
+ }
13
+ end
14
+ end
@@ -30,6 +30,19 @@
30
30
  ::Insights::API::Common::GraphQL::AssociationLoader.new(<%= klass_name.constantize %>, "<%= associations %>", args, graphql_options).load(obj)
31
31
  }
32
32
  end
33
+
34
+ field :<%= associations %>_aggregate do
35
+ description "Aggregation of <%= associations %> associated with <%= klass_name %> model."
36
+ type <%= "#{association_class_name}AggregateType" %>
37
+
38
+ associations_name = "<%= associations %>"
39
+ preload :<%= associations %>
40
+
41
+ resolve lambda { |obj, args, _ctx|
42
+ ::Insights::API::Common::GraphQL::AssociationLoader.new(<%= klass_name.constantize %>, "<%= associations %>", args, graphql_options).load(obj)
43
+ }
44
+ end
45
+
33
46
  <% end %>
34
47
  <% end %>
35
48
  end
@@ -31,18 +31,9 @@ QueryType = ::GraphQL::ObjectType.define do
31
31
  scope = model_class
32
32
  end
33
33
 
34
- if args[:filter]
35
- openapi_doc = ::Insights::API::Common::OpenApi::Docs.instance["<%= api_version %>"]
36
- openapi_schema_name, _schema = ::Insights::API::Common::GraphQL::Generator.openapi_schema(openapi_doc, klass_name)
37
- association_attribute_properties =
38
- Insights::API::Common::Filter.association_attribute_properties(openapi_doc.definitions, ActionController::Parameters.new(args[:filter]))
39
- scope = ::Insights::API::Common::Filter.new(
40
- scope,
41
- ActionController::Parameters.new(args[:filter]),
42
- openapi_doc.definitions[openapi_schema_name],
43
- association_attribute_properties).apply
44
- end
34
+ scope = ::Insights::API::Common::Filter.build_filtered_scope(scope, "<%= api_version %>", klass_name, args[:filter])
45
35
  scope = ::Insights::API::Common::GraphQL.search_options(scope, args)
36
+
46
37
  if <%= graphql_options[:use_pagination_v2] %> == true
47
38
  ::Insights::API::Common::PaginatedResponseV2.new(
48
39
  base_query: scope, request: nil, limit: args[:limit], offset: args[:offset], sort_by: args[:sort_by]
@@ -54,5 +45,24 @@ QueryType = ::GraphQL::ObjectType.define do
54
45
  end
55
46
  }
56
47
  end
48
+
49
+ field_aggregate = "#{collection}_aggregate"
50
+ field field_aggregate do
51
+ description "The #{collection} aggregation associated with this #{klass_name}"
52
+ klass_name_type = "::Insights::API::Common::GraphQL::Api::#{version_namespace}::#{klass_name}AggregateType".constantize
53
+ argument :filter, ::Insights::API::Common::GraphQL::Types::QueryFilter, "The Query Filter for querying the #{collection}"
54
+
55
+ type klass_name_type
56
+
57
+ resolve lambda { |_obj, args, _ctx|
58
+ if base_query.present?
59
+ scope = base_query.call(model_class, args, ctx)
60
+ else
61
+ scope = model_class
62
+ end
63
+
64
+ ::Insights::API::Common::Filter.build_filtered_scope(scope, "<%= api_version %>", klass_name, args[:filter]).count
65
+ }
66
+ end
57
67
  end
58
68
  end
@@ -1,7 +1,7 @@
1
1
  module Insights
2
2
  module API
3
3
  module Common
4
- VERSION = "5.0.4".freeze
4
+ VERSION = "5.0.5".freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insights-api-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.4
4
+ version: 5.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Insights Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-26 00:00:00.000000000 Z
11
+ date: 2021-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: acts_as_tenant
@@ -396,6 +396,8 @@ files:
396
396
  - lib/insights/api/common/graphql/associated_records.rb
397
397
  - lib/insights/api/common/graphql/association_loader.rb
398
398
  - lib/insights/api/common/graphql/generator.rb
399
+ - lib/insights/api/common/graphql/templates/aggregate_model_type.erb
400
+ - lib/insights/api/common/graphql/templates/aggregate_type.erb
399
401
  - lib/insights/api/common/graphql/templates/model_type.erb
400
402
  - lib/insights/api/common/graphql/templates/query_type.erb
401
403
  - lib/insights/api/common/graphql/templates/schema.erb