Sha256: 6b1ba4e2f68464f1899128c654748c2cad61fa398a64e7dcabf930073012c41f

Contents?: true

Size: 1.67 KB

Versions: 10

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true
require "graphql/deprecation"

module GraphQL
  # @api private
  class Filter
    def initialize(only: nil, except: nil, silence_deprecation_warning: false)
      if !silence_deprecation_warning
        line = caller(2, 10).find { |l| !l.include?("lib/graphql") }
        GraphQL::Deprecation.warn("GraphQL::Filter, `only:`, `except:`, and `.merge_filters` are deprecated and will be removed in v2.1.0. Implement `visible?` on your schema members instead (https://graphql-ruby.org/authorization/visibility.html).\n  #{line}")
      end
      @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, silence_deprecation_warning: true)
    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.size
        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

10 entries across 10 versions & 1 rubygems

Version Path
graphql-2.0.32 lib/graphql/filter.rb
graphql-2.0.31 lib/graphql/filter.rb
graphql-2.0.29 lib/graphql/filter.rb
graphql-2.0.28 lib/graphql/filter.rb
graphql-2.0.27 lib/graphql/filter.rb
graphql-2.0.26 lib/graphql/filter.rb
graphql-2.0.25 lib/graphql/filter.rb
graphql-2.0.24 lib/graphql/filter.rb
graphql-2.0.23 lib/graphql/filter.rb
graphql-2.0.22 lib/graphql/filter.rb