2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Types
|
|
|
|
# rubocop: disable Graphql/AuthorizeTypes
|
|
|
|
class CountableConnectionType < GraphQL::Types::Relay::BaseConnection
|
2021-01-29 00:20:46 +05:30
|
|
|
field :count, GraphQL::INT_TYPE, null: false,
|
2021-03-11 19:13:27 +05:30
|
|
|
description: 'Total count of collection.'
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
def count
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
relation = object.items
|
|
|
|
|
|
|
|
# sometimes relation is an Array
|
|
|
|
relation = relation.reorder(nil) if relation.respond_to?(:reorder)
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
if relation.try(:group_values)&.present?
|
|
|
|
relation.size.keys.size
|
|
|
|
else
|
|
|
|
relation.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|