-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (148 loc) · 4.31 KB
/
index.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
const fs = require("fs");
const{ ApolloServerPluginInlineTrace } = require("apollo-server-core");
const { ApolloServer, gql } = require("apollo-server");
const fetch = require("node-fetch");
const typeDefs = fs.readFileSync("./schema.graphql", "utf8").toString();
const headers = {
"X-API-Key": process.env.PROPUBLICA_KEY || "FAKE_KEY",
};
const toCamel = (s) => {
return s.replace(/([-_][a-z])/gi, ($1) => {
return $1.toUpperCase().replace("-", "").replace("_", "");
});
};
const snakeToCamel = (object) => {
const newObj = {};
Object.entries(object).forEach(([key, value]) => {
newObj[toCamel(key)] =
value instanceof Array
? value.map((item) => snakeToCamel(item))
: !!value && typeof value === "object"
? snakeToCamel(value)
: value;
});
return newObj;
};
const resolvers = {
Query: {
congress: async (_, args) => {
if (!args.congress)
throw new Error("Congress session must be specified, eg. 116");
if (!args.chamber)
throw new Error("Congress chamber must be specified, eg. SENATE");
return await fetch(
`https://api.propublica.org/congress/v1/${
args.congress
}/${args.chamber.toLowerCase()}/members.json`,
{
headers,
}
)
.then(async (res) => {
if (res.ok) {
const data = await res.json();
return data.results.map((res) => snakeToCamel(res));
} else {
throw new Error("Error fetching data. Did you include an API Key?");
}
})
.catch((err) => new Error(err));
},
memberById: async (_, args) => {
if (!args.id)
throw new Error(
"Must include an ID for a member of congress to fetch."
);
return await fetch(
`https://api.propublica.org/congress/v1/members/${args.id}.json`,
{
headers,
}
)
.then(async (res) => {
if (res.ok) {
const data = await res.json();
return snakeToCamel(data.results[0]);
} else {
throw new Error("Error fetching data. Did you include an API Key?");
}
})
.catch((err) => new Error(err));
},
},
MemberVote: {
vote: async (parent) => {
if (!parent.voteUri)
throw new Error("Cannot fetch vote without MemberVote.voteUri");
return await fetch(parent.voteUri, {
headers,
})
.then(async (res) => {
if (res.ok) {
const data = await res.json();
return snakeToCamel(data.results.votes.vote);
} else {
throw new Error("Error fetching data. Did you include an API Key?");
}
})
.catch((err) => new Error(err));
},
},
MemberDetails: {
votes: async (parent, args) => {
if (!parent.id)
throw new Error(
"Cannot fetch member vote data if Member.id is not requested."
);
return await fetch(
`https://api.propublica.org/congress/v1/members/${parent.id}/votes.json?offset=${args.offset}`,
{
headers,
}
)
.then(async (res) => {
if (res.ok) {
const data = await res.json();
return data.results[0].votes.map((res) => snakeToCamel(res));
} else {
throw new Error("Error fetching data. Did you include an API Key?");
}
})
.catch((err) => new Error(err));
},
},
Bill: {
details: async (parent, args) => {
if (!parent.apiUri)
throw new Error(
"Cannot fetch member vote data if Member.id is not requested."
);
return await fetch(parent.apiUri, {
headers,
})
.then(async (res) => {
if (res.ok) {
const data = await res.json();
console.log(data);
} else {
throw new Error("Error fetching data. Did you include an API Key?");
}
})
.catch((err) => new Error(err));
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [ApolloServerPluginInlineTrace()],
});
server
.listen({
port: process.env.PORT || 4000,
})
.then(({ port }) => {
console.log(`🚀 Server is running!
📭 Query at https://studio.apollographql.com/dev
🔉 Listening on port ${port}`);
});