Limit linked child documents but return total count

Hi! I was wondering if it's possible, having a category that has some posts, to limit the amount of posts to return when querying all categories? Also, is there a way to know the total count of posts without having to query all of them?

Hey Jessica,

Sure what you're looking for is pagination:

The API will also return the total number of results when you do a query.

Thanks.

thanks @Phil for the quick response. Doesn't that only applies to the parent document, meaning when you query all categories in this case?

But how would you limit the posts for each category using pagination?

So basically I'll have this query

allCategories {
    id
    ....
    posts_link {
        post {
            ...
        }
    }
}

I only want the first 5 post for every category

Thank you!

Ah ok, I see you're using GraphQL.

In that case, you can use The after & first arguments for pagination and totalCount for the results:

I'm actually not using the graphql api, sorry about the confusion. I just used as en example, but I'm actually using the REST API using the Prismic Client from @prismicio/client. I do use the graphQuery option to query the specific fields

But anyway, even using the graphql API, what you sent me only allows you to paginate the parent documents, in my case the categories, but not the linked post for each category.

What I need is, all categories, and from each category, I want the first 5 posts

OK, I think I understand what you're asking now.

The example you showed uses a GraphQL syntax, GraphQuery doesn't have anything like allCategories. You're doing normal REST API queries for the documents and adding the specific fields to be retrieved with the GraphQuery option.

In that case, since your querying different categories and limiting the results to 5, you need to do multiple 'Queries by field' for the posts by category content relationship and paginate the results of each:

I realise doing multiple queries for categories would involve a lot of processing time, but if you're using technology like Next.js to statically deploy your website then this shouldn't be an issue.

Let me know if this is clear.

Thanks.

Yes sorry about the confusion with the graphql query, I just wanted to make it clearer to describe what I wanted.

First thanks for the quick response.
If I understood that correctly, I'd have to query the categories first, and then for each category, query the posts for each category with a limit of 5, is that what you meant?
I'm in fact using NextJS but since the page will sit behind a login, it won't be statically generated. So as you said, this could end up having many requests, specially when having lot's of categories. (Categories are requested in an infinite scroll type of page)

And just to be clear, this is not possible with either the RestAPI nor the GraphQL api, right?

Yes, what your asking in terms of paginating linked content isn't possible in either API and wouldn't be an efficient way of managing this anyway as you'd have to keep a MASSIVE categories page up to date.

What I suggested above was to query by categories if you knew the category so yes, I suppose that getting all the categories first and searching by that would make this automatic, but again that would be a really slow set of queries.

What you should do is query every post in the store using getInitialProps, then call this content on your page behind the login and do the sorting by category on the frontend of your website. This will be the fastest most performant way to do this.

What do you mean by this?
Categories in my case would be like having a lists of Posts, and the each Post can have a bunch of comments. Having a feed style page, where I show a list of posts with the top 5 comments, and then load more as you scroll.

Querying all posts in getInitialProps might also not be a solution, since Posts could have hundreds of records.

What if I query the first 10 categories, and then query all posts by categories ids? Is there any way to do that? In that case I'd only do 2 queries and I could limit both the categories queries and the posts by categories ids query

UPDATE: I couldn't find a way to query the posts by category since the relationship comes from the categories and not the posts.

Hi @levijesica, Phil is off for a couple of days so I'm taking this over for him while he's gone.

If I understand correctly you have a page on your site where you want to show all your categories and the first 5 posts for each category. In Prismic, you have a Categories type with a Group field that contain a Content Relationship field where you link to the Posts.

  • Category 1
    • Group field
      • Link to Post 1
      • Link to Post 2
      • Link to Post 3
      • And so on...
  • Category 2
    • Group field
      • Link to Post 1
      • Link to Post 2
      • And so on...

Do I have this correct?

If so, then it seems to me that you could just query the categories that you need, and use the GraphQuery to retrieve whichever fields you need from the Posts to build the page. Unfortunately there isn't any way to limit the number of Posts you retrieve in the Group field, so you will get all of them. Then in your Next.js app, you would only use the first 5. And the length of the Group field array would tell you the total number of Posts for that Category.

Is that what you're trying to do or am I missing something?

hi @levi, yes that's exactly what I'm trying to do, and today I'm actually doing doing exactly what you suggested. Getting all linked documents from the group field and the only showing the first 5 from in the app.

I just wanted to make sure that there was no other way to limit this from the API

Thank you!

No problem, I'm happy that we eventually got to the answer!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.