Commit e5c9a1a6 authored by Alex Kalderimis's avatar Alex Kalderimis

General resolver and testing improvements

* Add current_user method to base resolver

  This is used often enough to be of general utility

* Allow resolving lazy values when calling resolvers

  Have distinct resolve and eager_resolve methods, so that tests can
  decide if they want laziness handled transparently or not.
parent 7dc1dfeb
...@@ -58,5 +58,9 @@ module Resolvers ...@@ -58,5 +58,9 @@ module Resolvers
def single? def single?
false false
end end
def current_user
context[:current_user]
end
end end
end end
...@@ -16,6 +16,20 @@ module GraphqlHelpers ...@@ -16,6 +16,20 @@ module GraphqlHelpers
resolver_class.new(object: obj, context: ctx).resolve(args) resolver_class.new(object: obj, context: ctx).resolve(args)
end end
# Eagerly run a loader's named resolver
# (syncs any lazy values returned by resolve)
def eager_resolve(resolver_class, **opts)
sync(resolve(resolver_class, **opts))
end
def sync(value)
if GitlabSchema.lazy?(value)
GitlabSchema.sync_lazy(value)
else
value
end
end
# Runs a block inside a BatchLoader::Executor wrapper # Runs a block inside a BatchLoader::Executor wrapper
def batch(max_queries: nil, &blk) def batch(max_queries: nil, &blk)
wrapper = proc do wrapper = proc do
...@@ -39,7 +53,7 @@ module GraphqlHelpers ...@@ -39,7 +53,7 @@ module GraphqlHelpers
def batch_sync(max_queries: nil, &blk) def batch_sync(max_queries: nil, &blk)
wrapper = proc do wrapper = proc do
lazy_vals = yield lazy_vals = yield
lazy_vals.is_a?(Array) ? lazy_vals.map(&:sync) : lazy_vals&.sync lazy_vals.is_a?(Array) ? lazy_vals.map { |val| sync(val) } : sync(lazy_vals)
end end
batch(max_queries: max_queries, &wrapper) batch(max_queries: max_queries, &wrapper)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment