Sha256: af5a71b928b79a70cf8b95ccf834b4aba95749312498a4e8b91ce81776695f30

Contents?: true

Size: 964 Bytes

Versions: 2

Compression:

Stored size: 964 Bytes

Contents

# frozen_string_literal: true

module GraphQL
  class Field
    NoGuardError = Class.new(StandardError)

    def guard(*args)
      raise NoGuardError.new("Get your field by calling: Type.field_with_guard('#{name}')") unless @__guard_type
      guard_proc = @__guard_object.guard_proc(@__guard_type, self)
      raise NoGuardError.new("Guard lambda does not exist for #{@__guard_type}.#{name}") unless guard_proc

      guard_proc.call(*args)
    end

    def __policy_object=(policy_object)
      @__policy_object = policy_object
      @__guard_object = GraphQL::Guard.new(policy_object: policy_object)
    end

    def __guard_type=(guard_type)
      @__guard_type = guard_type
    end
  end

  class ObjectType
    def field_with_guard(field_name, policy_object = nil)
      field = get_field(field_name)
      return unless field

      field.clone.tap do |f|
        f.__policy_object = policy_object
        f.__guard_type = self
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-guard-1.1.0 lib/graphql/guard/testing.rb
graphql-guard-1.0.0 lib/graphql/guard/testing.rb