Relationship with thousands of records


#1

Hello buddies.
Sorry if I duplicate the topic but I met some misunderstanding during I prepare my relationships in JSON:API topology of response.

I have a lot of records in relation. For an example thousands.

let’s imagine some article has 10000 comments. It means that we have to show all of them in relationships.comments.data within the array:
eg.
“comments”: {
“links”: {
“related”: “{url}/4/comments”,
“self”: “{url}/4/relationships/comments”
},
“data”: [
{
“type”: “comment”,
“id”: “10”
},
{
“type”: “comment”,
“id”: “12”
}

]
}

I thought a lot about this situation and only one decision I got:

  • setup query string param ?include=comments&page[comments.number]=20

These are so raw examples, just to get what I mean.


#2

Hi,
I just was thinking about the same problem.

But why would you call it &page[comments.number]=20, with .number?

I was thinking applying the pagination as well?

/api/articles?page=1
if param “page” is number => just the main resource

/api/articles?page[articles]=1&page[comments]=1&include=comments
if param “page” is array => pass it down to relationships

EDIT:
And actually in the specification its written:

https://jsonapi.org/format/#document-resource-object-relationships
A relationship object that represents a to-many relationship MAY also contain pagination links under the links member, as described below. Any pagination links in a relationship object MUST paginate the relationship data, not the related resources.

So you have the pagination-information inside the relationships:

{ type: article
  relationships: {
     comments: {
          data: [ {type: comments, id: 3}, {type: comments, id: 4},  ]
          links: [ first, last, prev, next ],
          meta: [ current_page: 2, per_page: 2, last_page: 10 ]
     }
  },
  includes: {
       {type: comments, id: 3},
       {type: comments, id: 4},
  }

In includes you are just including the objects, which are inside the relationships.

regards
Stefan


#3

This is an over complicated solution for a simple problem.

If a relationship has the potential to have a lot of records, just avoid adding the data member.

If a client wants to retrieve the records, they can simply call /articles/id/comments and use whatever simple pagination strategy is used by the API.

@SpirIT You are misinterpreting the quote. The links with pagination should point to something like /articles/id/comments?page[number]=1&page[size]=10, definitely not /articles?page[comments]=1.