Commit ded5cdcb authored by Sean McGivern's avatar Sean McGivern

Merge branch '13495-b-graphql-additions' into 'master'

13495-b: Add graphql additions needed by #13495

See merge request gitlab-org/gitlab!21264
parents a9328a10 0f69b572
......@@ -86,3 +86,4 @@ jsdoc/
.projections.json
/qa/.rakeTasks
webpack-dev-server.json
.nvimrc
......@@ -57,13 +57,20 @@ class GitlabSchema < GraphQL::Schema
object.to_global_id
end
def object_from_id(global_id, _ctx = nil)
def object_from_id(global_id, ctx = {})
expected_type = ctx[:expected_type]
gid = GlobalID.parse(global_id)
unless gid
raise Gitlab::Graphql::Errors::ArgumentError, "#{global_id} is not a valid GitLab id."
end
if expected_type && !gid.model_class.ancestors.include?(expected_type)
vars = { global_id: global_id, expected_type: expected_type }
msg = _('%{global_id} is not a valid id for %{expected_type}.') % vars
raise Gitlab::Graphql::Errors::ArgumentError, msg
end
if gid.model_class < ApplicationRecord
Gitlab::Graphql::Loaders::BatchModelLoader.new(gid.model_class, gid.model_id).find
elsif gid.model_class.respond_to?(:lazy_find)
......
......@@ -9,6 +9,10 @@ module Resolvers
def resolve(**args)
super.first
end
def single?
true
end
end
end
......@@ -17,6 +21,10 @@ module Resolvers
def resolve(**args)
super.last
end
def single?
true
end
end
end
......@@ -42,9 +50,13 @@ module Resolvers
override :object
def object
super.tap do |obj|
# If the field this resolver is used in is wrapped in a presenter, go back to it's subject
# If the field this resolver is used in is wrapped in a presenter, unwrap its subject
break obj.subject if obj.is_a?(Gitlab::View::Presenter::Base)
end
end
def single?
false
end
end
end
......@@ -11,6 +11,12 @@ module DesignManagement
#
# The object will have `#versions` called on it to set up the
# initial scope of the versions.
#
# valid params:
# - earlier_or_equal_to: Version
# - sha: String
# - version_id: Integer
#
def initialize(design_or_collection, current_user, params = {})
@design_or_collection = design_or_collection
@current_user = current_user
......@@ -25,6 +31,7 @@ module DesignManagement
items = design_or_collection.versions
items = by_earlier_or_equal_to(items)
items = by_sha(items)
items = by_version_id(items)
items.ordered
end
......@@ -36,6 +43,12 @@ module DesignManagement
items.earlier_or_equal_to(params[:earlier_or_equal_to])
end
def by_version_id(items)
return items unless params[:version_id]
items.id_in(params[:version_id])
end
def by_sha(items)
return items unless params[:sha]
......
......@@ -15,7 +15,9 @@ describe DesignManagement::VersionsFinder do
let(:design_or_collection) { issue.design_collection }
let(:params) { {} }
subject(:versions) { described_class.new(design_or_collection, user, params).execute }
let(:finder) { described_class.new(design_or_collection, user, params) }
subject(:versions) { finder.execute }
describe '#execute' do
shared_examples 'returns no results' do
......@@ -93,6 +95,34 @@ describe DesignManagement::VersionsFinder do
it { is_expected.to contain_exactly(version_2) }
end
end
describe 'returning versions by ID' do
context 'when argument is the first version' do
let(:params) { { version_id: version_1.id } }
it { is_expected.to contain_exactly(version_1) }
end
context 'when argument is the second version' do
let(:params) { { version_id: version_2.id } }
it { is_expected.to contain_exactly(version_2) }
end
end
describe 'mixing id and sha' do
context 'when arguments are consistent' do
let(:params) { { version_id: version_1.id, sha: version_1.sha } }
it { is_expected.to contain_exactly(version_1) }
end
context 'when arguments are in-consistent' do
let(:params) { { version_id: version_1.id, sha: version_2.sha } }
it { is_expected.to be_empty }
end
end
end
end
end
......
......@@ -272,6 +272,9 @@ msgstr ""
msgid "%{from} to %{to}"
msgstr ""
msgid "%{global_id} is not a valid id for %{expected_type}."
msgstr ""
msgid "%{group_docs_link_start}Groups%{group_docs_link_end} allow you to manage and collaborate across multiple projects. Members of a group have access to all of its projects."
msgstr ""
......
......@@ -8,7 +8,7 @@ end
RSpec::Matchers.define :have_graphql_fields do |*expected|
def expected_field_names
expected.map { |name| GraphqlHelpers.fieldnamerize(name) }
Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) }
end
match do |kls|
......
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