|
| 1 | +import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; |
| 2 | +import type { FederatedGraph, Subgraph } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_pb'; |
| 3 | +import type { BaseCommandOptions } from '../../core/types/types.js'; |
| 4 | +import { getBaseHeaders } from '../../core/config.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Retrieve user information [email] and [organization name] |
| 8 | + */ |
| 9 | +export async function fetchUserInfo(client: BaseCommandOptions['client']) { |
| 10 | + try { |
| 11 | + const response = await client.platform.whoAmI( |
| 12 | + {}, |
| 13 | + { |
| 14 | + headers: getBaseHeaders(), |
| 15 | + }, |
| 16 | + ); |
| 17 | + |
| 18 | + switch (response.response?.code) { |
| 19 | + case EnumStatusCode.OK: { |
| 20 | + return { |
| 21 | + userInfo: response, |
| 22 | + error: null, |
| 23 | + }; |
| 24 | + } |
| 25 | + default: { |
| 26 | + return { |
| 27 | + userInfo: null, |
| 28 | + error: new Error(response.response?.details ?? 'An unknown error occurred.'), |
| 29 | + }; |
| 30 | + } |
| 31 | + } |
| 32 | + } catch (err) { |
| 33 | + return { |
| 34 | + userInfo: null, |
| 35 | + error: err instanceof Error ? err : new Error('An unknown error occurred.'), |
| 36 | + }; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Retrieve onboarding record. Provides information about allowed [status]: |
| 42 | + * [error] | [not-allowed] | [ok] |
| 43 | + * If record exists, returns [onboarding] metadata. |
| 44 | + */ |
| 45 | +export async function checkExistingOnboarding(client: BaseCommandOptions['client']) { |
| 46 | + const { response, finishedAt, enabled } = await client.platform.getOnboarding( |
| 47 | + {}, |
| 48 | + { |
| 49 | + headers: getBaseHeaders(), |
| 50 | + }, |
| 51 | + ); |
| 52 | + |
| 53 | + if (response?.code !== EnumStatusCode.OK) { |
| 54 | + return { |
| 55 | + error: new Error(response?.details ?? 'Failed to fetch onboarding metadata.'), |
| 56 | + status: 'error', |
| 57 | + } as const; |
| 58 | + } |
| 59 | + |
| 60 | + if (!enabled) { |
| 61 | + return { |
| 62 | + status: 'not-allowed', |
| 63 | + } as const; |
| 64 | + } |
| 65 | + |
| 66 | + return { |
| 67 | + onboarding: { |
| 68 | + finishedAt, |
| 69 | + }, |
| 70 | + status: 'ok', |
| 71 | + } as const; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Retrieves federated graph by [name] *demo*. Missing federated graph |
| 76 | + * is a valid state. |
| 77 | + */ |
| 78 | +export async function fetchFederatedGraphByName( |
| 79 | + client: BaseCommandOptions['client'], |
| 80 | + { name, namespace }: { name: string; namespace: string }, |
| 81 | +) { |
| 82 | + const { response, graph, subgraphs } = await client.platform.getFederatedGraphByName( |
| 83 | + { |
| 84 | + name, |
| 85 | + namespace, |
| 86 | + }, |
| 87 | + { |
| 88 | + headers: getBaseHeaders(), |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + switch (response?.code) { |
| 93 | + case EnumStatusCode.OK: { |
| 94 | + return { data: { graph, subgraphs }, error: null }; |
| 95 | + } |
| 96 | + case EnumStatusCode.ERR_NOT_FOUND: { |
| 97 | + return { data: null, error: null }; |
| 98 | + } |
| 99 | + default: { |
| 100 | + return { |
| 101 | + data: null, |
| 102 | + error: new Error(response?.details ?? 'An unknown error occured'), |
| 103 | + }; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Cleans up the federated graph by [name] _demo_ and its related |
| 110 | + * subgraphs. |
| 111 | + */ |
| 112 | +export async function cleanUpFederatedGraph( |
| 113 | + client: BaseCommandOptions['client'], |
| 114 | + graphData: { |
| 115 | + graph: FederatedGraph; |
| 116 | + subgraphs: Subgraph[]; |
| 117 | + }, |
| 118 | +) { |
| 119 | + const subgraphDeleteResponses = await Promise.all( |
| 120 | + graphData.subgraphs.map(({ name, namespace }) => |
| 121 | + client.platform.deleteFederatedSubgraph( |
| 122 | + { |
| 123 | + namespace, |
| 124 | + subgraphName: name, |
| 125 | + disableResolvabilityValidation: false, |
| 126 | + }, |
| 127 | + { |
| 128 | + headers: getBaseHeaders(), |
| 129 | + }, |
| 130 | + ), |
| 131 | + ), |
| 132 | + ); |
| 133 | + |
| 134 | + const failedSubgraphDeleteResponses = subgraphDeleteResponses.filter( |
| 135 | + ({ response }) => response?.code !== EnumStatusCode.OK, |
| 136 | + ); |
| 137 | + |
| 138 | + if (failedSubgraphDeleteResponses.length > 0) { |
| 139 | + return { |
| 140 | + error: new Error( |
| 141 | + failedSubgraphDeleteResponses.map(({ response }) => response?.details ?? 'Unknown error occurred.').join('. '), |
| 142 | + ), |
| 143 | + }; |
| 144 | + } |
| 145 | + |
| 146 | + const federatedGraphDeleteResponse = await client.platform.deleteFederatedGraph( |
| 147 | + { |
| 148 | + name: graphData.graph.name, |
| 149 | + namespace: graphData.graph.namespace, |
| 150 | + }, |
| 151 | + { |
| 152 | + headers: getBaseHeaders(), |
| 153 | + }, |
| 154 | + ); |
| 155 | + |
| 156 | + switch (federatedGraphDeleteResponse.response?.code) { |
| 157 | + case EnumStatusCode.OK: { |
| 158 | + return { |
| 159 | + error: null, |
| 160 | + }; |
| 161 | + } |
| 162 | + default: { |
| 163 | + return { |
| 164 | + error: new Error(federatedGraphDeleteResponse.response?.details ?? 'Unknown error occurred.'), |
| 165 | + }; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * Creates federated graph using default [name] and [namespace], with pre-defined |
| 172 | + * [labelMatcher] which identify the graph as _demo_. |
| 173 | + */ |
| 174 | +export async function createFederatedGraph( |
| 175 | + client: BaseCommandOptions['client'], |
| 176 | + options: { |
| 177 | + name: string; |
| 178 | + namespace: string; |
| 179 | + labelMatcher: string; |
| 180 | + routingUrl: URL; |
| 181 | + }, |
| 182 | +) { |
| 183 | + const createFedGraphResponse = await client.platform.createFederatedGraph( |
| 184 | + { |
| 185 | + name: options.name, |
| 186 | + namespace: options.namespace, |
| 187 | + routingUrl: options.routingUrl.toString(), |
| 188 | + labelMatchers: [options.labelMatcher], |
| 189 | + }, |
| 190 | + { |
| 191 | + headers: getBaseHeaders(), |
| 192 | + }, |
| 193 | + ); |
| 194 | + |
| 195 | + switch (createFedGraphResponse.response?.code) { |
| 196 | + case EnumStatusCode.OK: { |
| 197 | + return { error: null }; |
| 198 | + } |
| 199 | + default: { |
| 200 | + return { |
| 201 | + error: new Error(createFedGraphResponse.response?.details ?? 'An unknown error occured'), |
| 202 | + }; |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments