Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 15cffde

Browse files
author
Rayhan Arayilakath
authored
Merge pull request #27 from RayhanADev/Add-Custom-Query-Class
Add Custom Query Class
2 parents 7cd920f + 258396b commit 15cffde

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ module.exports = {
1010
Board: replapi.Board,
1111
Notifications: replapi.Notifications,
1212
Misc: replapi.Misc,
13-
Login: replapi.Login
13+
Login: replapi.Login,
14+
CustomDataQuery: replapi.CustomDataQuery,
15+
CustomRecursiveQuery: replapi.CustomRecursiveQuery
1416
};

src/classes/Custom.js

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
let headers = require('../utils/headers.js');
2+
let variables = require('../utils/variables.js');
3+
4+
class CustomDataQuery {
5+
constructor(queryName, customQuery, customVariables) {
6+
this.queryName = queryName;
7+
this.customQuery = customQuery;
8+
this.customVariables = customVariables;
9+
}
10+
11+
async getData() {
12+
let queryName = this.queryName;
13+
let customQuery = this.customQuery;
14+
let customVariables = this.customVariables;
15+
16+
let specialQueryVariables = {
17+
since: 'KarmaSince',
18+
count: 'Int',
19+
id: 'Int',
20+
limit: 'Int'
21+
}
22+
Object.freeze(specialQueryVariables);
23+
24+
let queryVariables = Object.keys(customVariables);
25+
let queryVariablesString = '';
26+
for (let i = 0; i < queryVariables.length; i++) {
27+
let type;
28+
if(specialQueryVariables.hasOwnProperty(queryVariables[i])) {
29+
type = String(specialQueryVariables[queryVariables[i]]);
30+
} else {
31+
type = String(typeof queryVariables[i]).split('');
32+
type[0] = type[0].toUpperCase();
33+
type = type.join('')
34+
}
35+
36+
if(i !== queryVariables.length - 1) {
37+
queryVariablesString += `$${queryVariables[i]}: ${type}!, `
38+
} else {
39+
queryVariablesString += `$${queryVariables[i]}: ${type}!`
40+
}
41+
}
42+
43+
let info = await variables
44+
.fetch(variables.graphql, {
45+
method: 'POST',
46+
headers,
47+
body: JSON.stringify({
48+
query: `
49+
query ${queryName}(${queryVariablesString}) {
50+
${customQuery}
51+
}`,
52+
variables: JSON.stringify(customVariables)
53+
})
54+
})
55+
.then(res => res.json());
56+
57+
if (info.errors) {
58+
throw new Error(`Custom Data Query ${this.queryName} returned an error: ${JSON.stringify(info.errors)}`);
59+
} else {
60+
return info.data;
61+
}
62+
}
63+
}
64+
65+
class CustomRecursiveQuery {
66+
constructor(queryName, customQuery, customVariables, treePath, customAfter, customCount) {
67+
this.queryName = queryName;
68+
this.customQuery = customQuery;
69+
this.customVariables = customVariables;
70+
this.treePath = treePath;
71+
this.customAfter = customAfter;
72+
this.customCount = customCount;
73+
}
74+
75+
async getData() {
76+
let queryName = this.queryName;
77+
let customQuery = this.customQuery;
78+
let customVariables = this.customVariables;
79+
let treePath = this.treePath;
80+
let customAfter = this.customAfter;
81+
let customCount = this.customCount;
82+
83+
let specialQueryVariables = {
84+
since: 'KarmaSince',
85+
count: 'Int',
86+
id: 'Int',
87+
limit: 'Int'
88+
}
89+
Object.freeze(specialQueryVariables);
90+
91+
let queryVariables = Object.keys(customVariables);
92+
let queryVariablesString = '';
93+
for (let i = 0; i < queryVariables.length; i++) {
94+
let type;
95+
if(specialQueryVariables.hasOwnProperty(queryVariables[i])) {
96+
type = String(specialQueryVariables[queryVariables[i]]);
97+
} else {
98+
type = String(typeof queryVariables[i]).split('');
99+
type[0] = type[0].toUpperCase();
100+
type = type.join('')
101+
}
102+
103+
if(i !== queryVariables.length - 1) {
104+
queryVariablesString += `$${queryVariables[i]}: ${type}!, `
105+
} else {
106+
queryVariablesString += `$${queryVariables[i]}: ${type}!`
107+
}
108+
}
109+
110+
let output = [];
111+
112+
async function recurse(after) {
113+
if (after === null) return;
114+
115+
let info = await variables
116+
.fetch(variables.graphql, {
117+
method: 'POST',
118+
headers,
119+
body: JSON.stringify({
120+
query: `
121+
query ${queryName}(${queryVariablesString}) {
122+
${customQuery}
123+
}`,
124+
variables: JSON.stringify(customVariables)
125+
})
126+
})
127+
.then(res => res.json());
128+
129+
if (info.errors) {
130+
throw new Error(`Custom Recusive Query ${queryName} returned an error: ${JSON.stringify(info.errors)}`);
131+
} else {
132+
info.data[customQuery.trim().split('(')[0]][treePath].items.forEach(item => {
133+
output.push(item);
134+
});
135+
if (output.length !== customCount) {
136+
await recurse(info.data[customQuery.trim().split('(')[0]][treePath].pageInfo.nextCursor);
137+
}
138+
}
139+
140+
}
141+
142+
await recurse(this.customAfter);
143+
return output;
144+
}
145+
}
146+
147+
module.exports = {
148+
CustomDataQuery: CustomDataQuery,
149+
CustomRecursiveQuery: CustomRecursiveQuery
150+
};

src/classes/User.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class User {
4141
let user = this.username;
4242
let output = [];
4343

44-
async function recurse(after) {
44+
async function recurse(after) {
4545
if (after === null) return;
4646

4747
let info = await variables

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const languages = require('./classes/Languages.js');
88
const board = require('./classes/Board.js');
99
const notifications = require('./classes/Notifications.js');
1010
const misc = require('./classes/Misc.js')
11+
const custom = require('./classes/Custom.js')
1112

1213
if (!login) throw new Error('Login class not found');
1314
if (!user) throw new Error('User class not found');
@@ -19,6 +20,7 @@ if (!languages) throw new Error('Languages class not found');
1920
if (!board) throw new Error('Board class not found');
2021
if (!notifications) throw new Error('Notifications class not found');
2122
if (!misc) throw new Error('Miscellaneous class not found');
23+
if (!custom) throw new Error('Custom class not found');
2224

2325
module.exports = {
2426
Login: login.Login,
@@ -30,5 +32,7 @@ module.exports = {
3032
Languages: languages.Languages,
3133
Board: board.Board,
3234
Notifications: notifications.Notifications,
33-
Misc: misc.Misc
35+
Misc: misc.Misc,
36+
CustomDataQuery: custom.CustomDataQuery,
37+
CustomRecursiveQuery: custom.CustomRecursiveQuery
3438
}

0 commit comments

Comments
 (0)