Skip to content

Commit 03c907a

Browse files
saihajgithub-actions[bot]
andauthoredMay 27, 2024··
feat: publish to graph network (#1670)
* feat: publish to graph network * chore(dependencies): updated changesets for modified dependencies * allow passing url * fix copy * update readme * fix lint --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 2f59fc5 commit 03c907a

File tree

6 files changed

+146
-5
lines changed

6 files changed

+146
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphprotocol/graph-cli": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`open@8.4.2` ↗︎](https://www.npmjs.com/package/open/v/8.4.2) (to `dependencies`)

‎.changeset/bright-readers-remember.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
'@graphprotocol/graph-cli': minor
3+
---
4+
5+
Introduce `graph publish` command.
6+
7+
Now you can publish your subgraphs directly from the CLI. This command will build your subgraph, deploy, prompt you to add metadata and then sign the transaction to publish it to the Graph Network.
8+
9+
1. Build the subgraph and publish it to the network.
10+
11+
```sh
12+
graph publish
13+
```
14+
15+
2. Provide a IPFS Hash for the subgraph and publish it to the network.
16+
17+
```sh
18+
graph publish --ipfs <ipfs-hash>
19+
```
20+
21+
3. You can use a custom webapp url for deploying.
22+
23+
```sh
24+
graph publish --webapp-url <webapp-url>
25+
```

‎packages/cli/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ As of today, the command line interface supports the following commands:
2222
binary in order to test a subgraph.
2323
- `graph add` - Adds a new datasource to the yaml file and writes the necessary changes to other
2424
files - schema.graphql, abi and mapping.
25+
- `graph publish` - Publishes the subgraph to the Graph Network.
2526

2627
## How It Works
2728

‎packages/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"ipfs-http-client": "55.0.0",
4949
"jayson": "4.0.0",
5050
"js-yaml": "3.14.1",
51+
"open": "8.4.2",
5152
"prettier": "3.0.3",
5253
"semver": "7.4.0",
5354
"sync-request": "6.1.0",

‎packages/cli/src/commands/publish.ts

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import chalk from 'chalk';
2+
import { print } from 'gluegun';
3+
import open from 'open';
4+
import { Args, Command, Flags, ux } from '@oclif/core';
5+
import { createCompiler } from '../command-helpers/compiler';
6+
import * as DataSourcesExtractor from '../command-helpers/data-sources';
7+
import { DEFAULT_IPFS_URL } from '../command-helpers/ipfs';
8+
import Protocol from '../protocols';
9+
10+
export default class PublishCommand extends Command {
11+
static description = 'Publish to the Graph Network';
12+
13+
static args = {
14+
'subgraph-manifest': Args.string({
15+
default: 'subgraph.yaml',
16+
}),
17+
};
18+
19+
static flags = {
20+
help: Flags.help({
21+
char: 'h',
22+
}),
23+
ipfs: Flags.string({
24+
summary: 'Upload build results to an IPFS node.',
25+
char: 'i',
26+
default: DEFAULT_IPFS_URL,
27+
}),
28+
'ipfs-hash': Flags.string({
29+
summary: 'IPFS hash of the subgraph manifest to deploy.',
30+
required: false,
31+
}),
32+
'webapp-url': Flags.string({
33+
summary: 'URL of the web UI you want to use to deploy.',
34+
required: false,
35+
default: 'https://cli.thegraph.com/publish',
36+
}),
37+
};
38+
39+
/**
40+
* Prompt the user to open up the browser to continue publishing the subgraph
41+
*/
42+
async publishWithBrowser({ ipfsHash, webapp }: { ipfsHash: string; webapp: string }) {
43+
const answer = await ux.prompt(
44+
`Press ${chalk.green(
45+
'y',
46+
)} (or any key) to open up the browser to continue publishing or ${chalk.yellow(
47+
'q',
48+
)} to exit`,
49+
);
50+
51+
if (answer.toLowerCase() === 'q') {
52+
this.exit(0);
53+
}
54+
55+
const URL = `${webapp}?id=${ipfsHash}`;
56+
57+
print.success(
58+
`Finalize the publish of the subgraph from the Graph CLI publish page. Opening up the browser to continue publishing at ${URL}`,
59+
);
60+
await open(URL);
61+
return;
62+
}
63+
64+
async run() {
65+
const {
66+
args: { 'subgraph-manifest': manifest },
67+
flags: { 'ipfs-hash': ipfsHash, 'webapp-url': webUiUrl, ipfs },
68+
} = await this.parse(PublishCommand);
69+
70+
if (ipfsHash) {
71+
await this.publishWithBrowser({ ipfsHash, webapp: webUiUrl });
72+
return;
73+
}
74+
75+
let protocol;
76+
try {
77+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest);
78+
protocol = Protocol.fromDataSources(dataSourcesAndTemplates);
79+
} catch (e) {
80+
this.error(e, { exit: 1 });
81+
}
82+
83+
const compiler = createCompiler(manifest, {
84+
ipfs,
85+
outputDir: 'build/',
86+
outputFormat: 'wasm',
87+
skipMigrations: false,
88+
protocol,
89+
});
90+
91+
// Exit with an error code if the compiler couldn't be created
92+
if (!compiler) {
93+
this.exit(1);
94+
return;
95+
}
96+
const result = await compiler.compile({ validate: true });
97+
if (result === undefined || result === false) {
98+
// Compilation failed, not deploying.
99+
process.exitCode = 1;
100+
return;
101+
}
102+
103+
await this.publishWithBrowser({ ipfsHash: result, webapp: webUiUrl });
104+
return;
105+
}
106+
}

‎pnpm-lock.yaml

+8-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.