Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination of a nested resource, without using active record #65

Open
guy-simplee opened this issue Jun 7, 2017 · 6 comments
Open

Pagination of a nested resource, without using active record #65

guy-simplee opened this issue Jun 7, 2017 · 6 comments
Assignees
Labels

Comments

@guy-simplee
Copy link

guy-simplee commented Jun 7, 2017

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
Copy link
Author

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.

@tiagopog
Copy link
Owner

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.

@seanabrahams
Copy link

seanabrahams commented Jan 2, 2018

@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?

@tiagopog
Copy link
Owner

tiagopog commented Jan 6, 2018

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 tiagopog reopened this Jan 6, 2018
@aliaksandrb
Copy link

aliaksandrb commented Nov 14, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants