2018-11-09 18:39:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-23 07:55:14 +00:00
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module Present
|
|
|
|
class Instrumentation
|
|
|
|
def instrument(type, field)
|
2018-06-26 16:31:05 +00:00
|
|
|
return field unless field.metadata[:type_class]
|
|
|
|
|
2018-05-23 07:55:14 +00:00
|
|
|
presented_in = field.metadata[:type_class].owner
|
|
|
|
return field unless presented_in.respond_to?(:presenter_class)
|
|
|
|
return field unless presented_in.presenter_class
|
|
|
|
|
|
|
|
old_resolver = field.resolve_proc
|
|
|
|
|
|
|
|
resolve_with_presenter = -> (presented_type, args, context) do
|
2018-06-25 08:59:00 +00:00
|
|
|
# We need to wrap the original presentation type into a type that
|
|
|
|
# uses the presenter as an object.
|
2018-05-23 07:55:14 +00:00
|
|
|
object = presented_type.object
|
2018-06-25 08:59:00 +00:00
|
|
|
|
|
|
|
if object.is_a?(presented_in.presenter_class)
|
|
|
|
next old_resolver.call(presented_type, args, context)
|
|
|
|
end
|
|
|
|
|
2018-05-23 07:55:14 +00:00
|
|
|
presenter = presented_in.presenter_class.new(object, **context.to_h)
|
2019-08-26 17:43:38 +00:00
|
|
|
|
|
|
|
# we have to use the new `authorized_new` method, as `new` is protected
|
|
|
|
wrapped = presented_type.class.authorized_new(presenter, context)
|
2018-06-25 08:59:00 +00:00
|
|
|
|
|
|
|
old_resolver.call(wrapped, args, context)
|
2018-05-23 07:55:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
field.redefine do
|
|
|
|
resolve(resolve_with_presenter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|