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
end
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)
dav
......
......@@ -50,7 +50,11 @@ module Resolvers
end
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
......
......@@ -3,15 +3,16 @@
module Resolvers
module DesignManagement
class DesignsResolver < BaseResolver
argument :ids,
[::Types::GlobalIDType[::DesignManagement::Design]],
DesignID = ::Types::GlobalIDType[::DesignManagement::Design]
VersionID = ::Types::GlobalIDType[::DesignManagement::Version]
argument :ids, [DesignID],
required: false,
description: 'Filters designs by their ID'
argument :filenames,
[GraphQL::STRING_TYPE],
argument :filenames, [GraphQL::STRING_TYPE],
required: false,
description: 'Filters designs by their filename'
argument :at_version, ::Types::GlobalIDType[::DesignManagement::Version],
argument :at_version, VersionID,
required: false,
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'
......@@ -35,10 +36,17 @@ module Resolvers
def version(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
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
end
......
......@@ -5,17 +5,20 @@ module Resolvers
module Version
# Resolver for a DesignAtVersion object given an implicit version context
class DesignAtVersionResolver < BaseResolver
DesignAtVersionID = ::Types::GlobalIDType[::DesignManagement::DesignAtVersion]
DesignID = ::Types::GlobalIDType[::DesignManagement::Design]
include Gitlab::Graphql::Authorize::AuthorizeResource
type Types::DesignManagement::DesignAtVersionType, null: true
authorize :read_design
argument :id, ::Types::GlobalIDType[::DesignManagement::DesignAtVersion],
argument :id, DesignAtVersionID,
required: false,
as: :design_at_version_id,
description: 'The ID of the DesignAtVersion'
argument :design_id, ::Types::GlobalIDType[::DesignManagement::Design],
argument :design_id, DesignID,
required: false,
description: 'The ID of a specific design'
argument :filename, GraphQL::STRING_TYPE,
......@@ -29,6 +32,11 @@ module Resolvers
def resolve(design_id: nil, filename: nil, design_at_version_id: nil)
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 specific_design_at_version(design_at_version_id) if design_at_version_id
......@@ -49,7 +57,7 @@ module Resolvers
end
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)
dav
......@@ -65,8 +73,8 @@ module Resolvers
dav.design.visible_in?(version)
end
def find(id, filename)
ids = [parse_design_id(id).model_id] if id
def find(gid, filename)
ids = [gid.model_id] if gid
filenames = [filename] if filename
::DesignManagement::DesignsFinder
......@@ -74,10 +82,6 @@ module Resolvers
.execute
end
def parse_design_id(id)
GitlabSchema.parse_gid(id, expected_type: ::DesignManagement::Design)
end
def issue
version.issue
end
......
......@@ -11,7 +11,9 @@ module Resolvers
authorize :read_design
argument :ids, [::Types::GlobalIDType[::DesignManagement::Design]],
DesignID = ::Types::GlobalIDType[::DesignManagement::Design]
argument :ids, [DesignID],
required: false,
description: 'Filters designs by their ID'
argument :filenames,
......@@ -30,18 +32,16 @@ module Resolvers
private
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,
ids: ids,
ids: ids&.map(&:model_id),
filenames: filenames,
visible_at_version: version)
end
def parse_design_id(id)
GitlabSchema.parse_gid(id, expected_type: ::DesignManagement::Design)
end
def issue
version.issue
end
......
......@@ -11,20 +11,25 @@ module Resolvers
alias_method :collection, :object
VersionID = ::Types::GlobalIDType[::DesignManagement::Version]
argument :sha, GraphQL::STRING_TYPE,
required: false,
description: "The SHA256 of a specific version"
argument :id, ::Types::GlobalIDType[::DesignManagement::Version],
argument :id, VersionID,
as: :version_id,
required: false,
description: 'The Global ID of the version'
def resolve(id: nil, sha: nil)
check_args(id, sha)
def resolve(version_id: nil, sha: nil)
# 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
.new(collection, current_user, sha: sha, version_id: gid&.model_id)
.new(collection, current_user, sha: sha, version_id: version_id&.model_id)
.execute
.first
end
......
......@@ -18,7 +18,11 @@ module Resolvers
end
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
......
......@@ -7,13 +7,14 @@ module Resolvers
alias_method :design_or_collection, :object
VersionID = ::Types::GlobalIDType[::DesignManagement::Version]
argument :earlier_or_equal_to_sha, GraphQL::STRING_TYPE,
as: :sha,
required: false,
description: 'The SHA256 of the most recent acceptable version'
argument :earlier_or_equal_to_id,
::Types::GlobalIDType[::DesignManagement::Version],
argument :earlier_or_equal_to_id, VersionID,
as: :id,
required: false,
description: 'The Global ID of the most recent acceptable version'
......@@ -24,6 +25,9 @@ module Resolvers
end
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)
raise ::Gitlab::Graphql::Errors::ResourceNotAvailable, 'cutoff not found' unless version.present?
......@@ -48,8 +52,7 @@ module Resolvers
end
end
def specific_version(id, sha)
gid = GitlabSchema.parse_gid(id, expected_type: ::DesignManagement::Version) if id
def specific_version(gid, sha)
find(sha: sha, version_id: gid&.model_id).first
end
......@@ -59,8 +62,8 @@ module Resolvers
.execute
end
def by_id(id)
GitlabSchema.object_from_id(id, expected_type: ::DesignManagement::Version).sync
def by_id(gid)
::Gitlab::Graphql::Lazy.force(GitlabSchema.find_by_gid(gid))
end
# Find an `at_version` argument passed to a parent node.
......@@ -70,7 +73,11 @@ module Resolvers
# for consistency we should only present versions up to the given
# version here.
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
......
......@@ -57,7 +57,7 @@ RSpec.describe Resolvers::DesignManagement::DesignResolver do
end
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
expect(resolve_design).to be_nil
......@@ -65,10 +65,11 @@ RSpec.describe Resolvers::DesignManagement::DesignResolver do
end
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
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
......
......@@ -32,7 +32,7 @@ RSpec.describe Resolvers::DesignManagement::VersionInCollectionResolver do
end
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) }
end
......@@ -44,13 +44,14 @@ RSpec.describe Resolvers::DesignManagement::VersionInCollectionResolver do
end
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 }
end
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
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