|
| 1 | +import { Command } from 'commander'; |
| 2 | +import pc from 'picocolors'; |
| 3 | +import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; |
| 4 | +import inquirer from 'inquirer'; |
| 5 | +import Table from 'cli-table3'; |
| 6 | +import ora from 'ora'; |
| 7 | +import { BaseCommandOptions } from '../../../core/types/types.js'; |
| 8 | +import { getBaseHeaders } from '../../../core/config.js'; |
| 9 | + |
| 10 | +export default (opts: BaseCommandOptions) => { |
| 11 | + const command = new Command('delete'); |
| 12 | + command.description('Deletes a gRPC subgraph on the control plane.'); |
| 13 | + command.argument('<name>', 'The name of the gRPC subgraph to delete.'); |
| 14 | + command.option('-n, --namespace [string]', 'The namespace of the gRPC subgraph.'); |
| 15 | + command.option('-f, --force', 'Flag to force the deletion (skip confirmation).'); |
| 16 | + command.option('--suppress-warnings', 'This flag suppresses any warnings produced by composition.'); |
| 17 | + command.action(async (name, options) => { |
| 18 | + if (!options.force) { |
| 19 | + const deletionConfirmed = await inquirer.prompt({ |
| 20 | + name: 'confirmDeletion', |
| 21 | + type: 'confirm', |
| 22 | + message: `Are you sure you want to delete the gRPC subgraph "${name}"?`, |
| 23 | + }); |
| 24 | + if (!deletionConfirmed.confirmDeletion) { |
| 25 | + process.exitCode = 1; |
| 26 | + return; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + const spinner = ora(`The gRPC subgraph "${name}" is being deleted...`).start(); |
| 31 | + |
| 32 | + const resp = await opts.client.platform.deleteFederatedSubgraph( |
| 33 | + { |
| 34 | + subgraphName: name, |
| 35 | + namespace: options.namespace, |
| 36 | + }, |
| 37 | + { |
| 38 | + headers: getBaseHeaders(), |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + switch (resp.response?.code) { |
| 43 | + case EnumStatusCode.OK: { |
| 44 | + spinner.succeed(`The gRPC subgraph "${name}" was deleted successfully.`); |
| 45 | + if (resp.proposalMatchMessage) { |
| 46 | + console.log(pc.yellow(`Warning: Proposal match failed`)); |
| 47 | + console.log(pc.yellow(resp.proposalMatchMessage)); |
| 48 | + } |
| 49 | + break; |
| 50 | + } |
| 51 | + case EnumStatusCode.ERR_SCHEMA_MISMATCH_WITH_APPROVED_PROPOSAL: { |
| 52 | + spinner.fail(`Failed to delete gRPC subgraph "${name}".`); |
| 53 | + console.log(pc.red(`Error: Proposal match failed`)); |
| 54 | + console.log(pc.red(resp.proposalMatchMessage)); |
| 55 | + break; |
| 56 | + } |
| 57 | + case EnumStatusCode.ERR_SUBGRAPH_COMPOSITION_FAILED: { |
| 58 | + spinner.fail(`The gRPC subgraph "${name}" was deleted but with composition errors.`); |
| 59 | + |
| 60 | + const compositionErrorsTable = new Table({ |
| 61 | + head: [ |
| 62 | + pc.bold(pc.white('FEDERATED_GRAPH_NAME')), |
| 63 | + pc.bold(pc.white('NAMESPACE')), |
| 64 | + pc.bold(pc.white('FEATURE_FLAG')), |
| 65 | + pc.bold(pc.white('ERROR_MESSAGE')), |
| 66 | + ], |
| 67 | + colWidths: [30, 30, 30, 120], |
| 68 | + wordWrap: true, |
| 69 | + }); |
| 70 | + |
| 71 | + console.log( |
| 72 | + pc.red( |
| 73 | + `There were composition errors when composing at least one federated graph related to the` + |
| 74 | + ` gRPC subgraph "${name}".\nThe router will continue to work with the latest valid schema.` + |
| 75 | + `\n${pc.bold('Please check the errors below:')}`, |
| 76 | + ), |
| 77 | + ); |
| 78 | + for (const compositionError of resp.compositionErrors) { |
| 79 | + compositionErrorsTable.push([ |
| 80 | + compositionError.federatedGraphName, |
| 81 | + compositionError.namespace, |
| 82 | + compositionError.featureFlag || '-', |
| 83 | + compositionError.message, |
| 84 | + ]); |
| 85 | + } |
| 86 | + // Don't exit here with 1 because the change was still applied |
| 87 | + console.log(compositionErrorsTable.toString()); |
| 88 | + |
| 89 | + break; |
| 90 | + } |
| 91 | + case EnumStatusCode.ERR_DEPLOYMENT_FAILED: { |
| 92 | + spinner.warn( |
| 93 | + `The gRPC subgraph "${name}" was deleted, but the updated composition could not be deployed.` + |
| 94 | + `\nThis means the updated composition is not accessible to the router.` + |
| 95 | + `\n${pc.bold('Please check the errors below:')}`, |
| 96 | + ); |
| 97 | + |
| 98 | + const deploymentErrorsTable = new Table({ |
| 99 | + head: [ |
| 100 | + pc.bold(pc.white('FEDERATED_GRAPH_NAME')), |
| 101 | + pc.bold(pc.white('NAMESPACE')), |
| 102 | + pc.bold(pc.white('ERROR_MESSAGE')), |
| 103 | + ], |
| 104 | + colWidths: [30, 30, 120], |
| 105 | + wordWrap: true, |
| 106 | + }); |
| 107 | + |
| 108 | + for (const deploymentError of resp.deploymentErrors) { |
| 109 | + deploymentErrorsTable.push([ |
| 110 | + deploymentError.federatedGraphName, |
| 111 | + deploymentError.namespace, |
| 112 | + deploymentError.message, |
| 113 | + ]); |
| 114 | + } |
| 115 | + // Don't exit here with 1 because the change was still applied |
| 116 | + console.log(deploymentErrorsTable.toString()); |
| 117 | + |
| 118 | + break; |
| 119 | + } |
| 120 | + default: { |
| 121 | + spinner.fail(`Failed to delete the gRPC subgraph "${name}".`); |
| 122 | + if (resp.response?.details) { |
| 123 | + console.log(pc.red(pc.bold(resp.response?.details))); |
| 124 | + } |
| 125 | + process.exitCode = 1; |
| 126 | + return; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (!options.suppressWarnings && resp.compositionWarnings.length > 0) { |
| 131 | + const compositionWarningsTable = new Table({ |
| 132 | + head: [ |
| 133 | + pc.bold(pc.white('FEDERATED_GRAPH_NAME')), |
| 134 | + pc.bold(pc.white('NAMESPACE')), |
| 135 | + pc.bold(pc.white('FEATURE_FLAG')), |
| 136 | + pc.bold(pc.white('WARNING_MESSAGE')), |
| 137 | + ], |
| 138 | + colWidths: [30, 30, 30, 120], |
| 139 | + wordWrap: true, |
| 140 | + }); |
| 141 | + |
| 142 | + console.log(pc.yellow(`The following warnings were produced while composing the federated graph:`)); |
| 143 | + for (const compositionWarning of resp.compositionWarnings) { |
| 144 | + compositionWarningsTable.push([ |
| 145 | + compositionWarning.federatedGraphName, |
| 146 | + compositionWarning.namespace, |
| 147 | + compositionWarning.featureFlag || '-', |
| 148 | + compositionWarning.message, |
| 149 | + ]); |
| 150 | + } |
| 151 | + console.log(compositionWarningsTable.toString()); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + return command; |
| 156 | +}; |
0 commit comments