Class: Gitlab::Graphql::Loaders::LazyRelationLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/graphql/loaders/lazy_relation_loader.rb,
lib/gitlab/graphql/loaders/lazy_relation_loader/registry.rb,
lib/gitlab/graphql/loaders/lazy_relation_loader/top_n_loader.rb,
lib/gitlab/graphql/loaders/lazy_relation_loader/relation_proxy.rb

Defined Under Namespace

Classes: Registry, RelationProxy, TopNLoader

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_ctx, object, **kwargs) ⇒ LazyRelationLoader

Returns a new instance of LazyRelationLoader.



17
18
19
20
21
22
23
24
# File 'lib/gitlab/graphql/loaders/lazy_relation_loader.rb', line 17

def initialize(query_ctx, object, **kwargs)
  @query_ctx = query_ctx
  @object = object
  @kwargs = kwargs

  query_ctx[loader_cache_key] ||= Registry.new(relation(**kwargs))
  query_ctx[loader_cache_key].register(object)
end

Class Attribute Details

.associationObject

Returns the value of attribute association.



8
9
10
# File 'lib/gitlab/graphql/loaders/lazy_relation_loader.rb', line 8

def association
  @association
end

.modelObject

Returns the value of attribute model.



8
9
10
# File 'lib/gitlab/graphql/loaders/lazy_relation_loader.rb', line 8

def model
  @model
end

Class Method Details

.inherited(klass) ⇒ Object

Automatically register the inheriting classes to GitlabSchema as lazy objects.



12
13
14
# File 'lib/gitlab/graphql/loaders/lazy_relation_loader.rb', line 12

def inherited(klass)
  GitlabSchema.lazy_resolve(klass, :load)
end

Instance Method Details

#loadObject

Returns an instance of ‘RelationProxy` for the object (parent model). The returned object behaves like an Active Record relation to support keyset pagination.



29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/graphql/loaders/lazy_relation_loader.rb', line 29

def load
  case reflection.macro
  when :has_many
    relation_proxy
  when :has_one
    relation_proxy.last
  else
    raise 'Not supported association type!'
  end
end