Class: HQ::GraphQL::Field

Inherits:
GraphQL::Schema::Field
  • Object
show all
Defined in:
lib/hq/graphql/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, authorize_action: :read, authorize: nil, klass: nil, **options, &block) ⇒ Field

Returns a new instance of Field.



8
9
10
11
12
13
# File 'lib/hq/graphql/field.rb', line 8

def initialize(*args, authorize_action: :read, authorize: nil, klass: nil, **options, &block)
  super(*args, **options, &block)
  @authorize_action = authorize_action
  @authorize = authorize
  @klass_or_string = klass
end

Instance Attribute Details

#authorizeObject (readonly)

Returns the value of attribute authorize.



6
7
8
# File 'lib/hq/graphql/field.rb', line 6

def authorize
  @authorize
end

#authorize_actionObject (readonly)

Returns the value of attribute authorize_action.



6
7
8
# File 'lib/hq/graphql/field.rb', line 6

def authorize_action
  @authorize_action
end

Instance Method Details

#authorized?(object, _args, ctx) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/hq/graphql/field.rb', line 23

def authorized?(object, _args, ctx)
  super &&
    (!authorize || authorize.call(object, ctx)) &&
    ::HQ::GraphQL.authorize_field(authorize_action, self, object, ctx)
end

#klassObject



29
30
31
# File 'lib/hq/graphql/field.rb', line 29

def klass
  @klass ||= @klass_or_string.is_a?(String) ? @klass_or_string.constantize : @klass_or_string
end

#scope(&block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/hq/graphql/field.rb', line 15

def scope(&block)
  if block
    @scope = block
  else
    @scope
  end
end