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

Commit f09327b

Browse files
author
Ray Arayilakath
committed
Refactor Custom Class (and lint)
1 parent 89cb123 commit f09327b

File tree

1 file changed

+126
-130
lines changed

1 file changed

+126
-130
lines changed

src/classes/Custom.js

Lines changed: 126 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,142 @@
1-
import fetch from 'node-fetch'
1+
import fetch from 'node-fetch';
22

3-
import headers from '../utils/headers.js'
4-
import constants from '../utils/constants.js'
3+
import headers from '../utils/headers.js';
4+
import constants from '../utils/constants.js';
55

66
export class CustomDataQuery {
7-
constructor(queryName, customQuery, customVariables) {
8-
this.queryName = queryName;
9-
this.customQuery = customQuery;
10-
this.customVariables = customVariables;
11-
}
12-
13-
async getData() {
14-
let queryName = this.queryName;
15-
let customQuery = this.customQuery;
16-
let customVariables = this.customVariables;
17-
18-
let specialQueryVariables = {
19-
since: 'KarmaSince',
20-
count: 'Int',
21-
id: 'Int',
22-
limit: 'Int'
23-
}
24-
Object.freeze(specialQueryVariables);
25-
26-
let queryVariables = Object.keys(customVariables);
27-
let queryVariablesString = '';
28-
for (let i = 0; i < queryVariables.length; i++) {
29-
let type;
30-
if(specialQueryVariables.hasOwnProperty(queryVariables[i])) {
31-
type = String(specialQueryVariables[queryVariables[i]]);
7+
constructor(queryName, customQuery, customVariables) {
8+
this.queryName = queryName;
9+
this.customQuery = customQuery;
10+
this.customVariables = customVariables;
11+
}
12+
13+
async getData() {
14+
const { queryName } = this;
15+
const { customQuery } = this;
16+
const { customVariables } = this;
17+
18+
const specialQueryVariables = {
19+
since: 'KarmaSince',
20+
count: 'Int',
21+
id: 'Int',
22+
limit: 'Int',
23+
};
24+
Object.freeze(specialQueryVariables);
25+
26+
const queryVariables = Object.keys(customVariables);
27+
let queryVariablesString = '';
28+
for (let i = 0; i < queryVariables.length; i += 1) {
29+
let type;
30+
if (Object.prototype.hasOwnProperty.call(specialQueryVariables, queryVariables[i])) {
31+
type = String(specialQueryVariables[queryVariables[i]]);
3232
} else {
3333
type = String(typeof queryVariables[i]).split('');
3434
type[0] = type[0].toUpperCase();
35-
type = type.join('')
35+
type = type.join('');
36+
}
37+
38+
if (i !== queryVariables.length - 1) {
39+
queryVariablesString += `$${queryVariables[i]}: ${type}!, `;
40+
} else {
41+
queryVariablesString += `$${queryVariables[i]}: ${type}!`;
3642
}
43+
}
44+
45+
const info = await fetch(constants.graphql, {
46+
method: 'POST',
47+
headers,
48+
body: JSON.stringify({
49+
query: `
50+
query ${queryName}(${queryVariablesString}) {
51+
${customQuery}
52+
}`,
53+
variables: JSON.stringify(customVariables),
54+
}),
55+
}).then((res) => res.json());
3756

38-
if(i !== queryVariables.length - 1) {
39-
queryVariablesString += `$${queryVariables[i]}: ${type}!, `
40-
} else {
41-
queryVariablesString += `$${queryVariables[i]}: ${type}!`
42-
}
43-
}
44-
45-
let info = await fetch(variables.graphql, {
46-
method: 'POST',
47-
headers,
48-
body: JSON.stringify({
49-
query: `
50-
query ${queryName}(${queryVariablesString}) {
51-
${customQuery}
52-
}`,
53-
variables: JSON.stringify(customVariables)
54-
})
55-
})
56-
.then(res => res.json());
57-
58-
if (info.errors) {
59-
throw new Error(`Custom Data Query ${this.queryName} returned an error: ${JSON.stringify(info.errors)}`);
60-
} else {
61-
return info.data;
62-
}
63-
}
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+
}
6463
}
6564

6665
export class CustomRecursiveQuery {
67-
constructor(queryName, customQuery, customVariables, treePath, customAfter, customCount) {
68-
this.queryName = queryName;
69-
this.customQuery = customQuery;
70-
this.customVariables = customVariables;
71-
this.treePath = treePath;
72-
this.customAfter = customAfter;
73-
this.customCount = customCount;
74-
}
75-
76-
async getData() {
77-
let queryName = this.queryName;
78-
let customQuery = this.customQuery;
79-
let customVariables = this.customVariables;
80-
let treePath = this.treePath;
81-
let customAfter = this.customAfter;
82-
let customCount = this.customCount;
83-
84-
let specialQueryVariables = {
85-
since: 'KarmaSince',
86-
count: 'Int',
87-
id: 'Int',
88-
limit: 'Int'
89-
}
90-
Object.freeze(specialQueryVariables);
91-
92-
let queryVariables = Object.keys(customVariables);
93-
let queryVariablesString = '';
94-
for (let i = 0; i < queryVariables.length; i++) {
95-
let type;
96-
if(specialQueryVariables.hasOwnProperty(queryVariables[i])) {
97-
type = String(specialQueryVariables[queryVariables[i]]);
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+
const {
77+
queryName, customQuery, customVariables, treePath, customAfter, customCount,
78+
} = this;
79+
80+
const specialQueryVariables = {
81+
since: 'KarmaSince',
82+
count: 'Int',
83+
id: 'Int',
84+
limit: 'Int',
85+
};
86+
Object.freeze(specialQueryVariables);
87+
88+
const queryVariables = Object.keys(customVariables);
89+
let queryVariablesString = '';
90+
for (let i = 0; i < queryVariables.length; i += 1) {
91+
let type;
92+
if (Object.prototype.hasOwnProperty.call(specialQueryVariables, queryVariables[i])) {
93+
type = String(specialQueryVariables[queryVariables[i]]);
9894
} else {
9995
type = String(typeof queryVariables[i]).split('');
10096
type[0] = type[0].toUpperCase();
101-
type = type.join('')
97+
type = type.join('');
10298
}
10399

104-
if(i !== queryVariables.length - 1) {
105-
queryVariablesString += `$${queryVariables[i]}: ${type}!, `
106-
} else {
107-
queryVariablesString += `$${queryVariables[i]}: ${type}!`
108-
}
109-
}
110-
111-
let output = [];
112-
113-
async function recurse(after) {
114-
if (after === null) return;
115-
116-
let info = await variables
117-
.fetch(variables.graphql, {
118-
method: 'POST',
119-
headers,
120-
body: JSON.stringify({
121-
query: `
122-
query ${queryName}(${queryVariablesString}) {
123-
${customQuery}
124-
}`,
125-
variables: JSON.stringify(customVariables)
126-
})
127-
})
128-
.then(res => res.json());
129-
130-
if (info.errors) {
131-
throw new Error(`Custom Recusive Query ${queryName} returned an error: ${JSON.stringify(info.errors)}`);
132-
} else {
133-
info.data[customQuery.trim().split('(')[0]][treePath].items.forEach(item => {
134-
output.push(item);
135-
});
136-
if (output.length !== customCount) {
137-
await recurse(info.data[customQuery.trim().split('(')[0]][treePath].pageInfo.nextCursor);
138-
}
139-
}
140-
141-
}
142-
143-
await recurse(this.customAfter);
144-
return output;
145-
}
146-
}
100+
if (i !== queryVariables.length - 1) {
101+
queryVariablesString += `$${queryVariables[i]}: ${type}!, `;
102+
} else {
103+
queryVariablesString += `$${queryVariables[i]}: ${type}!`;
104+
}
105+
}
106+
107+
const output = [];
108+
109+
async function recurse(recurseAfter) {
110+
if (recurseAfter === null) return;
111+
112+
const info = await fetch(constants.graphql, {
113+
method: 'POST',
114+
headers,
115+
body: JSON.stringify({
116+
query: `
117+
query ${queryName}(${queryVariablesString}) {
118+
${customQuery}
119+
}`,
120+
variables: JSON.stringify({
121+
...customVariables,
122+
after: recurseAfter,
123+
}),
124+
}),
125+
}).then((res) => res.json());
126+
127+
if (info.errors) {
128+
throw new Error(`Custom Recusive Query ${queryName} returned an error: ${JSON.stringify(info.errors)}`);
129+
} else {
130+
info.data[customQuery.trim().split('(')[0]][treePath].items.forEach((item) => {
131+
output.push(item);
132+
});
133+
if (output.length !== customCount) {
134+
await recurse(info.data[customQuery.trim().split('(')[0]][treePath].pageInfo.nextCursor);
135+
}
136+
}
137+
}
138+
139+
await recurse(customAfter);
140+
return output;
141+
}
142+
}

0 commit comments

Comments
 (0)