Commit dbc5c4e8 authored by Alex Kalderimis's avatar Alex Kalderimis

Adds ability to assert expected superclass in object_from_id

With suggestions from @rpereira2
parent 82226d53
......@@ -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)
......
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