diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-23 00:08:53 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-23 00:08:53 +0000 |
commit | d65442b1d9621da6749d59ea1a544a2ea39b3a79 (patch) | |
tree | 0aa6fb67b70c38cdd949a2b4002ceb459860fa82 /spec/support/helpers | |
parent | b6ec12ceca58b12d974d46d0579742f4d3cdb9d7 (diff) | |
download | gitlab-ce-d65442b1d9621da6749d59ea1a544a2ea39b3a79.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support/helpers')
-rw-r--r-- | spec/support/helpers/graphql_helpers.rb | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/spec/support/helpers/graphql_helpers.rb b/spec/support/helpers/graphql_helpers.rb index bb8b0dfde21..353c632fced 100644 --- a/spec/support/helpers/graphql_helpers.rb +++ b/spec/support/helpers/graphql_helpers.rb @@ -16,6 +16,20 @@ module GraphqlHelpers resolver_class.new(object: obj, context: ctx).resolve(args) 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 def batch(max_queries: nil, &blk) wrapper = proc do @@ -39,7 +53,7 @@ module GraphqlHelpers def batch_sync(max_queries: nil, &blk) wrapper = proc do 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 batch(max_queries: max_queries, &wrapper) @@ -164,16 +178,26 @@ module GraphqlHelpers def attributes_to_graphql(attributes) attributes.map do |name, value| - value_str = if value.is_a?(Array) - '["' + value.join('","') + '"]' - else - "\"#{value}\"" - end + value_str = as_graphql_literal(value) "#{GraphqlHelpers.fieldnamerize(name.to_s)}: #{value_str}" end.join(", ") end + # Fairly dumb Ruby => GraphQL rendering function. Only suitable for testing. + # Missing support for Enums (feel free to add if you need it). + def as_graphql_literal(value) + case value + when Array then "[#{value.map { |v| as_graphql_literal(v) }.join(',')}]" + when Integer, Float then value.to_s + when String then "\"#{value.gsub(/"/, '\\"')}\"" + when nil then 'null' + when true then 'true' + when false then 'false' + else raise ArgumentError, "Cannot represent #{value} as GraphQL literal" + end + end + def post_multiplex(queries, current_user: nil, headers: {}) post api('/', current_user, version: 'graphql'), params: { _json: queries }, headers: headers end @@ -216,6 +240,11 @@ module GraphqlHelpers json_response['data'] || (raise NoData, graphql_errors) end + def graphql_data_at(*path) + keys = path.map { |segment| GraphqlHelpers.fieldnamerize(segment) } + graphql_data.dig(*keys) + end + def graphql_errors case json_response when Hash # regular query |