Sha256: a7a71597e8be24b4f3cf6e64b7401d975b5f6281256e79c2d3efca5e98830542

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require 'graphql-pundit/instrumenter'
require 'graphql-pundit/version'

require 'graphql'

# Defines authorization related helpers
module GraphQL
  # Defines `authorize` and `authorize!` helpers
  class AuthorizationHelper
    attr_reader :raise_unauthorized

    def initialize(raise_unauthorized)
      @raise_unauthorized = raise_unauthorized
    end

    def call(defn, query = nil, policy: nil, record: nil)
      opts = {record: record,
              query: query || defn.name,
              policy: policy,
              raise: raise_unauthorized}
      if query.respond_to?(:call)
        opts = {proc: query, raise: raise_unauthorized}
      end
      Define::InstanceDefinable::AssignMetadataKey.new(:authorize).
        call(defn, opts)
    end
  end

  # Defines `scope` helper
  class ScopeHelper
    def call(defn, proc = :infer_scope)
      Define::InstanceDefinable::AssignMetadataKey.new(:scope).
        call(defn, proc)
    end
  end

  Field.accepts_definitions(authorize: AuthorizationHelper.new(false),
                            authorize!: AuthorizationHelper.new(true),
                            scope: ScopeHelper.new)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-pundit-0.5.1 lib/graphql-pundit.rb
graphql-pundit-0.5.0 lib/graphql-pundit.rb