diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2019-06-20 11:02:33 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2019-06-20 11:02:33 +0300 |
commit | 406808583c2392a0b57f68f98b2418e9d23b23ab (patch) | |
tree | 98613723a6caf6dc152f9ff431e75906ebe2c5ca /lib/gitlab/graphql | |
parent | adeccba13676b831335e2f12f779f77602298b31 (diff) |
Render GFM html in GraphQL
This adds a `markdown_field` to our types.
Using this helper will render a model's markdown field using the
existing `MarkupHelper` with the context of the GraphQL query
available to the helper.
Having the context available to the helper is needed for redacting
links to resources that the current user is not allowed to see.
Because rendering the HTML can cause queries, the complexity of a
these fields is raised by 5 above the default.
The markdown field helper can be used as follows:
```
markdown_field :note_html, null: false
```
This would generate a field that will render the markdown field `note`
of the model. This could be overridden by adding the `method:`
argument. Passing a symbol for the method name:
```
markdown_field :body_html, null: false, method: :note
```
It will have this description by default:
> The GitLab Flavored Markdown rendering of `note`
This could be overridden by passing a `description:` argument.
The type of a `markdown_field` is always `GraphQL::STRING_TYPE`.
Diffstat (limited to 'lib/gitlab/graphql')
-rw-r--r-- | lib/gitlab/graphql/markdown_field.rb | 26 | ||||
-rw-r--r-- | lib/gitlab/graphql/markdown_field/resolver.rb | 22 |
2 files changed, 48 insertions, 0 deletions
diff --git a/lib/gitlab/graphql/markdown_field.rb b/lib/gitlab/graphql/markdown_field.rb new file mode 100644 index 00000000000..7be6810f7ba --- /dev/null +++ b/lib/gitlab/graphql/markdown_field.rb @@ -0,0 +1,26 @@ +# 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$/, '') + kwargs[:resolve] = Gitlab::Graphql::MarkdownField::Resolver.new(method_name.to_sym).proc + + 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 + end + end + end + end +end diff --git a/lib/gitlab/graphql/markdown_field/resolver.rb b/lib/gitlab/graphql/markdown_field/resolver.rb new file mode 100644 index 00000000000..11a01b95ad1 --- /dev/null +++ b/lib/gitlab/graphql/markdown_field/resolver.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Gitlab + module Graphql + module MarkdownField + class Resolver + attr_reader :method_name + + def initialize(method_name) + @method_name = method_name + end + + def proc + -> (object, _args, ctx) do + # We need to `dup` the context so the MarkdownHelper doesn't modify it + ::MarkupHelper.markdown_field(object, method_name, ctx.to_h.dup) + end + end + end + end + end +end |