-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
108 lines (86 loc) · 2.97 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
const DataLoader = require('dataloader');
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const graphqlHTTP = require('express-graphql');
const schema = require('./schema');
const yaml = require('js-yaml');
const cors = require('cors');
const config = yaml.safeLoad(fs.readFileSync('./grapi.yml', 'utf8'));
const BASE_URL = config.api_url;
let authHeader;
function getJSONFromURL(url) {
url = url.replace('https', 'http');
const headers = {"Authorization": authHeader};
return axios.get(url, {headers})
.then(res => {
console.log("Request: ", res.request.path);
console.log("Response: ", res.data);
return res.data
}).catch(err => {
console.error(err.response.data);
throw new Error(JSON.stringify(err.response.data));
});
}
function getJSONFromRelativeURL(relativeURL) {
return getJSONFromURL(`${BASE_URL}${relativeURL}`);
}
function getApps(url) {
return getJSONFromRelativeURL(url)
.then(json => json.resources);
}
function getTasks() {
return getJSONFromRelativeURL(`/v3/tasks`)
.then(json => json.resources);
}
function getResourceByURL(url) {
return getJSONFromURL(url);
}
function getManyResourcesByURL(url) {
return getJSONFromURL(url)
.then(json => json.resources);
}
function getV2ResourceByURL(url) {
return getJSONFromURL(url)
.then(json => json.entity);
}
function getV2ResourceByRelativeURL(relativeURL) {
return getV2ResourceByURL(`${BASE_URL}${relativeURL}`);
}
const app = express();
app.use(cors(), graphqlHTTP(req => {
const cacheMap = new Map();
authHeader = req.header('Authorization');
const appLoader =
new DataLoader(keys => Promise.all(keys.map(getApps)), {cacheMap});
const taskLoader =
new DataLoader(keys => Promise.all(keys.map(getTasks)), {cacheMap});
const resourceLoader = {};
const resourceByURLLoader =
new DataLoader(keys => Promise.all(keys.map(getResourceByURL)), {cacheMap});
const manyResourcesByURLLoader =
new DataLoader(keys => Promise.all(keys.map(getManyResourcesByURL)), {cacheMap});
const v2ResourceLoader =
new DataLoader(keys => Promise.all(keys.map(getV2ResourceByURL)), {cacheMap});
const relativeV2ResourceLoader =
new DataLoader(keys => Promise.all(keys.map(getV2ResourceByRelativeURL)), {cacheMap});
appLoader.loadAll = appLoader.load.bind(appLoader);
taskLoader.loadAll = taskLoader.load.bind(taskLoader, '__all__');
resourceLoader.loadByURL = resourceByURLLoader.load.bind(resourceByURLLoader);
resourceLoader.loadManyByURL = manyResourcesByURLLoader.load.bind(manyResourcesByURLLoader);
v2ResourceLoader.loadRelatively = relativeV2ResourceLoader.load.bind(relativeV2ResourceLoader);
const loaders = {
app: appLoader,
resource: resourceLoader,
task: taskLoader,
v2Resource: v2ResourceLoader
};
return {
context: {loaders},
schema,
};
}));
app.listen(
process.env.PORT || 5000,
() => console.log('GraphQL Server running at http://localhost:5000')
);