Prisma 1 Forum

Query without pagination limits

Hey everyone, I’m here again, :grin::wink:.

This time I’m bringing a theme already discussed a lot, and it involves these links graphcool/feature-requests/, apollographql/react-apollo, dynamically-set-graphql-queries-with-apollo-client, graph.cool/docs and their nested links.

I’m using React + Apollo + Graphcool to build my application, and in my App I have a type Project, where this entity has more than 1000 nodes.

I have a CRUD to work with the Project entity, then the number of projects is constantly updated, increasing and decreasing. Now I’m trying to get all Project entity nodes in my Graphcool API, and to make it happens I need to use the Graphcool pagination, but my problem is that I have a dynamic total amount of Projects, so I need a dynamic query that works querying my all Projects, nothing more, nothing less, the correct total of Projects.

I have written a function to create dynamic Project queries to get all Projects, that function just needs to receive the total amount of Projects, that I can get using the _allProjectsMeta { count } query.

You can check my allProjectsPagination function below:

export const allProjectPagination = (allProjectsMetaCount=1000) => {

  const graphcoolLimit = 1000;
  let counter = allProjectsMetaCount;
  let skip = 0;
  let query = '';

  do {
    query = `${query}allProjects${skip}: allProjects(first: ${counter < graphcoolLimit ? counter : graphcoolLimit}, skip: ${skip * graphcoolLimit}) {
      id
      title
    }`;

    skip++;
    counter -= graphcoolLimit;
  } while (counter > 0);

  query = gql `query allProjectsPagination {${query} _allProjectsMeta { count }}`;

  console.log(query);

  return graphql(query, {
    name: "allProjectPagination",
    options: {
      fetchPolicy: 'network-only'
    }
  })
};

I can’t update my query (Apollo) and I also can’t use only the pagination feature (Graphcool) to get the dynamic amount of Projects. So I would like to have possible solutions or suggestions from you to get my all my Projects.

And yes, I know that I could create an HOC to solve my problem( like this Stack Overflow answer ), but isn’t a so much elegant solution, :expressionless: :upside_down_face: .

I think two queries are required here to acheive what you want - one to get the count and the other to do your paginated query. So, if you really want to avoid using nested components or a HOC, you will have to do both queries within the same component.

If I wanted to do that, I would do the count query using the graphql HOC and then use the componentWillReceiveProps lifecycle method to pick up changes to the count and re run the paginated query using the apollo imperative API.

Personally though, I would use two components - one that is responsible just for fetching and displaying a particular page of results and an outer component that manages the overall count.

2 Likes

Hey @dankent,

Thank you for your explanations, I really appreciate you taking the time to reply my point.

I have decided to avoid the two components solution, so I’m using the graphql HOC to count my projects, Apollo imperative API to work with the dynamic query and react lifecycle methods to get and parse all my projects.

I really liked that solution, to me is clean, elegant and nice.

Best regards, :grin::+1:!!

1 Like