Limit query with Apollo (like pagination... but not?)


Struggling with a query on Apollo. I want to limit a result to the last 10 records from a postgres table. However I can’t even get the ‘limit’ to work. I assume I need to do something in the schema / resolvers too?

Here’s the code in my component:

// Define the graphql query to retrieve the adverts and the desired attributes
const allAdverts = gql`
  query AdvertsForDisplay ( $limit: Int ) {
    adverts ( limit: $limit ) {

// Use the graphql container to run the allAdverts query
export default AdvertContainer = graphql( allAdverts, {
  options() {
    return {
      variables: {
        limit: 10,
      pollInterval: 15000,
})( Adverts );

As always, any help is very much appreciated! I’m loving GraphQL so far but it takes a while to get your head around it I think :confused:


That’s right - GraphQL doesn’t come with any built-in limit functionality. That argument needs to be implemented in your schema and resolvers.

See here for example:

1 Like

Righto, will get on that now and see how it ends up :slight_smile:

Thanks again @sashko

Just to update this, I found a fairly easy solution. I’m using Sequelize so all I had to do was:

Import Sequelize into my resolvers:

import Sequelize from 'sequelize';

And then add then just amend the adverts query:

adverts( _, args ) {
  return Advert.findAll({ limit: 16, order: [ [ Sequelize.fn('RANDOM') ] ] });

So yeah, sorted! Not sure if Sequelize being imported in my connectors and resolvers will make it slower though… I could merge the files maybe?

Anyway, thanks again @sashko!