-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ts
115 lines (110 loc) · 3.74 KB
/
setup.ts
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
/* eslint-disable @typescript-eslint/naming-convention */
import { commands, window, workspace } from 'vscode';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { isAbsolute, join } from 'path';
import axios from 'axios';
export const sampleSupergraphJson = {
subgraphs: {
// eslint-disable-next-line @typescript-eslint/naming-convention
Subgraph1: {
path: '',
localUrl: 'http://localhost:3000/graphql',
},
// eslint-disable-next-line @typescript-eslint/naming-convention
Subgraph2: {
path: 'subgraphs/subgraph2',
localUrl: 'http://localhost:3001/graphql',
},
// eslint-disable-next-line @typescript-eslint/naming-convention
Subgraph3: {
path: 'subgraphs/subgraph3',
localUrl: 'http://localhost:3002/graphql',
devUrl: 'https://sampleendpointoverridingstudioconfig.com/graphql',
},
},
supergraphs: {
// eslint-disable-next-line @typescript-eslint/naming-convention
Supergraph1: ['Subgraph1', 'Subgraph3'],
// eslint-disable-next-line @typescript-eslint/naming-convention
Supergraph2: ['Subgraph2', 'Subgraph3'],
},
};
export const generateTemplate = (rootPath: string) => {
if (!existsSync(rootPath + '/.rover-runner')) {
mkdirSync(rootPath + '/.rover-runner');
}
if (!existsSync(rootPath + '/.vscode')) {
mkdirSync(rootPath + '/.vscode');
}
if (!existsSync(rootPath + '/.rover-runner/supergraph.json')) {
writeFileSync(
`${rootPath}/.rover-runner/supergraph.json`,
JSON.stringify(sampleSupergraphJson, null, 2),
'utf-8'
);
commands.executeCommand('subgraphsList.refreshEntry');
}
};
export const isApolloConfigured = () => {
if (
workspace.getConfiguration().get('apolloStudioConfiguration.apolloKey', '')
.length > 0 &&
workspace
.getConfiguration()
.get('apolloStudioConfiguration.apolloGraphRef', '').length > 0
) {
return true;
}
return false;
};
export const fetchSubgraphUrls = async () => {
const apolloKey = workspace
.getConfiguration()
.get('apolloStudioConfiguration.apolloKey', '');
const graphRef = workspace
.getConfiguration()
.get('apolloStudioConfiguration.apolloGraphRef', '');
const body = {
operationName: 'GetSubgraphUrls',
query:
'query GetSubgraphUrls($ref: ID!) { variant(ref: $ref) { ... on GraphVariant { subgraphs { name url }} ... on InvalidRefFormat { message }}}',
variables: { ref: graphRef },
};
return await axios
.post('https://api.apollographql.com/graphql', body, {
headers: {
Connection: 'keep-alive',
'Content-Type': 'application/json',
Accept: 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
'apollographql-client-name': 'rover-runner',
'apollographql-client-version': '0.1.0',
'X-API-KEY': apolloKey,
},
})
.then((response) => {
if (response.status === 200) {
return response.data;
} else {
const unexpectedResponse = `fetchSubgraphUrls status ${response.status} - ${response.statusText}`;
console.log(unexpectedResponse);
window.showErrorMessage(unexpectedResponse);
return {};
}
})
.catch((error) => {
const errorResponse = `fetchSubgraphUrls status ${error.response.status} - ${error.response.statusText}`;
console.log(errorResponse);
window.showErrorMessage(errorResponse);
return {};
});
};
export const makePathAbsolute = (filePath: string, configPath: string) => {
// If absolute or empty path, then return
if (isAbsolute(filePath) || filePath.length === 0) {
return filePath;
}
// Convert relative path to be absolute
const workspaceRoot = configPath.split('.rover-runner')[0];
return join(workspaceRoot, filePath);
};