2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-23 03:55:14 -04:00
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module Present
|
|
|
|
class Instrumentation
|
|
|
|
def instrument(type, field)
|
2018-06-26 12:31:05 -04:00
|
|
|
return field unless field.metadata[:type_class]
|
|
|
|
|
2018-05-23 03:55:14 -04: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 04:59:00 -04:00
|
|
|
# We need to wrap the original presentation type into a type that
|
|
|
|
# uses the presenter as an object.
|
2018-05-23 03:55:14 -04:00
|
|
|
object = presented_type.object
|
2018-06-25 04:59:00 -04:00
|
|
|
|
|
|
|
if object.is_a?(presented_in.presenter_class)
|
|
|
|
next old_resolver.call(presented_type, args, context)
|
|
|
|
end
|
|
|
|
|
2018-05-23 03:55:14 -04:00
|
|
|
presenter = presented_in.presenter_class.new(object, **context.to_h)
|
2018-06-25 04:59:00 -04:00
|
|
|
wrapped = presented_type.class.new(presenter, context)
|
|
|
|
|
|
|
|
old_resolver.call(wrapped, args, context)
|
2018-05-23 03:55:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
field.redefine do
|
|
|
|
resolve(resolve_with_presenter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|