module ActionPolicy::GraphQL::AuthorizedField

Add `authorized` option to the field

Example:

class PostType < ::GraphQL::Schema::Object
  field :comments, null: false, authorized: true

  # or with options
  field :comments, null: false, authorized: { type: :relation, with: MyPostPolicy }
end

Public Class Methods

new(*args, preauthorize: nil, authorize: nil, authorized_scope: nil, authorize_field: nil, **kwargs, &block) click to toggle source
Calls superclass method
# File lib/action_policy/graphql/authorized_field.rb, line 125
def initialize(*args, preauthorize: nil, authorize: nil, authorized_scope: nil, authorize_field: nil, **kwargs, &block)
  if authorize && authorized_scope
    raise ArgumentError, "Only one of `authorize` and `authorized_scope` " \
                         "options could be specified. You can use `preauthorize` or `authorize_field` along with scoping"
  end

  if !!authorize == !!preauthorize ? authorize : authorize_field
    raise ArgumentError, "Only one of `authorize`, `preauthorize` or `authorize_field` " \
                         "options could be specified."
  end

  extensions = (kwargs[:extensions] ||= [])

  add_extension! extensions, AuthorizeExtension, authorize
  add_extension! extensions, ScopeExtension, authorized_scope
  add_extension! extensions, PreauthorizeExtension, preauthorize
  add_extension! extensions, AuthorizeFieldExtension, authorize_field

  super(*args, **kwargs, &block)
end

Private Instance Methods

add_extension!(extensions, extension_class, options) click to toggle source
# File lib/action_policy/graphql/authorized_field.rb, line 148
def add_extension!(extensions, extension_class, options)
  return unless options

  options = {} if options == true

  extension = {extension_class => options}

  if extensions.is_a?(Hash)
    extensions.merge!(extension)
  else
    extensions << extension
  end
end