This repository has been archived by the owner on Jan 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
1070 lines (965 loc) · 27.5 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const BillTemplate = path.resolve('./src/components/templates/Bill/index.tsx');
const ElectedOfficialTemplate = path.resolve(
'./src/components/templates/ElectedOfficial/index.tsx'
);
const CommitteeTemplate = path.resolve(
'./src/components/templates/Committee/index.tsx'
);
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
// Root
'@data': path.resolve(__dirname, 'data'),
// `src/**`
'@components': path.resolve(__dirname, 'src/components'),
'@icons': path.resolve(__dirname, 'src/icons'),
'@pages': path.resolve(__dirname, 'src/pages'),
'@type': path.resolve(__dirname, 'src/types'),
'@utils': path.resolve(__dirname, 'src/utils'),
'@constants': path.resolve(__dirname, 'src/constants'),
// `static/**`
'@static': path.resolve(__dirname, 'static'),
'@images': path.resolve(__dirname, 'static/images'),
'@states': path.resolve(__dirname, 'static/images/states'),
},
},
});
};
exports.createPages = async ({ graphql, actions: gatsbyActions, reporter }) => {
const bills = await fetchAllBills(graphql, reporter);
const amendments = await fetchAllAmendments(graphql, reporter);
const cosponsorships = await fetchAllCosponsorships(graphql, reporter);
const actions = await fetchAllBillActions(graphql, reporter);
let rollCalls = await fetchAllRollCalls(graphql, reporter);
let electedOfficials = await fetchAllElectedOfficials(graphql, reporter);
const committees = await fetchAllCommittees(graphql, reporter);
const images = await fetchAllImages(graphql, reporter);
const subCommittees = await fetchAllSubCommittees(graphql, reporter);
// Inject elected_official's Image data
electedOfficials = electedOfficials.map((electedOfficial) => ({
...electedOfficial,
image: findImage(images, electedOfficial.id),
}));
// Inject Roll call voter's elected_official data
rollCalls = rollCalls.map((rollCall) => {
const votes = rollCall.votes?.map((vote) => ({
...vote,
elected_official: findElectedOfficial(
electedOfficials,
vote.elected_official_id
),
}));
return {
...rollCall,
votes,
};
});
// Create elected official pages //
for (const electedOfficial of electedOfficials) {
await createElectedOfficialPage(gatsbyActions.createPage, electedOfficial, {
bills,
amendments,
cosponsorships,
committees,
subCommittees,
rollCalls,
});
}
// Create bill pages //
for (const bill of bills) {
await createBillPage(gatsbyActions.createPage, bill, {
electedOfficials,
cosponsorships,
actions,
rollCalls,
});
}
// Create committee pages //
for (const committee of committees) {
await createCommitteePage(gatsbyActions.createPage, committee, {
bills,
actions,
electedOfficials,
subCommittees,
});
}
};
// Pages
async function createBillPage(
createPageFn,
bill,
{ electedOfficials, cosponsorships, actions, rollCalls }
) {
// Inject chamber data
bill.originalChamber = getOriginalChamber(bill.type);
// Inject bill status
bill.status = humanizeBillStatus(bill.status, bill.originalChamber);
// Inject Sponsor's elected_official data
bill.sponsor = findElectedOfficial(electedOfficials, bill.sponsor_id);
// Inject cosponsorship data
bill.cosponsorships = findBillCosponsorships(cosponsorships, bill.id);
// Inject cosponsor's elected_official data
bill.cosponsorships = bill.cosponsorships.map((cosponsorship) => ({
...cosponsorship,
elected_official: findElectedOfficial(
electedOfficials,
cosponsorship.elected_official_id
),
}));
// Inject actions
bill.actions = actions.filter((action) => action.bill_id === bill.id);
// Inject Roll Calls
bill.roll_calls = rollCalls.filter(
(rollCall) => rollCall.bill_id === bill.id
);
// Inject roll Call into actions
bill.actions = bill.actions.map((action) => {
// Add missing roll property if mentioned in text
if (action.text.includes('Record Vote Number:')) {
// TODO: Replace with regex capture groups
const voteTextSplit = action.text.split('Record Vote Number: ');
let roll = voteTextSplit[voteTextSplit.length - 1];
// Take period out
roll = roll.substr(0, roll.length - 1);
action.roll = Number(roll);
}
if (action.text.includes('(Roll no. ')) {
// TODO: Replace with regex capture groups
const voteTextSplit = action.text.split('(Roll no. ');
let roll = voteTextSplit[voteTextSplit.length - 1];
// Take `).` out
roll = roll.substr(0, roll.length - 2);
action.roll = Number(roll);
}
action.roll_call = findRollCall(bill.roll_calls, Number(action.roll));
return action;
});
// Inject chamber into actions
bill.actions = bill.actions.map((action, index) => {
if (index === 0) {
action.chamber = bill.originalChamber;
}
let lastAction = null;
if (index > 0) {
lastAction = bill.actions[index - 1];
action.chamber = lastAction.chamber;
}
if (action?.roll_call?.chamber === 'h') {
action.chamber = 'HOUSE';
}
if (action?.roll_call?.chamber === 's') {
action.chamber = 'SENATE';
}
if (action.status === 'PASS_OVER:SENATE') {
action.chamber = 'SENATE';
}
if (action.status === 'PASS_OVER:HOUSE') {
action.chamber = 'HOUSE';
}
if (lastAction?.status === 'PASS_OVER:SENATE') {
action.chamber = 'HOUSE';
}
if (lastAction?.status === 'PASS_OVER:HOUSE') {
action.chamber = 'SENATE';
}
if (action.text?.includes('Presented to President')) {
action.chamber = 'PRESIDENT';
}
if (action.type === 'enacted') {
action.chamber = 'PRESIDENT';
}
return action;
});
const billSlug = `${bill.congress}/${bill.type}${bill.number}`;
createPageFn({
path: billSlug,
component: BillTemplate,
context: {
slug: billSlug,
bill,
},
});
}
async function createElectedOfficialPage(
createPageFn,
electedOfficial,
{ bills, amendments, cosponsorships, committees, subCommittees, rollCalls }
) {
const electedOfficialSlug = `officials/${electedOfficial.id}`;
const electedOfficialBills = bills.filter(
(bill) => bill.sponsor_id === electedOfficial.id
);
const electedOfficialAmendments = amendments.filter(
(amendment) => amendment.sponsor_id === electedOfficial.id
);
const electedOfficialCosponsorships = cosponsorships.filter(
(cosponsorship) => cosponsorship.elected_official_id === electedOfficial.id
);
createPageFn({
path: electedOfficialSlug,
component: ElectedOfficialTemplate,
context: {
slug: electedOfficialSlug,
electedOfficial: {
id: electedOfficial.id,
position: electedOfficial.position,
rank: electedOfficial.rank,
state: electedOfficial.state,
political_party: electedOfficial.political_party,
gender: electedOfficial.gender,
district: electedOfficial.district,
first_name: electedOfficial.first_name,
last_name: electedOfficial.last_name,
preferred_name: electedOfficial.preferred_name,
is_active: electedOfficial.is_active,
house_terms: electedOfficial.house_terms,
senate_terms: electedOfficial.senate_terms,
vice_president_terms: electedOfficial.vice_president_terms,
president_terms: electedOfficial.president_terms,
term_end_at: electedOfficial.term_end_at,
term_start_at: electedOfficial.term_start_at,
born_at: electedOfficial.born_at,
billsCount: electedOfficialBills.length,
bills: electedOfficialBills,
amendmentsCount: electedOfficialAmendments.length,
amendments: electedOfficialAmendments,
cosponsorshipsCount: electedOfficialCosponsorships.length,
cosponsorships: electedOfficialCosponsorships,
committeesMembershipsCount:
electedOfficial.committee_memberships_aggregate.aggregate.count,
committeesMemberships:
electedOfficial.committee_memberships_aggregate.nodes.map(
(committeeMembershipNode) => {
return {
id: committeeMembershipNode.id,
title: committeeMembershipNode.title,
rank: committeeMembershipNode.rank,
party: committeeMembershipNode.party,
committee: committees.find(
(committee) =>
committee.id === committeeMembershipNode.committee_id
),
};
}
),
subCommitteesMembershipsCount:
electedOfficial.subcommittee_memberships_aggregate.aggregate.count,
subCommitteesMemberships:
electedOfficial.subcommittee_memberships_aggregate.nodes.map(
(subcommitteeMembershipNode) => {
return {
title: subcommitteeMembershipNode.title,
rank: subcommitteeMembershipNode.rank,
party: subcommitteeMembershipNode.party,
subCommittees: subCommittees.find(
(subcommittee) =>
subcommittee.id === subcommitteeMembershipNode.id
),
};
}
),
votesCount: electedOfficial.votes_aggregate.aggregate.count,
votes: electedOfficial.votes_aggregate.nodes.map((voteNode) => {
return {
id: voteNode.id,
state: voteNode.state,
date: voteNode.date,
decision: voteNode.decision,
created_at: voteNode.created_at,
rollCall: rollCalls.find(
(rollCall) => rollCall.id === voteNode.roll_call_id
),
};
}),
},
},
});
}
async function createCommitteePage(
createPageFn,
committee,
{ bills, actions, electedOfficials, subCommittees }
) {
// Inject bills that committee has worked on
const committeeBills = committee.bills_aggregate.nodes.map(
(billsAggregateNode) => {
return {
...bills.find((bill) => bill.id === billsAggregateNode.id),
activity: billsAggregateNode.activity,
};
}
);
// Inject actions
const committeeActions = committee.committee_actions_aggregate.nodes.map(
({ action_id }) => actions.find((action) => action.id === action_id)
);
// Inject elected officials into member nodes
const committeeMembers = committee.committee_memberships_aggregate.nodes.map(
(membership) => {
return {
...electedOfficials.find(
(electedOfficial) =>
electedOfficial.id === membership.elected_official_id
),
rank: membership.rank,
title: membership.title,
};
}
);
const committeeSubCommittees = committee.subcommittees_aggregate.nodes.map(
({ id }) => subCommittees.find((subCommittee) => subCommittee.id === id)
);
const committeeSlug = `committees/${committee.id}`;
createPageFn({
path: committeeSlug,
component: CommitteeTemplate,
context: {
slug: committeeSlug,
committee: {
id: committee.id,
name: committee.name,
type: committee.type,
jurisdiction: committee.jurisdiction,
url: committee.url,
created_at: committee.created_at,
updated_at: committee.updated_at,
description: committee.description,
billsCount: committee.bills_aggregate.aggregate.count,
bills: committeeBills,
membersCount: committee.committee_memberships_aggregate.aggregate.count,
members: committeeMembers,
subCommitteesCount: committee.subcommittees_aggregate.aggregate.count,
subCommittees: committeeSubCommittees,
actionsCount: committee.committee_actions_aggregate.aggregate.count,
actions: committeeActions,
},
},
});
}
// Sources
async function queryGraphqlSource(graphqlFn, reporter, { query, variables }) {
const response = await graphqlFn(query, variables);
if (response.errors) {
reporter.panicOnBuild('Error while running GraphQL query.');
console.error(response.errors);
throw new Error(
'Error while running GraphQL query. See logs above for details.'
);
}
return response;
}
async function paginateGraphqlSource(
graphqlFn,
reporter,
{ query, totalCount, queryLimit, dataKey }
) {
console.debug(`Paginating through all ${dataKey} data.`);
const timerName = `Time taken to paginate through all ${dataKey} data`;
console.time(timerName);
let collection = [];
for (let offset = 0; totalCount > offset; offset += queryLimit) {
const response = await queryGraphqlSource(graphqlFn, reporter, {
query,
variables: {
limit: queryLimit,
offset,
},
});
if (!response.data.hasura[dataKey]) {
reporter.panicOnBuild(
`Graphql response does not have provided dataKey "${dataKey}"`
);
console.error(response.errors);
throw new Error(
'Error while running GraphQL query. See logs above for details.'
);
}
collection = [...collection, ...response.data.hasura[dataKey]];
}
console.timeEnd(timerName);
return collection;
}
// Data fetches
async function fetchAllBills(graphqlFn, reporter) {
const billsCountQuery = `
query BillCountQuery {
hasura {
bills_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: billsCountQuery,
});
const billsCount = response.data.hasura.bills_aggregate.aggregate.count;
const billsQuery = `
query BillsQuery($limit: Int!, $offset: Int!) {
hasura {
bills(
limit: $limit
offset: $offset
order_by: { status_at: desc }
) {
id
congress
type
number
type
congress
number
subject
subjects
title
short_title
summary
sponsor_id
by_request
status
status_at
introduced_at
created_at
updated_at
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: billsQuery,
totalCount: billsCount,
queryLimit: 500,
dataKey: 'bills',
});
}
async function fetchAllAmendments(graphqlFn, reporter) {
const amendmentsCountQuery = `
query AmendmentsCountQuery {
hasura {
amendments_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: amendmentsCountQuery,
});
const amendmentsCount =
response.data.hasura.amendments_aggregate.aggregate.count;
const amendmentsQuery = `
query AmendmentsQuery($limit: Int!, $offset: Int!) {
hasura {
amendments(
limit: $limit
offset: $offset
order_by: { status_at: desc }
) {
id
congress
type
number
type
congress
number
sponsor_id
status
status_at
introduced_at
updated_at
bill_id
purpose
description
amendment_id
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: amendmentsQuery,
totalCount: amendmentsCount,
queryLimit: 500,
dataKey: 'amendments',
});
}
async function fetchAllCosponsorships(graphqlFn, reporter) {
const cosponsorshipsCountQuery = `
query CosponsorshipsCountQuery {
hasura {
cosponsorships_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: cosponsorshipsCountQuery,
});
const cosponsorshipsCount =
response.data.hasura.cosponsorships_aggregate.aggregate.count;
const cosponsorshipsQuery = `
query CosponsorshipsQuery($limit: Int!, $offset: Int!) {
hasura {
cosponsorships(
limit: $limit
offset: $offset
order_by: { sponsored_at: desc }
where: { withdrawn_at: { _is_null: true } }
) {
id
original_cosponsor
state
district
sponsored_at
withdrawn_at
elected_official_id
bill_id
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: cosponsorshipsQuery,
totalCount: cosponsorshipsCount,
queryLimit: 15000,
dataKey: 'cosponsorships',
});
}
async function fetchAllBillActions(graphqlFn, reporter) {
const actionsCountQuery = `
query ActionsCountQuery {
hasura {
actions_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: actionsCountQuery,
});
const actionsCount = response.data.hasura.actions_aggregate.aggregate.count;
const actionsQuery = `
query ActionsQuery($limit: Int!, $offset: Int!) {
hasura {
actions(
limit: $limit
offset: $offset
order_by: { acted_at: asc }
) {
acted_at
action_code
amendment_id
bill_id
how
id
references
result
roll
status
suspension
text
type
vote_type
where
committees {
committee_id
}
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: actionsQuery,
totalCount: actionsCount,
queryLimit: 10000,
dataKey: 'actions',
});
}
async function fetchAllRollCalls(graphqlFn, reporter) {
const rollCallsCountQuery = `
query RollCallsCountQuery {
hasura {
roll_calls_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: rollCallsCountQuery,
});
const rollCallsCount =
response.data.hasura.roll_calls_aggregate.aggregate.count;
const rollCallsQuery = `
query RollCallsQuery($limit: Int!, $offset: Int!) {
hasura {
roll_calls(
limit: $limit
offset: $offset
order_by: { date: asc }
) {
id
type
chamber
congress
number
requires
date
amendment_id
bill_id
result
result_text
category
subject
nomination
question
record_modified_at
session
updated_at
votes {
id
decision
elected_official_id
state
date
created_at
}
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: rollCallsQuery,
totalCount: rollCallsCount,
queryLimit: 150,
dataKey: 'roll_calls',
});
}
async function fetchAllCommittees(graphqlFn, reporter) {
const committeesCountQuery = `
query CommitteesCountQuery {
hasura {
committees_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: committeesCountQuery,
});
const committeesCount =
response.data.hasura.committees_aggregate.aggregate.count;
const committeesQuery = `
query CommitteesQuery($limit: Int!, $offset: Int!) {
hasura {
committees(
limit: $limit
offset: $offset
) {
id
name
type
jurisdiction
url
created_at
updated_at
description
bills_aggregate {
aggregate {
count
}
nodes {
activity
bill_id
}
}
committee_memberships_aggregate {
aggregate {
count
}
nodes {
id
elected_official_id
title
rank
created_at
updated_at
}
}
subcommittees_aggregate {
aggregate {
count
}
nodes {
id
}
}
committee_actions_aggregate {
aggregate {
count
}
nodes {
action_id
}
}
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: committeesQuery,
totalCount: committeesCount,
queryLimit: 4,
dataKey: 'committees',
});
}
async function fetchAllSubCommittees(graphqlFn, reporter) {
const subCommitteesCountQuery = `
query SubCommitteesCountQuery {
hasura {
subcommittees_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: subCommitteesCountQuery,
});
const subCommitteesCount =
response.data.hasura.subcommittees_aggregate.aggregate.count;
const subCommitteesQuery = `
query SubCommitteesQuery($limit: Int!, $offset: Int!) {
hasura {
subcommittees(
limit: $limit
offset: $offset
) {
id
name
committee_id
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: subCommitteesQuery,
totalCount: subCommitteesCount,
queryLimit: 3,
dataKey: 'subcommittees',
});
}
async function fetchAllElectedOfficials(graphqlFn, reporter) {
const electedOfficialsCountQuery = `
query ElectedOfficialsCountQuery {
hasura {
elected_officials_aggregate {
aggregate {
count
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: electedOfficialsCountQuery,
});
const electedOfficialsCount =
response.data.hasura.elected_officials_aggregate.aggregate.count;
const electedOfficialsQuery = `
query ElectedOfficialsQuery($limit: Int!, $offset: Int!) {
hasura {
elected_officials(
limit: $limit
offset: $offset
) {
id
position
rank
state
political_party
gender
district
first_name
last_name
preferred_name
is_active
house_terms
senate_terms
vice_president_terms
president_terms
term_end_at
term_start_at
born_at
created_at
updated_at
}
}
}
`;
return paginateGraphqlSource(graphqlFn, reporter, {
query: electedOfficialsQuery,
totalCount: electedOfficialsCount,
queryLimit: 150,
dataKey: 'elected_officials',
});
}
async function fetchAllImages(graphqlFn, reporter) {
console.debug('Fetching images');
const timerName = 'Time taken to fetch images';
console.time(timerName);
const imagesQuery = `
query ImagesQuery {
congressImages: allFile(
filter: { sourceInstanceName: { eq: "congressImages" } }
) {
nodes {
extension
name
modifiedTime
childImageSharp {
gatsbyImageData(
quality: 80
height: 400
formats: [AUTO, WEBP, AVIF]
)
}
}
}
}
`;
const response = await queryGraphqlSource(graphqlFn, reporter, {
query: imagesQuery,
});
console.timeEnd(timerName);
return response.data.congressImages.nodes;
}
// Lookups
function findImage(images, elected_official_id) {
return images.find((image) => image.name === elected_official_id);
}
function findRollCall(billRollCalls, roll) {
return billRollCalls.find((rollCall) => roll === rollCall.number);
}
function findElectedOfficial(electedOfficials, elected_official_id) {
const match = electedOfficials.find(
(electedOfficial) => electedOfficial.id === elected_official_id
);
if (!match) {
throw new Error(
`Elected Official not in memory where elected_official_id: ${elected_official_id}`
);
}
return match;
}
function findBillCosponsorships(cosponsorships, bill_id) {
return cosponsorships.filter((cosponsorship) => {
return cosponsorship.bill_id === bill_id;
});
}
// Normalizations
function getOriginalChamber(type) {
const HOUSE = 'HOUSE';
const SENATE = 'SENATE';
switch (type.toLowerCase()) {
case 's':
case 'sres':
case 'sjres':
return SENATE;
case 'hr':
case 'hres':
case 'hjres':
default:
return HOUSE;
}
}