11import { GraphQLClient , gql } from 'graphql-request' ;
22import fs from 'fs' ;
33import path from 'path' ;
4- import { AllDataQuery } from '../generated/types' ;
4+ import { ProjectsPageQuery , StaticDataQuery } from '../generated/types' ;
5+
6+ type FetchedData = StaticDataQuery & { publicProjects : ProjectsPageQuery [ 'publicProjects' ] } ;
57
68const datadir = path . join ( __dirname , '../fullData' ) ;
79const baseUrl = process . env . MAPSWIPE_API_ENDPOINT || 'http://localhost:8000/' ;
@@ -12,7 +14,7 @@ const pipelineType = process.env.PIPELINE_TYPE;
1214
1315const graphQLClient = new GraphQLClient ( GRAPHQL_ENDPOINT ) ;
1416
15- const dummyData : AllDataQuery = {
17+ const dummyData : FetchedData = {
1618 publicProjects : {
1719 results : [ ] ,
1820 totalCount : 0 ,
@@ -29,15 +31,17 @@ const dummyData: AllDataQuery = {
2931 globalExportAssets : [ ] ,
3032} ;
3133
32- const query = gql `
33- query AllData {
34+ const PROJECT_PAGE_SIZE = 500 ;
35+
36+ const projectsQuery = gql `
37+ query ProjectsPage($limit: Int!, $offset: Int!) {
3438 publicProjects(
3539 filters: {
3640 status: {
3741 inList: [PUBLISHED, FINISHED],
3842 },
3943 },
40- pagination: { limit: 9999 },
44+ pagination: { limit: $limit, offset: $offset },
4145 ) {
4246 results {
4347 id
@@ -166,6 +170,11 @@ const query = gql`
166170 }
167171 totalCount
168172 }
173+ }
174+ ` ;
175+
176+ const staticQuery = gql `
177+ query StaticData {
169178 communityStats {
170179 id
171180 totalContributors
@@ -217,7 +226,7 @@ async function getCsrfTokenValue() {
217226}
218227
219228async function fetchAndWriteData ( ) {
220- let data = { } as AllDataQuery ;
229+ let data = { } as FetchedData ;
221230 if ( pipelineType === 'ci' ) {
222231 data = dummyData ;
223232 } else {
@@ -236,7 +245,33 @@ async function fetchAndWriteData() {
236245 graphQLClient . setHeader ( 'X-CSRFToken' , csrfTokenValue ) ;
237246 graphQLClient . setHeader ( 'Cookie' , `${ COOKIE_NAME } =${ csrfTokenValue } ` ) ;
238247 graphQLClient . setHeader ( 'Referer' , referer ) ;
239- data = ( await graphQLClient . request ( query ) ) as AllDataQuery ;
248+
249+ const staticData = ( await graphQLClient . request ( staticQuery ) ) as Pick <
250+ FetchedData ,
251+ 'communityStats' | 'publicOrganizations' | 'globalExportAssets'
252+ > ;
253+
254+ const allProjects : FetchedData [ 'publicProjects' ] [ 'results' ] = [ ] ;
255+ let totalCount = Infinity ;
256+ // eslint-disable-next-line no-await-in-loop
257+ for ( let offset = 0 ; offset < totalCount ; offset += PROJECT_PAGE_SIZE ) {
258+ // eslint-disable-next-line no-await-in-loop
259+ const page = ( await graphQLClient . request ( projectsQuery , {
260+ limit : PROJECT_PAGE_SIZE ,
261+ offset,
262+ } ) ) as Pick < FetchedData , 'publicProjects' > ;
263+ allProjects . push ( ...page . publicProjects . results ) ;
264+ totalCount = page . publicProjects . totalCount ;
265+ console . log ( `Fetched ${ allProjects . length } /${ totalCount } projects` ) ;
266+ }
267+
268+ data = {
269+ ...staticData ,
270+ publicProjects : {
271+ results : allProjects ,
272+ totalCount,
273+ } ,
274+ } ;
240275 }
241276
242277 // ensure the `data` directory exists
0 commit comments