Skip to content

Commit 99271e2

Browse files
committed
fix: fetch projects iteratively
1 parent be761b9 commit 99271e2

5 files changed

Lines changed: 53 additions & 12 deletions

File tree

scripts/fetchData.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { GraphQLClient, gql } from 'graphql-request';
22
import fs from 'fs';
33
import 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

68
const datadir = path.join(__dirname, '../fullData');
79
const baseUrl = process.env.MAPSWIPE_API_ENDPOINT || 'http://localhost:8000/';
@@ -12,7 +14,7 @@ const pipelineType = process.env.PIPELINE_TYPE;
1214

1315
const 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

219228
async 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

src/components/ProjectsMap/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import {
2828

2929
import GestureHandler from 'components/LeafletGestureHandler';
3030
import Link from 'components/Link';
31-
import { AllDataQuery } from 'generated/types';
31+
import { ProjectsPageQuery } from 'generated/types';
3232

3333
import styles from './styles.module.css';
3434

35-
type PublicProject = NonNullable<NonNullable<AllDataQuery['publicProjects']>['results']>[number];
35+
type PublicProject = ProjectsPageQuery['publicProjects']['results'][number];
3636

3737
const pathOptions: {
3838
[key in ProjectStatus]?: CircleMarkerOptions

src/pages/[locale]/data/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ import useDebouncedValue from 'hooks/useDebouncedValue';
4949
import { GlobalExportAssets } from 'utils/queries';
5050
import data from 'fullData/staticData.json';
5151

52-
import { AllDataQuery } from 'generated/types';
52+
import { ProjectsPageQuery, StaticDataQuery } from 'generated/types';
5353
import i18nextConfig from '@/next-i18next.config';
5454

5555
import styles from './styles.module.css';
5656

57+
type AllDataQuery = StaticDataQuery & { publicProjects: ProjectsPageQuery['publicProjects'] };
58+
5759
type PublicProjects = NonNullable<NonNullable<AllDataQuery['publicProjects']>['results']>;
5860
type PublicProject = PublicProjects[number];
5961

src/pages/[locale]/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import data from 'fullData/staticData.json';
2727

2828
import i18nextConfig from '@/next-i18next.config';
2929

30-
import { AllDataQuery } from 'generated/types';
30+
import { ProjectsPageQuery, StaticDataQuery } from 'generated/types';
3131

3232
import styles from './styles.module.css';
3333

34+
type AllDataQuery = StaticDataQuery & { publicProjects: ProjectsPageQuery['publicProjects'] };
35+
3436
async function getAllData() {
3537
// FIXME: This should be inferred
3638
return data as AllDataQuery;

src/pages/[locale]/projects/[id].tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ import {
5353
} from 'utils/chart';
5454
import { UrlInfo } from 'utils/queries';
5555

56-
import { AllDataQuery } from 'generated/types';
56+
import { ProjectsPageQuery, StaticDataQuery } from 'generated/types';
5757
import i18nextConfig from '@/next-i18next.config';
5858

5959
import styles from './styles.module.css';
6060

61+
type AllDataQuery = StaticDataQuery & { publicProjects: ProjectsPageQuery['publicProjects'] };
62+
6163
type PublicProjects = NonNullable<NonNullable<AllDataQuery['publicProjects']>['results']>;
6264
async function getAllProjects() {
6365
return (data as AllDataQuery)?.publicProjects?.results as unknown as PublicProjects;

0 commit comments

Comments
 (0)