/
get-instances.ts
100 lines (95 loc) · 1.97 KB
/
get-instances.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
/**
* Get a list of all enabled and non-system instances, including their flows and alert monitors.
*/
import { GraphQLClient, gql } from "graphql-request";
interface Instance {
id: string;
name: string;
flowConfigs: {
nodes: {
id: string;
flow: {
name: string;
};
monitors: {
nodes: {
id: string;
name: string;
groups: {
nodes: {
id: string;
}[];
};
}[];
};
}[];
};
customer: {
id: string;
name: string;
};
}
interface InstanceResult {
instances: {
nodes: Instance[];
pageInfo: { endCursor: string; hasNextPage: boolean };
};
}
const GET_INSTANCES_QUERY = gql`
query myGetInstancesQuery($cursor: String) {
instances(
isSystem: false
enabled: true
sortBy: { direction: ASC, field: CREATED_AT }
after: $cursor
) {
nodes {
id
name
flowConfigs {
nodes {
id
flow {
name
}
monitors {
nodes {
id
name
groups {
nodes {
id
}
}
}
}
}
}
customer {
id
name
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
`;
const getInstances = async (client: GraphQLClient) => {
let cursor: string = "";
let hasNextPage = false;
let instances: Instance[] = [];
// Loop over pages of all enabled and non-system instances
do {
const result = await client.request<InstanceResult>(GET_INSTANCES_QUERY, {
cursor,
});
instances = [...instances, ...result.instances.nodes];
cursor = result.instances.pageInfo.endCursor;
hasNextPage = result.instances.pageInfo.hasNextPage;
} while (hasNextPage);
return instances;
};
export default getInstances;