Sha256: f69ebeebd1c9ee30c0c3c8c694e564f8692e2fe3589506c2efc16b01b48595e9

Contents?: true

Size: 1.97 KB

Versions: 8

Compression:

Stored size: 1.97 KB

Contents

require_relative 'base_comparison_input_type'

module GraphQL
  module Filters
    module InputTypes
      class FieldsComparisonInputType
        include CachedClass

        resolve_cache_miss do |object_type, klass|
          klass.new BaseComparisonInputType do
            graphql_name "#{object_type.graphql_name}ComparisonInput"

            object_type.fields.each_value do |field_object|
              next unless field_object.filter_options[:enabled]

              type = field_object.type
              filter_options = field_object.filter_options

              argument field_object.name,
                       type.comparison_input_type,
                       required: false,
                       prepare: lambda { |field_comparator, _context|
                         lambda { |scope|
                           if scope.klass.attribute_names.include? filter_options[:attribute_name].to_s
                             field_comparator.call scope, filter_options[:attribute_name]
                           else
                             field_comparator.call scope, filter_options[:association_name]
                           end
                         }
                       }
            end

            define_method :prepare do
              lambda do |scope, association_name=nil|
                model_class = object_type.model_class

                nested_query = values.reduce model_class.all do |acc, val|
                  val.call acc
                end

                if association_name.nil?
                  unless scope.structurally_compatible? nested_query
                    raise "the produced nested query on model #{model_class}"\
                          'is not compatible with the provided scope'
                  end

                  scope.and nested_query
                else
                  scope.where association_name => nested_query
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
graphql-filters-1.0.8 lib/graphql/filters/input_types/fields_comparison_input_type.rb
graphql-filters-1.0.7 lib/graphql/filters/input_types/fields_comparison_input_type.rb
graphql-filters-1.0.6 lib/graphql/filters/input_types/fields_comparison_input_type.rb
graphql-filters-1.0.5 lib/graphql/filters/input_types/fields_comparison_input_type.rb
graphql-filters-1.0.4 lib/graphql/filters/input_types/fields_comparison_input_type.rb
graphql-filters-1.0.3 lib/graphql/filters/input_types/fields_comparison_input_type.rb
graphql-filters-1.0.2 lib/graphql/filters/input_types/fields_comparison_input_type.rb
graphql-filters-1.0.1 lib/graphql/filters/input_types/fields_comparison_input_type.rb