@graphprotocol/graph-cli 0.37.4 → 0.37.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/bin.js +6 -0
  3. package/dist/cli.js +26 -0
  4. package/dist/codegen/schema.js +244 -0
  5. package/dist/codegen/schema.test.js +455 -0
  6. package/dist/codegen/template.js +66 -0
  7. package/dist/codegen/types/conversions.js +335 -0
  8. package/dist/codegen/types/index.js +71 -0
  9. package/dist/codegen/types/index.test.js +563 -0
  10. package/dist/codegen/typescript.js +200 -0
  11. package/dist/codegen/util.js +40 -0
  12. package/dist/codegen/util.test.js +93 -0
  13. package/dist/command-helpers/abi.js +76 -0
  14. package/dist/command-helpers/auth.js +76 -0
  15. package/dist/command-helpers/compiler.js +66 -0
  16. package/dist/command-helpers/data-sources.js +29 -0
  17. package/dist/command-helpers/fs.js +9 -0
  18. package/dist/command-helpers/gluegun.js +48 -0
  19. package/dist/command-helpers/ipfs.js +5 -0
  20. package/dist/command-helpers/jsonrpc.js +27 -0
  21. package/dist/command-helpers/network.js +90 -0
  22. package/dist/command-helpers/network.test.js +86 -0
  23. package/dist/command-helpers/node.js +40 -0
  24. package/dist/command-helpers/scaffold.js +110 -0
  25. package/dist/command-helpers/spinner.js +80 -0
  26. package/dist/command-helpers/studio.js +14 -0
  27. package/dist/command-helpers/studio.test.js +41 -0
  28. package/dist/command-helpers/subgraph.js +22 -0
  29. package/dist/command-helpers/version.js +66 -0
  30. package/dist/command-helpers/version.test.js +186 -0
  31. package/dist/commands/add.js +187 -0
  32. package/dist/commands/auth.js +122 -0
  33. package/dist/commands/build.js +136 -0
  34. package/dist/commands/codegen.js +128 -0
  35. package/dist/commands/create.js +94 -0
  36. package/dist/commands/deploy.js +335 -0
  37. package/dist/commands/init.js +800 -0
  38. package/dist/commands/local.js +380 -0
  39. package/dist/commands/remove.js +95 -0
  40. package/dist/commands/test.js +293 -0
  41. package/dist/compiler/asc.js +83 -0
  42. package/dist/compiler/index.js +466 -0
  43. package/dist/debug.js +16 -0
  44. package/dist/migrations/mapping_api_version_0_0_1.js +83 -0
  45. package/dist/migrations/mapping_api_version_0_0_2.js +83 -0
  46. package/dist/migrations/mapping_api_version_0_0_3.js +83 -0
  47. package/dist/migrations/mapping_api_version_0_0_4.js +85 -0
  48. package/dist/migrations/mapping_api_version_0_0_5.js +85 -0
  49. package/dist/migrations/spec_version_0_0_2.js +45 -0
  50. package/dist/migrations/spec_version_0_0_3.js +50 -0
  51. package/dist/migrations/util/load-manifest.js +16 -0
  52. package/dist/migrations/util/versions.js +28 -0
  53. package/dist/migrations.js +54 -0
  54. package/dist/protocols/arweave/manifest.graphql +57 -0
  55. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  56. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  57. package/dist/protocols/arweave/subgraph.js +23 -0
  58. package/dist/protocols/contract.js +2 -0
  59. package/dist/protocols/cosmos/manifest.graphql +76 -0
  60. package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
  61. package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
  62. package/dist/protocols/cosmos/subgraph.js +28 -0
  63. package/dist/protocols/ethereum/abi.js +138 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +474 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +325 -0
  66. package/dist/protocols/ethereum/codegen/template.js +52 -0
  67. package/dist/protocols/ethereum/contract.js +22 -0
  68. package/dist/protocols/ethereum/manifest.graphql +94 -0
  69. package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
  70. package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
  71. package/dist/protocols/ethereum/subgraph.js +172 -0
  72. package/dist/protocols/ethereum/type-generator.js +123 -0
  73. package/dist/protocols/index.js +266 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +53 -0
  75. package/dist/protocols/near/contract.js +41 -0
  76. package/dist/protocols/near/manifest.graphql +64 -0
  77. package/dist/protocols/near/scaffold/manifest.js +16 -0
  78. package/dist/protocols/near/scaffold/mapping.js +38 -0
  79. package/dist/protocols/near/subgraph.js +23 -0
  80. package/dist/protocols/subgraph.js +2 -0
  81. package/dist/protocols/substreams/manifest.graphql +45 -0
  82. package/dist/protocols/substreams/scaffold/manifest.js +12 -0
  83. package/dist/protocols/substreams/subgraph.js +23 -0
  84. package/dist/scaffold/cosmos.test.js +82 -0
  85. package/dist/scaffold/ethereum.test.js +496 -0
  86. package/dist/scaffold/index.js +133 -0
  87. package/dist/scaffold/mapping.js +57 -0
  88. package/dist/scaffold/near.test.js +85 -0
  89. package/dist/scaffold/schema.js +86 -0
  90. package/dist/scaffold/tests.js +179 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +243 -0
  93. package/dist/type-generator.js +225 -0
  94. package/dist/validation/contract.js +44 -0
  95. package/dist/validation/index.js +10 -0
  96. package/dist/validation/manifest.js +266 -0
  97. package/dist/validation/schema.js +768 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/package.json +1 -1
