-
Notifications
You must be signed in to change notification settings - Fork 2
/
QueryAppInterface.ts
55 lines (45 loc) · 1.77 KB
/
QueryAppInterface.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
import { useState, useEffect } from 'react';
import { request } from 'graphql-request';
import { useEntity } from '@backstage/plugin-catalog-react';
import { useApi, configApiRef } from '@backstage/core-plugin-api';
const QueryQontract = (query: string, path?: string) => {
type QontractApp = Record<string, any>;
// Get Backstage objects
const config = useApi(configApiRef);
const { entity } = useEntity();
// Constants
const backendUrl = config.getString('backend.baseUrl');
const proxyUrl = `${backendUrl}/api/proxy/visual-qontract/graphql`
const [result, setResult] = useState<QontractApp>({});
const [loaded, setLoaded] = useState<boolean>(false);
const [error, setError] = useState<boolean>(false);
// Function to get the app interface path
const getQontractPath = () => {
if (path) {
return path
}
// use 'visual-qontract/app-path' annotation if defined on the entity
const appPath = entity?.metadata?.annotations?.["visual-qontract/app-path"];
if (appPath) {
return appPath
}
// otherwise fall back to making an educated guess at this entity's app path
const platform = entity?.metadata?.labels?.platform
const service = entity?.metadata?.labels?.service
return `/services/${platform}/${service}/app.yml`
}
// Get the qontract data on load
useEffect(() => {
const variables = { path: getQontractPath() };
request(proxyUrl, query, variables)
.then((data: any) => {
setLoaded(true)
setResult(data)
})
.catch((_error) => {
setError(true)
});
}, []);
return { result, loaded, error }
}
export default QueryQontract;