birkir / gatsby-source-prismic-graphql

Gatsby source plugin for Prismic GraphQL
MIT License
137 stars 75 forks source link

Can't load more than 20 items #246

Open necheporenko opened 3 years ago

necheporenko commented 3 years ago

As you can see on my screenshot, I have 46 items, but on edges I see only 20. How to increase this limit?

image

Giulico commented 3 years ago

It returns 20 items by design. You need to use pagination, take a look here: Paginate your results

necheporenko commented 3 years ago

So, is it possible to get all items on one page? Or I should create something like this: /page-1 / (0-20) /page-2 / (20-40) /page-3 / (40-50)

necheporenko commented 3 years ago

I have worked with Netlify CMS in the past and have not seen this limitation on GraphQL.

Giulico commented 3 years ago

As far as I can see you have few choises: 1) Create different pages, as you said 2) Use client API to fetch the next pages with ajax (those items will not be rendered on the static page) 3) Use gatsby-node.js to build an array that contains all your documents (retrieved with a pagination loop) and pass it down to the page through pageContext.

necheporenko commented 3 years ago

@Giulico, got it, thanks.

andreasrippl commented 3 years ago

@necheporenko Did you manage it and if yes could you share your approach?

necheporenko commented 3 years ago

@andreasrippl, yes, I've used the client API.

  useEffect(() => {
    let collectedData;
    const loadMoreProjects = async (res) => {
      const response = await client.query({
        query: gql`
          query($lang: String!, $after: String!) {
            allProjects(lang: $lang, after: $after) {
              edges {
                node {
                  title
                  description
                  image
                  logo
                  _meta {
                    id
                  }
                }
              }
              pageInfo {
                hasNextPage
                endCursor
              }
            }
          }
        `,
        variables: { lang: pageContext.lang, after: res.pageInfo.endCursor },
      });
      collectedData = [...prismicProjects, ...response.data.allProjects.edges];

      if (response.data.allProjects.pageInfo.hasNextPage) {
        loadMoreProjects(response.data.allProjects);
      } else {
        setPrismicProjects(collectedData);
        return Promise.resolve();
      }
    };

    if (PrismicDataAllProjects.pageInfo.hasNextPage) {
      loadMoreProjects(PrismicDataAllProjects);
    }
  }, []);