@@ -0,0 +1,380 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const child_process_1 = require("child_process");
7
+ const http_1 = __importDefault(require("http"));
8
+ const net_1 = __importDefault(require("net"));
9
+ const path_1 = __importDefault(require("path"));
10
+ const chalk_1 = __importDefault(require("chalk"));
11
+ const docker_compose_1 = __importDefault(require("docker-compose"));
12
+ const strip_ansi_1 = __importDefault(require("strip-ansi"));
13
+ const tmp_promise_1 = __importDefault(require("tmp-promise"));
14
+ const gluegun_1 = require("../command-helpers/gluegun");
15
+ const spinner_1 = require("../command-helpers/spinner");
16
+ // Clean up temporary files even when an uncaught exception occurs
17
+ tmp_promise_1.default.setGracefulCleanup();
18
+ const HELP = `
19
+ ${chalk_1.default.bold('graph local')} [options] ${chalk_1.default.bold('<local-command>')}
20
+
21
+ Options:
22
+
23
+ -h, --help Show usage information
24
+ --node-logs Print the Graph Node logs (optional)
25
+ --ethereum-logs Print the Ethereum logs (optional)
26
+ --compose-file <file> Custom Docker Compose file for additional services (optional)
27
+ --node-image <image> Custom Graph Node image to test against (default: graphprotocol/graph-node:latest)
28
+ --standalone-node <cmd> Use a standalone Graph Node outside Docker Compose (optional)
29
+ --standalone-node-args Custom arguments to be passed to the standalone Graph Node (optional)
30
+ --skip-wait-for-ipfs Don't wait for IPFS to be up at localhost:15001 (optional)
31
+ --skip-wait-for-ethereum Don't wait for Ethereum to be up at localhost:18545 (optional)
32
+ --skip-wait-for-postgres Don't wait for Postgres to be up at localhost:15432 (optional)
33
+ --timeout Time to wait for service containers. (optional, defaults to 120000 milliseconds)
34
+ `;
35
+ exports.default = {
36
+ description: 'Runs local tests against a Graph Node environment (using Ganache by default)',
37
+ run: async (toolbox) => {
38
+ // Obtain tools
39
+ const { filesystem, print } = toolbox;
40
+ // Parse CLI parameters
41
+ let { composeFile, ethereumLogs, h, help, nodeImage, nodeLogs, skipWaitForEthereum, skipWaitForIpfs, skipWaitForPostgres, standaloneNode, standaloneNodeArgs, timeout, } = toolbox.parameters.options;
42
+ // Support both short and long option variants
43
+ help ||= h;
44
+ // Extract test command
45
+ const params = (0, gluegun_1.fixParameters)(toolbox.parameters, {
46
+ ethereumLogs,
47
+ h,
48
+ help,
49
+ nodeLogs,
50
+ skipWaitForEthereum,
51
+ skipWaitForIpfs,
52
+ skipWaitForPostgres,
53
+ });
54
+ // Show help text if requested
55
+ if (help) {
56
+ print.info(HELP);
57
+ return;
58
+ }
59
+ if (params.length == 0) {
60
+ print.error(`Test command not provided as the last argument`);
61
+ process.exitCode = 1;
62
+ return;
63
+ }
64
+ const testCommand = params[0];
65
+ // Obtain the Docker Compose file for services that the tests run against
66
+ composeFile ||= path_1.default.join(__dirname, '..', '..', 'resources', 'test', standaloneNode ? 'docker-compose-standalone-node.yml' : 'docker-compose.yml');
67
+ // parse timeout. Defaults to 120 seconds
68
+ timeout = Math.abs(parseInt(timeout)) || 120000;
69
+ if (!filesystem.exists(composeFile)) {
70
+ print.error(`Docker Compose file \`${composeFile}\` not found`);
71
+ process.exitCode = 1;
72
+ return;
73
+ }
74
+ // Create temporary directory to operate in
75
+ const { path: tempdir } = await tmp_promise_1.default.dir({ prefix: 'graph-test', unsafeCleanup: true });
76
+ try {
77
+ await configureTestEnvironment(toolbox, tempdir, composeFile, nodeImage);
78
+ }
79
+ catch (e) {
80
+ process.exitCode = 1;
81
+ return;
82
+ }
83
+ // Bring up test environment
84
+ try {
85
+ await startTestEnvironment(tempdir);
86
+ }
87
+ catch (e) {
88
+ print.error(e);
89
+ process.exitCode = 1;
90
+ return;
91
+ }
92
+ // Wait for test environment to come up
93
+ try {
94
+ await waitForTestEnvironment({
95
+ skipWaitForEthereum,
96
+ skipWaitForIpfs,
97
+ skipWaitForPostgres,
98
+ timeout,
99
+ });
100
+ }
101
+ catch (e) {
102
+ await stopTestEnvironment(tempdir);
103
+ process.exitCode = 1;
104
+ return;
105
+ }
106
+ // Bring up Graph Node separately, if a standalone node is used
107
+ let nodeProcess;
108
+ const nodeOutputChunks = [];
109
+ if (standaloneNode) {
110
+ try {
111
+ nodeProcess = await startGraphNode(standaloneNode, standaloneNodeArgs, nodeOutputChunks);
112
+ }
113
+ catch (e) {
114
+ toolbox.print.error('');
115
+ toolbox.print.error(' Graph Node');
116
+ toolbox.print.error(' ----------');
117
+ toolbox.print.error(indent(' ', Buffer.concat(nodeOutputChunks).toString('utf-8')));
118
+ toolbox.print.error('');
119
+ await stopTestEnvironment(tempdir);
120
+ process.exitCode = 1;
121
+ return;
122
+ }
123
+ }
124
+ // Wait for Graph Node to come up
125
+ try {
126
+ await waitForGraphNode(timeout);
127
+ }
128
+ catch (e) {
129
+ toolbox.print.error('');
130
+ toolbox.print.error(' Graph Node');
131
+ toolbox.print.error(' ----------');
132
+ toolbox.print.error(indent(' ', await collectGraphNodeLogs(tempdir, standaloneNode, nodeOutputChunks)));
133
+ toolbox.print.error('');
134
+ await stopTestEnvironment(tempdir);
135
+ process.exitCode = 1;
136
+ return;
137
+ }
138
+ // Run tests
139
+ const result = await runTests(testCommand);
140
+ // Bring down Graph Node, if a standalone node is used
141
+ if (nodeProcess) {
142
+ try {
143
+ await stopGraphNode(nodeProcess);
144
+ }
145
+ catch (e) {
146
+ // do nothing (the spinner already logs the problem)
147
+ }
148
+ }
149
+ if (result.exitCode == 0) {
150
+ toolbox.print.success('✔ Tests passed');
151
+ }
152
+ else {
153
+ toolbox.print.error('✖ Tests failed');
154
+ }
155
+ // Capture logs
156
+ nodeLogs =
157
+ nodeLogs || result.exitCode !== 0
158
+ ? await collectGraphNodeLogs(tempdir, standaloneNode, nodeOutputChunks)
159
+ : undefined;
160
+ ethereumLogs = ethereumLogs ? await collectEthereumLogs(tempdir) : undefined;
161
+ // Bring down the test environment
162
+ try {
163
+ await stopTestEnvironment(tempdir);
164
+ }
165
+ catch (e) {
166
+ // do nothing (the spinner already logs the problem)
167
+ }
168
+ if (nodeLogs) {
169
+ toolbox.print.info('');
170
+ toolbox.print.info(' Graph node');
171
+ toolbox.print.info(' ----------');
172
+ toolbox.print.info('');
173
+ toolbox.print.info(indent(' ', nodeLogs));
174
+ }
175
+ if (ethereumLogs) {
176
+ toolbox.print.info('');
177
+ toolbox.print.info(' Ethereum');
178
+ toolbox.print.info(' --------');
179
+ toolbox.print.info('');
180
+ toolbox.print.info(indent(' ', ethereumLogs));
181
+ }
182
+ // Always print the test output
183
+ toolbox.print.info('');
184
+ toolbox.print.info(' Output');
185
+ toolbox.print.info(' ------');
186
+ toolbox.print.info('');
187
+ toolbox.print.info(indent(' ', result.output));
188
+ // Propagate the exit code from the test run
189
+ process.exitCode = result.exitCode;
190
+ },
191
+ };
192
+ /**
193
+ * Indents all lines of a string
194
+ */
195
+ const indent = (indentation, str) => str
196
+ .split('\n')
197
+ .map(s => `${indentation}${s}`)
198
+ // Remove whitespace from empty lines
199
+ .map(s => s.replace(/^\s+$/g, ''))
200
+ .join('\n');
201
+ const configureTestEnvironment = async (toolbox, tempdir, composeFile, nodeImage) => await (0, spinner_1.withSpinner)(`Configure test environment`, `Failed to configure test environment`, `Warnings configuring test environment`, async () => {
202
+ // Temporary compose file
203
+ const tempComposeFile = path_1.default.join(tempdir, 'compose', 'docker-compose.yml');
204
+ // Copy the compose file to the temporary directory
205
+ toolbox.filesystem.copy(composeFile, tempComposeFile);
206
+ // Substitute the graph-node image with the custom one, if appropriate
207
+ if (nodeImage) {
208
+ await toolbox.patching.replace(tempComposeFile, 'graphprotocol/graph-node:latest', nodeImage);
209
+ }
210
+ });
211
+ const waitFor = async (timeout, testFn) => {
212
+ const deadline = Date.now() + timeout;
213
+ let error = undefined;
214
+ return new Promise((resolve, reject) => {
215
+ const check = async () => {
216
+ if (Date.now() > deadline) {
217
+ reject(error);
218
+ }
219
+ else {
220
+ try {
221
+ const result = await testFn();
222
+ resolve(result);
223
+ }
224
+ catch (e) {
225
+ error = e;
226
+ setTimeout(check, 500);
227
+ }
228
+ }
229
+ };
230
+ setTimeout(check, 0);
231
+ });
232
+ };
233
+ const startTestEnvironment = async (tempdir) => await (0, spinner_1.withSpinner)(`Start test environment`, `Failed to start test environment`, `Warnings starting test environment`, async (_spinner) => {
234
+ // Bring up the test environment
235
+ await docker_compose_1.default.upAll({
236
+ cwd: path_1.default.join(tempdir, 'compose'),
237
+ });
238
+ });
239
+ const waitForTestEnvironment = async ({ skipWaitForEthereum, skipWaitForIpfs, skipWaitForPostgres, timeout, }) => await (0, spinner_1.withSpinner)(`Wait for test environment`, `Failed to wait for test environment`, `Warnings waiting for test environment`, async (spinner) => {
240
+ // Wait 10s for IPFS (if desired)
241
+ if (skipWaitForIpfs) {
242
+ (0, spinner_1.step)(spinner, 'Skip waiting for IPFS');
243
+ }
244
+ else {
245
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
246
+ http_1.default
247
+ .get('http://localhost:15001/api/v0/version', () => {
248
+ resolve();
249
+ })
250
+ .on('error', e => {
251
+ reject(new Error(`Could not connect to IPFS: ${e}`));
252
+ });
253
+ }));
254
+ (0, spinner_1.step)(spinner, 'IPFS is up');
255
+ }
256
+ // Wait 10s for Ethereum (if desired)
257
+ if (skipWaitForEthereum) {
258
+ (0, spinner_1.step)(spinner, 'Skip waiting for Ethereum');
259
+ }
260
+ else {
261
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
262
+ http_1.default
263
+ .get('http://localhost:18545', () => {
264
+ resolve();
265
+ })
266
+ .on('error', e => {
267
+ reject(new Error(`Could not connect to Ethereum: ${e}`));
268
+ });
269
+ }));
270
+ (0, spinner_1.step)(spinner, 'Ethereum is up');
271
+ }
272
+ // Wait 10s for Postgres (if desired)
273
+ if (skipWaitForPostgres) {
274
+ (0, spinner_1.step)(spinner, 'Skip waiting for Postgres');
275
+ }
276
+ else {
277
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
278
+ try {
279
+ const socket = net_1.default.connect(15432, 'localhost', () => resolve());
280
+ socket.on('error', e => reject(new Error(`Could not connect to Postgres: ${e}`)));
281
+ socket.end();
282
+ }
283
+ catch (e) {
284
+ reject(new Error(`Could not connect to Postgres: ${e}`));
285
+ }
286
+ }));
287
+ (0, spinner_1.step)(spinner, 'Postgres is up');
288
+ }
289
+ });
290
+ const stopTestEnvironment = async (tempdir) => await (0, spinner_1.withSpinner)(`Stop test environment`, `Failed to stop test environment`, `Warnings stopping test environment`, async () => {
291
+ // Our containers do not respond quickly to the SIGTERM which `down` tries before timing out
292
+ // and killing them, so speed things up by sending a SIGKILL right away.
293
+ try {
294
+ await docker_compose_1.default.kill({ cwd: path_1.default.join(tempdir, 'compose') });
295
+ }
296
+ catch (e) {
297
+ // Do nothing, we will just try to run 'down'
298
+ // to bring down the environment
299
+ }
300
+ await docker_compose_1.default.down({ cwd: path_1.default.join(tempdir, 'compose') });
301
+ });
302
+ const startGraphNode = async (standaloneNode, standaloneNodeArgs, nodeOutputChunks) => await (0, spinner_1.withSpinner)(`Start Graph node`, `Failed to start Graph node`, `Warnings starting Graph node`, async (spinner) => {
303
+ const defaultArgs = [
304
+ '--ipfs',
305
+ 'localhost:15001',
306
+ '--postgres-url',
307
+ 'postgresql://graph:let-me-in@localhost:15432/graph',
308
+ '--ethereum-rpc',
309
+ 'test:http://localhost:18545',
310
+ '--http-port',
311
+ '18000',
312
+ '--ws-port',
313
+ '18001',
314
+ '--admin-port',
315
+ '18020',
316
+ '--index-node-port',
317
+ '18030',
318
+ '--metrics-port',
319
+ '18040',
320
+ ];
321
+ const defaultEnv = {
322
+ GRAPH_LOG: 'debug',
323
+ GRAPH_MAX_API_VERSION: '0.0.5',
324
+ };
325
+ const args = standaloneNodeArgs ? standaloneNodeArgs.split(' ') : defaultArgs;
326
+ const env = { ...defaultEnv, ...process.env };
327
+ const nodeProcess = (0, child_process_1.spawn)(standaloneNode, args, {
328
+ cwd: process.cwd(),
329
+ env,
330
+ });
331
+ (0, spinner_1.step)(spinner, 'Graph node:', String(nodeProcess.spawnargs.join(' ')));
332
+ nodeProcess.stdout.on('data', data => nodeOutputChunks.push(Buffer.from(data)));
333
+ nodeProcess.stderr.on('data', data => nodeOutputChunks.push(Buffer.from(data)));
334
+ nodeProcess.on('error', e => {
335
+ nodeOutputChunks.push(Buffer.from(String(e), 'utf-8'));
336
+ });
337
+ // Return the node child process
338
+ return nodeProcess;
339
+ });
340
+ const waitForGraphNode = async (timeout) => await (0, spinner_1.withSpinner)(`Wait for Graph node`, `Failed to wait for Graph node`, `Warnings waiting for Graph node`, async () => {
341
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
342
+ http_1.default
343
+ .get('http://localhost:18000', { timeout }, () => resolve())
344
+ .on('error', e => reject(e));
345
+ }));
346
+ });
347
+ const stopGraphNode = async (nodeProcess) => await (0, spinner_1.withSpinner)(`Stop Graph node`, `Failed to stop Graph node`, `Warnings stopping Graph node`, async () => {
348
+ nodeProcess.kill(9);
349
+ });
350
+ const collectGraphNodeLogs = async (tempdir, standaloneNode, nodeOutputChunks) => {
351
+ if (standaloneNode) {
352
+ // Pull the logs from the captured output
353
+ return (0, strip_ansi_1.default)(Buffer.concat(nodeOutputChunks).toString('utf-8'));
354
+ }
355
+ // Pull the logs from docker compose
356
+ const logs = await docker_compose_1.default.logs('graph-node', {
357
+ follow: false,
358
+ cwd: path_1.default.join(tempdir, 'compose'),
359
+ });
360
+ return (0, strip_ansi_1.default)(logs.out.trim()).replace(/graph-node_1 {2}\| /g, '');
361
+ };
362
+ const collectEthereumLogs = async (tempdir) => {
363
+ const logs = await docker_compose_1.default.logs('ethereum', {
364
+ follow: false,
365
+ cwd: path_1.default.join(tempdir, 'compose'),
366
+ });
367
+ return (0, strip_ansi_1.default)(logs.out.trim()).replace(/ethereum_1 {2}\| /g, '');
368
+ };
369
+ const runTests = async (testCommand) => await (0, spinner_1.withSpinner)(`Run tests`, `Failed to run tests`, `Warnings running tests`, async () => new Promise(resolve => {
370
+ const output = [];
371
+ const testProcess = (0, child_process_1.spawn)(String(testCommand), { shell: true });
372
+ testProcess.stdout.on('data', data => output.push(Buffer.from(data)));
373
+ testProcess.stderr.on('data', data => output.push(Buffer.from(data)));
374
+ testProcess.on('close', code => {
375
+ resolve({
376
+ exitCode: code,
377
+ output: Buffer.concat(output).toString('utf-8'),
378
+ });
379
+ });
380
+ }));
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const url_1 = require("url");
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const auth_1 = require("../command-helpers/auth");
9
+ const jsonrpc_1 = require("../command-helpers/jsonrpc");
10
+ const node_1 = require("../command-helpers/node");
11
+ const HELP = `
12
+ ${chalk_1.default.bold('graph remove')} ${chalk_1.default.dim('[options]')} ${chalk_1.default.bold('<subgraph-name>')}
13
+
14
+ ${chalk_1.default.dim('Options:')}
15
+
16
+ --access-token <token> Graph access token
17
+ -h, --help Show usage information
18
+ -g, --node <url> Graph node to create the subgraph in
19
+ `;
20
+ exports.default = {
21
+ description: 'Unregisters a subgraph name',
22
+ run: async (toolbox) => {
23
+ // Obtain tools
24
+ const { print } = toolbox;
25
+ // Read CLI parameters
26
+ let { accessToken, g, h, help, node } = toolbox.parameters.options;
27
+ const subgraphName = toolbox.parameters.first;
28
+ // Support both long and short option variants
29
+ help ||= h;
30
+ node ||= g;
31
+ // Show help text if requested
32
+ if (help) {
33
+ print.info(HELP);
34
+ return;
35
+ }
36
+ // Validate the subgraph name
37
+ if (!subgraphName) {
38
+ print.error('No subgraph name provided');
39
+ print.info(HELP);
40
+ process.exitCode = 1;
41
+ return;
42
+ }
43
+ // Validate node
44
+ if (!node) {
45
+ print.error(`No Graph node provided`);
46
+ print.info(HELP);
47
+ process.exitCode = 1;
48
+ return;
49
+ }
50
+ try {
51
+ (0, node_1.validateNodeUrl)(node);
52
+ }
53
+ catch (e) {
54
+ print.error(`Graph node "${node}" is invalid: ${e.message}`);
55
+ process.exitCode = 1;
56
+ return;
57
+ }
58
+ const requestUrl = new url_1.URL(node);
59
+ const client = (0, jsonrpc_1.createJsonRpcClient)(requestUrl);
60
+ // Exit with an error code if the client couldn't be created
61
+ if (!client) {
62
+ process.exitCode = 1;
63
+ return;
64
+ }
65
+ // Use the access token, if one is set
66
+ accessToken = await (0, auth_1.identifyDeployKey)(node, accessToken);
67
+ if (accessToken !== undefined && accessToken !== null) {
68
+ // @ts-expect-error options property seems to exist
69
+ client.options.headers = { Authorization: `Bearer ${accessToken}` };
70
+ }
71
+ const spinner = print.spin(`Creating subgraph in Graph node: ${requestUrl}`);
72
+ client.request('subgraph_remove', { name: subgraphName }, (
73
+ // @ts-expect-error TODO: why are the arguments not typed?
74
+ requestError,
75
+ // @ts-expect-error TODO: why are the arguments not typed?
76
+ jsonRpcError,
77
+ // TODO: this argument is unused, but removing it from the commands/create.ts fails the basic-event-handlers tests.
78
+ // I'll therefore leave it in here too until we figure out the weirdness
79
+ // @ts-expect-error TODO: why are the arguments not typed?
80
+ _res) => {
81
+ if (jsonRpcError) {
82
+ spinner.fail(`Error removing the subgraph: ${jsonRpcError.message}`);
83
+ process.exitCode = 1;
84
+ }
85
+ else if (requestError) {
86
+ spinner.fail(`HTTP error removing the subgraph: ${requestError.code}`);
87
+ process.exitCode = 1;
88
+ }
89
+ else {
90
+ spinner.stop();
91
+ print.success(`Removed subgraph: ${subgraphName}`);
92
+ }
93
+ });
94
+ },
95
+ };