trailblazer/representable

.represent() does not work with collections and JSON API

promisedlandt opened this issue · 8 comments

I'm trying to use Trailblazer to create a JSON API backend. Unfortunately, I can't get collections to work at all. This might be a mistake on my part, but I think I've tracked it down representable (but I'm not 100% sure).

What it boils down to is that .represent() does not work in JSON API when it's handed a collection.

This works:
Author::Representer::Index.for_collection.new(Author.all).to_json

This does not work:
Author::Representer::Index.represent(Author.all).to_json

I think that should work, right?

I have created a very simple Rails application to demonstrate:
https://github.com/promisedlandt/trailblazerjsonapi

When you say "JSON API", do you mean the JSONAPI module or a random JSON API?

I mean the JSONAPI module.

It's not even released yet 😁 haha! Is everything else working as expected? I will get back to the Roar 1.1 release, soon. Why is this posted on Representable, then, and not Roar, NILS?

Damn, it's unreleased? That explains the next 3 issues in ran into.

I didn't know whether the problem was in trailblazer, roar, representable or the docs -_-

I need the feedback, man, I can't just magically find out what's not working in the current version! 😉

I'm in the chat.

myabc commented

This is an issue with Trailblazer 1.1.x (not sure if Trailblazer 2.0 is affected) and not Representable/ROAR JSON API.

The represent method currently expects an instance of the Array class, which Author.all is not (it's an ActiveRecord::Relation).

Either of the the following should work:

Author::Representer::Index.represent(Author.all.to_a).to_json
Author::Representer::Index.represent(Author.all, ActiveRecord::Relation).to_json