2019-06-20 04:02:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module MarkdownField
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
prepended do
|
|
|
|
def self.markdown_field(name, **kwargs)
|
|
|
|
if kwargs[:resolver].present? || kwargs[:resolve].present?
|
|
|
|
raise ArgumentError, 'Only `method` is allowed to specify the markdown field'
|
|
|
|
end
|
|
|
|
|
|
|
|
method_name = kwargs.delete(:method) || name.to_s.sub(/_html$/, '')
|
2020-09-25 05:09:40 -04:00
|
|
|
resolver_method = "#{name}_resolver".to_sym
|
|
|
|
kwargs[:resolver_method] = resolver_method
|
2019-06-20 04:02:33 -04:00
|
|
|
|
|
|
|
kwargs[:description] ||= "The GitLab Flavored Markdown rendering of `#{method_name}`"
|
|
|
|
# Adding complexity to rendered notes since that could cause queries.
|
|
|
|
kwargs[:complexity] ||= 5
|
|
|
|
|
|
|
|
field name, GraphQL::STRING_TYPE, **kwargs
|
2020-09-25 05:09:40 -04:00
|
|
|
|
|
|
|
define_method resolver_method do
|
|
|
|
# We need to `dup` the context so the MarkdownHelper doesn't modify it
|
|
|
|
::MarkupHelper.markdown_field(object, method_name.to_sym, context.to_h.dup)
|
|
|
|
end
|
2019-06-20 04:02:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|