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
  @class_name = 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, ctx) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#klassObject



48
49
50
# File 'lib/hq/graphql/field.rb', line 48

def klass
  @klass ||= @class_name&.constantize
end

#resolve_field(object, args, ctx) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hq/graphql/field.rb', line 21

def resolve_field(object, args, ctx)
  if klass.present? && !!::GraphQL::Batch::Executor.current && object.object
    loader =
      if ::HQ::GraphQL.use_experimental_associations?
        limit       = args[:limit]
        offset      = args[:offset]
        sort_by     = args[:sortBy]
        sort_order  = args[:sortOrder]

        PaginatedAssociationLoader.for(
          klass,
          original_name,
          limit: limit,
          offset: offset,
          sort_by: sort_by,
          sort_order: sort_order
        )
      else
        AssociationLoader.for(klass, original_name)
      end

    loader.load(object.object)
  else
    super
  end
end