-
Notifications
You must be signed in to change notification settings - Fork 0
/
gql.js
41 lines (35 loc) · 1.07 KB
/
gql.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
// Better GQL Function
// Courtesy @RayhanADev <3
const { lightfetch } = require('lightfetch-node');
class GraphQL {
constructor(token) {
this.headers = {
'X-Requested-With': 'XMLHttpRequest',
'Referrer': 'https://replit.com/',
'Cookie': token ? `connect.sid=${token};` : '',
};
}
async request(query, variables={}) {
const { data, errors } = await lightfetch('https://replit.com/graphql', {
method: 'POST',
headers: {
...this.headers
},
body: {
query,
variables: JSON.stringify(variables),
}
}).then(async (res) => {
let data = await res.text();
//console.log(data);
// ^^^^^^ IF YOU NEED TO DEBUG AN ERROR, UNCOMMENT THIS LINE
return res.json()
}).catch(e => {
console.error(e)
return { data: {}, errors: [e] }
});
if (errors) throw new Error(errors);
return data;
}
}
module.exports = GraphQL