Class: Graphiti::Scope

Inherits:
Object show all
Defined in:
lib/graphiti/scope.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, resource, query, opts = {}) ⇒ Scope

Returns a new instance of Scope.



34
35
36
37
38
39
40
41
42
43
# File 'lib/graphiti/scope.rb', line 34

def initialize(object, resource, query, opts = {})
  @object = object
  @resource = resource
  @query = query
  @opts = opts

  @object = @resource.around_scoping(@object, @query.hash) { |scope|
    apply_scoping(scope, opts)
  }
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



3
4
5
# File 'lib/graphiti/scope.rb', line 3

def object
  @object
end

#paginationObject (readonly)

Returns the value of attribute pagination.



4
5
6
# File 'lib/graphiti/scope.rb', line 4

def pagination
  @pagination
end

#unpaginated_objectObject

Returns the value of attribute unpaginated_object.



3
4
5
# File 'lib/graphiti/scope.rb', line 3

def unpaginated_object
  @unpaginated_object
end

Class Method Details

.global_thread_pool_executorObject



24
25
26
# File 'lib/graphiti/scope.rb', line 24

def self.global_thread_pool_executor
  GLOBAL_THREAD_POOL_EXECUTOR.value!
end

.global_thread_pool_statsObject



28
29
30
31
32
# File 'lib/graphiti/scope.rb', line 28

def self.global_thread_pool_stats
  GLOBAL_THREAD_POOL_EXECUTOR_BROADCAST_STATS.each_with_object({}) do |key, memo|
    memo[key] = global_thread_pool_executor.send(key)
  end
end

Instance Method Details

#cache_keyObject



79
80
81
82
83
84
85
86
87
# File 'lib/graphiti/scope.rb', line 79

def cache_key
  # This is the combined cache key for the base query and the query for all sideloads
  # Changing the query will yield a different cache key

  cache_keys = sideload_resource_proxies.map { |proxy| proxy.try(:cache_key) }

  cache_keys << @object.try(:cache_key) # this is what calls into the ORM (ActiveRecord, most likely)
  ActiveSupport::Cache.expand_cache_key(cache_keys.flatten.compact)
end

#cache_key_with_versionObject



89
90
91
92
93
94
95
96
97
# File 'lib/graphiti/scope.rb', line 89

def cache_key_with_version
  # This is the combined and versioned cache key for the base query and the query for all sideloads
  # If any returned model's updated_at changes, this key will change

  cache_keys = sideload_resource_proxies.map { |proxy| proxy.try(:cache_key_with_version) }

  cache_keys << @object.try(:cache_key_with_version) # this is what calls into ORM (ActiveRecord, most likely)
  ActiveSupport::Cache.expand_cache_key(cache_keys.flatten.compact)
end

#future_resolve {|resolved| ... } ⇒ Object

Yields:

  • (resolved)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphiti/scope.rb', line 53

def future_resolve
  return Concurrent::Promises.fulfilled_future([], self.class.global_thread_pool_executor) if @query.zero_results?

  resolved = broadcast_data { |payload|
    @object = @resource.before_resolve(@object, @query)
    payload[:results] = @resource.resolve(@object)
    payload[:results]
  }
  resolved.compact!
  assign_serializer(resolved)
  yield resolved if block_given?
  @opts[:after_resolve]&.call(resolved)
  sideloaded = @query.parents.any?
  close_adapter = Graphiti.config.concurrency && sideloaded
  if close_adapter
    @resource.adapter.close
  end

  future_resolve_sideloads(resolved)
    .then_on(self.class.global_thread_pool_executor, resolved) { resolved }
end

#parent_resourceObject



75
76
77
# File 'lib/graphiti/scope.rb', line 75

def parent_resource
  @resource
end

#resolveObject



45
46
47
# File 'lib/graphiti/scope.rb', line 45

def resolve
  future_resolve.value!
end

#resolve_sideloads(results) ⇒ Object



49
50
51
# File 'lib/graphiti/scope.rb', line 49

def resolve_sideloads(results)
  future_resolve_sideloads(results).value!
end

#updated_atObject Also known as: last_modified_at



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/graphiti/scope.rb', line 99

def updated_at
  updated_time = nil
  begin
    updated_ats = sideload_resource_proxies.map(&:updated_at)
    updated_ats << @object.maximum(:updated_at)
    updated_time = updated_ats.compact.max
  rescue => e
    Graphiti.log(["error calculating last_modified_at for #{@resource.class}", :red])
    Graphiti.log(e)
  end

  updated_time || Time.now
end