Sha256: b8c545b75c9ce6c263b8dd557542ec9ddf220d38c05e2e76c0111cab47993fdb

Contents?: true

Size: 1.19 KB

Versions: 80

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true
module GraphQL
  # @api private
  class Filter
    def initialize(only: nil, except: nil)
      @only = only
      @except = except
    end

    # Returns true if `member, ctx` passes this filter
    def call(member, ctx)
      (@only ? @only.call(member, ctx) : true) &&
      (@except ? !@except.call(member, ctx) : true)
    end

    def merge(only: nil, except: nil)
      onlies = [self].concat(Array(only))
      merged_only = MergedOnly.build(onlies)
      merged_except = MergedExcept.build(Array(except))
      self.class.new(only: merged_only, except: merged_except)
    end

    private

    class MergedOnly
      def initialize(first, second)
        @first = first
        @second = second
      end

      def call(member, ctx)
        @first.call(member, ctx) && @second.call(member, ctx)
      end

      def self.build(onlies)
        case onlies
        when 0
          nil
        when 1
          onlies[0]
        else
          onlies.reduce { |memo, only| self.new(memo, only) }
        end
      end
    end

    class MergedExcept < MergedOnly
      def call(member, ctx)
        @first.call(member, ctx) || @second.call(member, ctx)
      end
    end
  end
end

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/filter.rb
graphql-1.9.21 lib/graphql/filter.rb
graphql-1.9.20 lib/graphql/filter.rb
graphql-1.9.19 lib/graphql/filter.rb
graphql-1.9.18 lib/graphql/filter.rb
graphql-1.9.17 lib/graphql/filter.rb
graphql-1.9.16 lib/graphql/filter.rb
graphql-1.9.15 lib/graphql/filter.rb
graphql-1.9.14 lib/graphql/filter.rb
graphql-1.9.13 lib/graphql/filter.rb
graphql-1.9.12 lib/graphql/filter.rb
graphql-1.9.11 lib/graphql/filter.rb
graphql-1.9.10 lib/graphql/filter.rb
graphql-1.9.9 lib/graphql/filter.rb
graphql-1.9.8 lib/graphql/filter.rb
graphql-1.9.7 lib/graphql/filter.rb
graphql-1.9.6 lib/graphql/filter.rb
graphql-1.9.5 lib/graphql/filter.rb
graphql-1.9.4 lib/graphql/filter.rb
graphql-1.9.3 lib/graphql/filter.rb