Commit c5dfe519 authored by Alex Kalderimis's avatar Alex Kalderimis

[GQL] Test helpers: support #ready? usage

This ensures that we call the resolver's `#ready?` method during tests
in a way that mimics the framework. This allows resolvers to start using
`#ready?` and continue to make use of our helpers.
parent 691b1b99
......@@ -11,9 +11,19 @@ module GraphqlHelpers
underscored_field_name.to_s.camelize(:lower)
end
# Run a loader's named resolver
# Run a loader's named resolver in a way that closely mimics the framework.
#
# First the `ready?` method is called. If it turns out that the resolver is not
# ready, then the early return is returned instead.
#
# Then the resolve method is called.
def resolve(resolver_class, obj: nil, args: {}, ctx: {}, field: nil)
resolver_class.new(object: obj, context: ctx, field: field).resolve(args)
resolver = resolver_class.new(object: obj, context: ctx, field: field)
ready, early_return = sync_all { resolver.ready?(**args) }
return early_return unless ready
resolver.resolve(args)
end
# Eagerly run a loader's named resolver
......@@ -51,12 +61,12 @@ module GraphqlHelpers
# BatchLoader::GraphQL returns a wrapper, so we need to :sync in order
# to get the actual values
def batch_sync(max_queries: nil, &blk)
wrapper = proc do
lazy_vals = yield
lazy_vals.is_a?(Array) ? lazy_vals.map { |val| sync(val) } : sync(lazy_vals)
batch(max_queries: max_queries) { sync_all(&blk) }
end
batch(max_queries: max_queries, &wrapper)
def sync_all(&blk)
lazy_vals = yield
lazy_vals.is_a?(Array) ? lazy_vals.map { |val| sync(val) } : sync(lazy_vals)
end
def graphql_query_for(name, attributes = {}, fields = nil)
......
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