jsonapi-utils icon indicating copy to clipboard operation
jsonapi-utils copied to clipboard

Pagination of a nested resource, without using active record

Open guy-simplee opened this issue 7 years ago • 6 comments

Hi, In our project, we are using JU for our API, currently only for get calls. We have had some difficulties since we do not have direct access to our models in the API project, meaning we only have resources and controllers, and we are retrieving the data manually in the controller actions, into a hash which we then pass to jsonapi_format and render.

Specifically with pagination, what happens is the following:

We have some nested resources, with the routes declared this way:

jsonapi_resources :accounts, :only => [:show] do
     jsonapi_resources :bills, :only => [:index]
end

So for our bills index route, we end up with the expected request url of "http://localhost:3005/accounts/2/bills" for example, but the pagination links get generated with the following url, which doesn't exist in our routing:

"http://localhost:3005/bills?page%5Bnumber%5D=2&page%5Bsize%5D=1" (example for the "next" link)

If i manually add the account part to the request url, it seems like the paging functionality works as expected and I see the next page in the response.

My question is if there is any support for this scenario of pagination of a nested resource, or do I have no alternative but to manually alter the links section of the response and concatenate the missing part of the URL, which seems far from an ideal solution to me.

Not sure if this is a JU or JR issue, but any help would be much appreciated!

Thanks!

guy-simplee avatar Jun 07 '17 11:06 guy-simplee

Update: In the end we found that solving it by a different method would require a lot of overriding of the JR methods, which was too much overhead so we simply manipulated the response before rendering, to display the correct link.

guy-simplee avatar Jun 12 '17 08:06 guy-simplee

Sorry for not answering in time, @guy-simplee. I went through hard days last week, but as soon as I get some free time, I'll check this issue in order to try taking some insights of improvement from it.

tiagopog avatar Jun 12 '17 14:06 tiagopog

@tiagopog I'd like to revisit this issue as I'm currently experiencing it while looking into using jsonapi-utils for a project. In the README you have an example response for a nested resource (https://github.com/tiagopog/jsonapi-utils#nested-resources)

Request:

GET /users/1/posts HTTP/1.1
Accept: application/vnd.api+json

Response:

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json

{
  "data": [
    {
      "id": "1",
      "type": "posts",
      "links": {
        "self": "http://api.myblog.com/posts/1"
      },
      "attributes": {
        "title": "An awesome post",
        "body": "Lorem ipsum dolot sit amet"
      },
      "relationships": {
        "author": {
          "links": {
            "self": "http://api.myblog.com/posts/1/relationships/author",
            "related": "http://api.myblog.com/posts/1/author"
          }
        }
      }
    }
  ],
  "meta": {
    "record_count": 1
  },
  "links": {
    "first": "http://api.myblog.com/posts?page%5Bnumber%5D=1&page%5Bsize%5D=10",
    "last": "http://api.myblog.com/posts?page%5Bnumber%5D=1&page%5Bsize%5D=10"
  }
}

Note that the response.dig("data", 0, "links", "self") points to http://api.myblog.com/posts/1 whereas we requested /users/1/posts which would leave me to expect the value to be http://api.myblog.com/users/1/posts/1?

seanabrahams avatar Jan 02 '18 20:01 seanabrahams

I think it's definitely worth revisiting it, @seanabrahams. Since I'm currently working on a few improvements for the gem I may come up with a solution for this issue soon.

tiagopog avatar Jan 06 '18 01:01 tiagopog

Bumping the tread, I was affected by that behavior too :/

How much work it would require to fix it on at gem side? Probably might help a bit.

aliaksandrb avatar Nov 14 '18 02:11 aliaksandrb

Looks like a daunting task, related issues from jsonapi-resources repo:

  • https://github.com/cerebris/jsonapi-resources/issues/970
  • https://github.com/cerebris/jsonapi-resources/issues/542

aliaksandrb avatar Nov 14 '18 19:11 aliaksandrb