Introduce concept of creating JSON payload for Vue framework
Since we introduced Vue.js we probably need create and use more complex JSON payloads from now on.
From what I understand, we need to move some logic from HAML views to JSON payload, and pass it to Vue using an API endpoint. It is needed to use Vue efficiently as it maps JSON payload into internal model, which is then used in the views.
Currently, we do not have a good way to create complex view-related JSON payload:
-
We can use API
- since we can authenticate using cookie, (thanks@nick.thomas), we can access API from frontend code easily
- but JSON payload provided by API may contain irrelevant data
- payload also won't have some view-related stuff, like paths, URLs, page-context variables
- we may need only a specific subset of data provided by API, or we will need to access multiple endpoints to acquire what is needed
-
We can create an endpoint created in the controller action using
format.json
- this is probably interesting approach, but we do not have means to abstract this concept away
- using
resource.as_json
is probably not good approach when resource has multiple relations - creating JSON payload manually may be error-prone and not robust enough
- we can use grape entities there like
API::Entities::Deployment.new(@deployment).as_json
but it is difficult to extend that with view-related data - example here shows the problem https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/7015#note_17474557
-
We can use view decorators https://gitlab.com/gitlab-org/gitlab-ce/issues/23563
- but view decorator is a little different concept
- view data does not overlap completely with data that may be required by Vue
- we won't be able to use view helpers in decorator https://gitlab.com/gitlab-org/gitlab-ce/issues/23563#note_17515407
- having
SomeDecorator#to_json
may violate cohesion of this class
-
We can use separate concept like serializers
- this may be interesting to have
app/serializers/
along withapp/decorators/
- we would need to pass all request-related variables to the serializer, which is connected to https://gitlab.com/gitlab-org/gitlab-ce/issues/20045
- it would require coherent abstraction we would be able to use in all controllers
- this may be interesting to have
I wonder if an interesting solution would be using separate concept that would include using grape entities, encapsulating request data in object and using view helpers inside separate class.
What do you think @jschatz1 @iamphill @dzaporozhets @rspeicher @rymai @adamniedzielski?