Fix snippet blob viewers graphql endpoints
In the snippets VUE refactor we make use of the GraphQL endpoints. Specifically, we request for snippets and also the blob, where the blob represents the real content to represent in the view.
Each blob can have two types of viewers: simple_viewer
and rich_viewer
. A simple_viewer
represents the text with lines codes and with the syntax highlighted. A rich_viewer
represents the data in a custom way, because the content can be a binary or a non binary one with special format, like markdown.
In the current state, we're not returning the proper info to the FE and we need to address that. When the requested info is for the simpleData
attribute in GraphQL, handled by https://gitlab.com/gitlab-org/gitlab/blob/master/app/presenters/snippet_blob_presenter.rb#L17
, we need to return always the context highlighted. Basically we would need to change that line to highlighted(plain: false)
.
But, the problem is with the richData
attribute. When the content is binary, we don't render anything, nevertheless, there are 4 types of files that can have a rich viewer attached: SVG, openapi, notebook, and markup. If the blob is not one of those types, the blob won't have a rich_viewer
. The file type is based on the snippet#file_name
attribute.
And how the content for richData
is rendered? Well, this is the bad part, the logic is spread across the views. For example:
- SVG type is handled by
app/views/projects/blob/viewers/_svg.html.haml
- notebook type is handled by
app/views/projects/blob/viewers/_notebook.html.haml
- openapi type is handled by
app/views/projects/blob/viewers/_openapi.html.haml
- markdown type is handled by
app/views/projects/blob/viewers/_markup.html.haml
At the moment the FE is creating their own viewers, so basically it will have to create a viewer for each one of these rich types and then, in https://gitlab.com/gitlab-org/gitlab/blob/master/app/presenters/snippet_blob_presenter.rb#L4 we should handle the file type and render the content based on the logic inside the views.
That is one solution, but the less optimal. Why? because when we add a new blob type rendered, we will have to:
- First, create a view in haml to handle the logic
- Second, remember to update https://gitlab.com/gitlab-org/gitlab/blob/master/app/presenters/snippet_blob_presenter.rb
- Third, remember to update the FE to add the new type
The most optimal solution would be to remove all these viewers from the FE and dump whatever the backend sends in the richData
attribute. To implement this basically we would need to render, from the presenter, the specific view, and that is not an easy task. Invoking the rendering outside a controller is a headache and, even more, when the views can call helpers and routes. I started to work on this in https://gitlab.com/gitlab-org/gitlab/-/tree/fj-fix-blob-viewers-graphql, nevertheless there are still some errors in the specs.
My suggestion is to continue or see how we can render the viewers view directly and return that. Otherwise, we will have to go for the boring solution, replication what the views do to render the content.
More info
There is a great deep dive about GrapQL in create-stage#1 (closed), although there is no need to touch the GraphQL code.