Class: GraphitiGraphQL::Federation::Loaders::HasMany
- Inherits:
-
GraphQL::Batch::Loader
- Object
- GraphQL::Batch::Loader
- GraphitiGraphQL::Federation::Loaders::HasMany
- Defined in:
- lib/graphiti_graphql/federation/loaders/has_many.rb
Instance Method Summary collapse
-
#initialize(federated_relationship, params) ⇒ HasMany
constructor
A new instance of HasMany.
- #perform(ids) ⇒ Object
Constructor Details
#initialize(federated_relationship, params) ⇒ HasMany
Returns a new instance of HasMany.
5 6 7 8 9 10 |
# File 'lib/graphiti_graphql/federation/loaders/has_many.rb', line 5 def initialize(federated_relationship, params) @federated_relationship = federated_relationship @resource_class = federated_relationship.local_resource_class @params = params @foreign_key = federated_relationship.foreign_key end |
Instance Method Details
#perform(ids) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/graphiti_graphql/federation/loaders/has_many.rb', line 12 def perform(ids) @params[:filter] ||= {} @params[:filter][@foreign_key] = {eq: ids.join(",")} @federated_relationship.params_block&.call(@params) if (first = @params.delete(:first)) @params[:page] ||= {} @params[:page][:size] = first end if (after = @params.delete(:after)) @params[:page] ||= {} @params[:page][:after] = after end if (before = @params.delete(:before)) @params[:page] ||= {} @params[:page][:before] = before end if ids.length > 1 && @params[:page] raise Graphiti::Errors::UnsupportedPagination elsif !@params[:page] @params[:page] = {size: 999} end Util.with_gql_context do records = @resource_class.all(@params).as_json[:data] map = records.group_by { |record| record[@foreign_key].to_s} ids.each { |id| fulfill(id, (map[id] || [])) } end end |