debian-mirror-gitlab/lib/gitlab/graphql/markdown_field.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
1.1 KiB
Ruby
Raw Normal View History

2019-09-30 21:07:59 +05:30
# 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$/, '')
2021-01-03 14:25:43 +05:30
resolver_method = "#{name}_resolver".to_sym
kwargs[:resolver_method] = resolver_method
2019-09-30 21:07:59 +05:30
2023-04-23 21:23:45 +05:30
kwargs[:description] ||= "GitLab Flavored Markdown rendering of `#{method_name}`"
2019-09-30 21:07:59 +05:30
# Adding complexity to rendered notes since that could cause queries.
kwargs[:complexity] ||= 5
2021-10-27 15:23:28 +05:30
field name, GraphQL::Types::String, **kwargs
2021-01-03 14:25:43 +05:30
define_method resolver_method do
2022-07-23 23:45:48 +05:30
markdown_object = block_given? ? yield(object) : object
2021-01-03 14:25:43 +05:30
# We need to `dup` the context so the MarkdownHelper doesn't modify it
2022-07-23 23:45:48 +05:30
::MarkupHelper.markdown_field(markdown_object, method_name.to_sym, context.to_h.dup)
2021-01-03 14:25:43 +05:30
end
2019-09-30 21:07:59 +05:30
end
end
end
end
end