Commit 5e96ce00 authored by Alex Kalderimis's avatar Alex Kalderimis

Add missing coercions

This adds the temporary coercions needed while the compatibility layer
is in place.
parent 09244107
...@@ -18,7 +18,10 @@ module Resolvers ...@@ -18,7 +18,10 @@ module Resolvers
end end
def find_object(id:) def find_object(id:)
dav = GitlabSchema.object_from_id(id, expected_type: ::DesignManagement::DesignAtVersion) # TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
id = ::Types::GlobalIDType[::DesignManagement::DesignAtVersion].coerce_isolated_input(id)
dav = GitlabSchema.find_by_gid(id)
return unless consistent?(dav) return unless consistent?(dav)
dav dav
......
...@@ -50,7 +50,11 @@ module Resolvers ...@@ -50,7 +50,11 @@ module Resolvers
end end
def parse_gid(gid) def parse_gid(gid)
GitlabSchema.parse_gid(gid, expected_type: ::DesignManagement::Design).model_id # TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
gid = ::Types::GlobalIDType[::DesignManagement::Design].coerce_isolated_input(gid)
gid.model_id
end end
end end
end end
......
...@@ -3,15 +3,16 @@ ...@@ -3,15 +3,16 @@
module Resolvers module Resolvers
module DesignManagement module DesignManagement
class DesignsResolver < BaseResolver class DesignsResolver < BaseResolver
argument :ids, DesignID = ::Types::GlobalIDType[::DesignManagement::Design]
[::Types::GlobalIDType[::DesignManagement::Design]], VersionID = ::Types::GlobalIDType[::DesignManagement::Version]
argument :ids, [DesignID],
required: false, required: false,
description: 'Filters designs by their ID' description: 'Filters designs by their ID'
argument :filenames, argument :filenames, [GraphQL::STRING_TYPE],
[GraphQL::STRING_TYPE],
required: false, required: false,
description: 'Filters designs by their filename' description: 'Filters designs by their filename'
argument :at_version, ::Types::GlobalIDType[::DesignManagement::Version], argument :at_version, VersionID,
required: false, required: false,
description: 'Filters designs to only those that existed at the version. ' \ description: 'Filters designs to only those that existed at the version. ' \
'If argument is omitted or nil then all designs will reflect the latest version' 'If argument is omitted or nil then all designs will reflect the latest version'
...@@ -35,10 +36,17 @@ module Resolvers ...@@ -35,10 +36,17 @@ module Resolvers
def version(at_version) def version(at_version)
return unless at_version return unless at_version
GitlabSchema.object_from_id(at_version, expected_type: ::DesignManagement::Version)&.sync # TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
at_version = VersionID.coerce_isolated_input(at_version)
# TODO: when we get promises use this to make resolve lazy
Gitlab::Graphql::Lazy.force(GitlabSchema.find_by_gid(at_version))
end end
def design_ids(gids) def design_ids(gids)
# TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
gids = Array.wrap(gids).map { |id| DesignID.coerce_isolated_input(id) }
Array.wrap(gids).compact.map(&:model_id).presence Array.wrap(gids).compact.map(&:model_id).presence
end end
......
...@@ -5,17 +5,20 @@ module Resolvers ...@@ -5,17 +5,20 @@ module Resolvers
module Version module Version
# Resolver for a DesignAtVersion object given an implicit version context # Resolver for a DesignAtVersion object given an implicit version context
class DesignAtVersionResolver < BaseResolver class DesignAtVersionResolver < BaseResolver
DesignAtVersionID = ::Types::GlobalIDType[::DesignManagement::DesignAtVersion]
DesignID = ::Types::GlobalIDType[::DesignManagement::Design]
include Gitlab::Graphql::Authorize::AuthorizeResource include Gitlab::Graphql::Authorize::AuthorizeResource
type Types::DesignManagement::DesignAtVersionType, null: true type Types::DesignManagement::DesignAtVersionType, null: true
authorize :read_design authorize :read_design
argument :id, ::Types::GlobalIDType[::DesignManagement::DesignAtVersion], argument :id, DesignAtVersionID,
required: false, required: false,
as: :design_at_version_id, as: :design_at_version_id,
description: 'The ID of the DesignAtVersion' description: 'The ID of the DesignAtVersion'
argument :design_id, ::Types::GlobalIDType[::DesignManagement::Design], argument :design_id, DesignID,
required: false, required: false,
description: 'The ID of a specific design' description: 'The ID of a specific design'
argument :filename, GraphQL::STRING_TYPE, argument :filename, GraphQL::STRING_TYPE,
...@@ -29,6 +32,11 @@ module Resolvers ...@@ -29,6 +32,11 @@ module Resolvers
def resolve(design_id: nil, filename: nil, design_at_version_id: nil) def resolve(design_id: nil, filename: nil, design_at_version_id: nil)
validate_arguments(design_id, filename, design_at_version_id) validate_arguments(design_id, filename, design_at_version_id)
# TODO: remove this when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
design_id &&= DesignID.coerce_isolated_input(design_id)
design_at_version_id &&= DesignAtVersionID.coerce_isolated_input(design_at_version_id)
return unless Ability.allowed?(current_user, :read_design, issue) return unless Ability.allowed?(current_user, :read_design, issue)
return specific_design_at_version(design_at_version_id) if design_at_version_id return specific_design_at_version(design_at_version_id) if design_at_version_id
...@@ -49,7 +57,7 @@ module Resolvers ...@@ -49,7 +57,7 @@ module Resolvers
end end
def specific_design_at_version(id) def specific_design_at_version(id)
dav = GitlabSchema.object_from_id(id, expected_type: ::DesignManagement::DesignAtVersion) dav = GitlabSchema.find_by_gid(id)
return unless consistent?(dav) return unless consistent?(dav)
dav dav
...@@ -65,8 +73,8 @@ module Resolvers ...@@ -65,8 +73,8 @@ module Resolvers
dav.design.visible_in?(version) dav.design.visible_in?(version)
end end
def find(id, filename) def find(gid, filename)
ids = [parse_design_id(id).model_id] if id ids = [gid.model_id] if gid
filenames = [filename] if filename filenames = [filename] if filename
::DesignManagement::DesignsFinder ::DesignManagement::DesignsFinder
...@@ -74,10 +82,6 @@ module Resolvers ...@@ -74,10 +82,6 @@ module Resolvers
.execute .execute
end end
def parse_design_id(id)
GitlabSchema.parse_gid(id, expected_type: ::DesignManagement::Design)
end
def issue def issue
version.issue version.issue
end end
......
...@@ -11,7 +11,9 @@ module Resolvers ...@@ -11,7 +11,9 @@ module Resolvers
authorize :read_design authorize :read_design
argument :ids, [::Types::GlobalIDType[::DesignManagement::Design]], DesignID = ::Types::GlobalIDType[::DesignManagement::Design]
argument :ids, [DesignID],
required: false, required: false,
description: 'Filters designs by their ID' description: 'Filters designs by their ID'
argument :filenames, argument :filenames,
...@@ -30,18 +32,16 @@ module Resolvers ...@@ -30,18 +32,16 @@ module Resolvers
private private
def find(ids, filenames) def find(ids, filenames)
ids = ids&.map { |id| parse_design_id(id).model_id } # TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
ids = ids&.map { |id| DesignID.coerce_isolated_input(id) }
::DesignManagement::DesignsFinder.new(issue, current_user, ::DesignManagement::DesignsFinder.new(issue, current_user,
ids: ids, ids: ids&.map(&:model_id),
filenames: filenames, filenames: filenames,
visible_at_version: version) visible_at_version: version)
end end
def parse_design_id(id)
GitlabSchema.parse_gid(id, expected_type: ::DesignManagement::Design)
end
def issue def issue
version.issue version.issue
end end
......
...@@ -11,20 +11,25 @@ module Resolvers ...@@ -11,20 +11,25 @@ module Resolvers
alias_method :collection, :object alias_method :collection, :object
VersionID = ::Types::GlobalIDType[::DesignManagement::Version]
argument :sha, GraphQL::STRING_TYPE, argument :sha, GraphQL::STRING_TYPE,
required: false, required: false,
description: "The SHA256 of a specific version" description: "The SHA256 of a specific version"
argument :id, ::Types::GlobalIDType[::DesignManagement::Version], argument :id, VersionID,
as: :version_id,
required: false, required: false,
description: 'The Global ID of the version' description: 'The Global ID of the version'
def resolve(id: nil, sha: nil) def resolve(version_id: nil, sha: nil)
check_args(id, sha) # TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
version_id &&= VersionID.coerce_isolated_input(version_id)
gid = GitlabSchema.parse_gid(id, expected_type: ::DesignManagement::Version) if id check_args(version_id, sha)
::DesignManagement::VersionsFinder ::DesignManagement::VersionsFinder
.new(collection, current_user, sha: sha, version_id: gid&.model_id) .new(collection, current_user, sha: sha, version_id: version_id&.model_id)
.execute .execute
.first .first
end end
......
...@@ -18,7 +18,11 @@ module Resolvers ...@@ -18,7 +18,11 @@ module Resolvers
end end
def find_object(id:) def find_object(id:)
GitlabSchema.object_from_id(id, expected_type: ::DesignManagement::Version) # TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
id = ::Types::GlobalIDType[::DesignManagement::Version].coerce_isolated_input(id)
GitlabSchema.find_by_gid(id)
end end
end end
end end
......
...@@ -7,13 +7,14 @@ module Resolvers ...@@ -7,13 +7,14 @@ module Resolvers
alias_method :design_or_collection, :object alias_method :design_or_collection, :object
VersionID = ::Types::GlobalIDType[::DesignManagement::Version]
argument :earlier_or_equal_to_sha, GraphQL::STRING_TYPE, argument :earlier_or_equal_to_sha, GraphQL::STRING_TYPE,
as: :sha, as: :sha,
required: false, required: false,
description: 'The SHA256 of the most recent acceptable version' description: 'The SHA256 of the most recent acceptable version'
argument :earlier_or_equal_to_id, argument :earlier_or_equal_to_id, VersionID,
::Types::GlobalIDType[::DesignManagement::Version],
as: :id, as: :id,
required: false, required: false,
description: 'The Global ID of the most recent acceptable version' description: 'The Global ID of the most recent acceptable version'
...@@ -24,6 +25,9 @@ module Resolvers ...@@ -24,6 +25,9 @@ module Resolvers
end end
def resolve(parent: nil, id: nil, sha: nil) def resolve(parent: nil, id: nil, sha: nil)
# TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
id &&= VersionID.coerce_isolated_input(id)
version = cutoff(parent, id, sha) version = cutoff(parent, id, sha)
raise ::Gitlab::Graphql::Errors::ResourceNotAvailable, 'cutoff not found' unless version.present? raise ::Gitlab::Graphql::Errors::ResourceNotAvailable, 'cutoff not found' unless version.present?
...@@ -48,8 +52,7 @@ module Resolvers ...@@ -48,8 +52,7 @@ module Resolvers
end end
end end
def specific_version(id, sha) def specific_version(gid, sha)
gid = GitlabSchema.parse_gid(id, expected_type: ::DesignManagement::Version) if id
find(sha: sha, version_id: gid&.model_id).first find(sha: sha, version_id: gid&.model_id).first
end end
...@@ -59,8 +62,8 @@ module Resolvers ...@@ -59,8 +62,8 @@ module Resolvers
.execute .execute
end end
def by_id(id) def by_id(gid)
GitlabSchema.object_from_id(id, expected_type: ::DesignManagement::Version).sync ::Gitlab::Graphql::Lazy.force(GitlabSchema.find_by_gid(gid))
end end
# Find an `at_version` argument passed to a parent node. # Find an `at_version` argument passed to a parent node.
...@@ -70,7 +73,11 @@ module Resolvers ...@@ -70,7 +73,11 @@ module Resolvers
# for consistency we should only present versions up to the given # for consistency we should only present versions up to the given
# version here. # version here.
def at_version_arg(parent) def at_version_arg(parent)
::Gitlab::Graphql::FindArgumentInParent.find(parent, :at_version, limit_depth: 4) # TODO: remove coercion when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
version_id = ::Gitlab::Graphql::FindArgumentInParent.find(parent, :at_version, limit_depth: 4)
version_id &&= VersionID.coerce_isolated_input(version_id)
version_id
end end
end end
end end
......
...@@ -57,7 +57,7 @@ RSpec.describe Resolvers::DesignManagement::DesignResolver do ...@@ -57,7 +57,7 @@ RSpec.describe Resolvers::DesignManagement::DesignResolver do
end end
context 'the ID belongs to a design on another issue' do context 'the ID belongs to a design on another issue' do
let(:args) { { id: GitlabSchema.id_from_object(design_on_other_issue).to_s } } let(:args) { { id: global_id_of(design_on_other_issue) } }
it 'returns nothing' do it 'returns nothing' do
expect(resolve_design).to be_nil expect(resolve_design).to be_nil
...@@ -65,10 +65,11 @@ RSpec.describe Resolvers::DesignManagement::DesignResolver do ...@@ -65,10 +65,11 @@ RSpec.describe Resolvers::DesignManagement::DesignResolver do
end end
context 'the ID does not belong to a design at all' do context 'the ID does not belong to a design at all' do
let(:args) { { id: GitlabSchema.id_from_object(create(:user)).to_s } } let(:args) { { id: global_id_of(issue) } }
let(:msg) { /does not represent an instance of DesignManagement::Design/ }
it 'complains meaningfully' do it 'complains meaningfully' do
expect { resolve_design }.to raise_error(::Gitlab::Graphql::Errors::ArgumentError, /not a valid ID for DesignManagement::Design/) expect { resolve_design }.to raise_error(msg)
end end
end end
end end
......
...@@ -32,7 +32,7 @@ RSpec.describe Resolvers::DesignManagement::VersionInCollectionResolver do ...@@ -32,7 +32,7 @@ RSpec.describe Resolvers::DesignManagement::VersionInCollectionResolver do
end end
context 'we pass an id' do context 'we pass an id' do
let(:params) { { id: global_id_of(first_version) } } let(:params) { { version_id: global_id_of(first_version) } }
it { is_expected.to eq(first_version) } it { is_expected.to eq(first_version) }
end end
...@@ -44,13 +44,14 @@ RSpec.describe Resolvers::DesignManagement::VersionInCollectionResolver do ...@@ -44,13 +44,14 @@ RSpec.describe Resolvers::DesignManagement::VersionInCollectionResolver do
end end
context 'we pass an inconsistent mixture of sha and version id' do context 'we pass an inconsistent mixture of sha and version id' do
let(:params) { { sha: first_version.sha, id: global_id_of(create(:design_version)) } } let(:params) { { sha: first_version.sha, version_id: global_id_of(create(:design_version)) } }
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end
context 'we pass the id of something that is not a design_version' do context 'we pass the id of something that is not a design_version' do
let(:params) { { id: global_id_of(project) } } let(:params) { { version_id: global_id_of(project) } }
let(:appropriate_error) { ::GraphQL::CoercionError }
it 'raises an appropriate error' do it 'raises an appropriate error' do
expect { result }.to raise_error(appropriate_error) expect { result }.to raise_error(appropriate_error)
......
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