Module: ActionPolicy::GraphQL::AuthorizedField

Defined in:
lib/action_policy/graphql/authorized_field.rb

Overview

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

Defined Under Namespace

Classes: AuthorizeExtension, AuthorizeFieldExtension, Extension, PreauthorizeExtension, ScopeExtension

Instance Method Summary collapse

Instance Method Details

#initialize(*args, preauthorize: nil, authorize: nil, authorized_scope: nil, authorize_field: nil, **kwargs, &block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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