Skip to content

Commit 5b312cf

Browse files
Fix substreams source name handling (#1871)
* handle spkg path better * add tests * fixes #1868
1 parent a4f0745 commit 5b312cf

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

.changeset/angry-ties-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
Fix empty source name for substreams subgraphs #1868

packages/cli/src/commands/init.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const protocolChoices = Array.from(Protocol.availableProtocols().keys());
3232
const initDebugger = debugFactory('graph-cli:commands:init');
3333

3434
const DEFAULT_EXAMPLE_SUBGRAPH = 'ethereum-gravatar';
35+
const DEFAULT_CONTRACT_NAME = 'Contract';
3536

3637
export default class InitCommand extends Command {
3738
static description = 'Creates a new subgraph with basic scaffolding.';
@@ -61,7 +62,7 @@ export default class InitCommand extends Command {
6162
description: 'Creates a scaffold based on an example subgraph.',
6263
// TODO: using a default sets the value and therefore requires not to have --from-contract
6364
// default: 'Contract',
64-
exclusive: ['from-contract'],
65+
exclusive: ['from-contract', 'spkg'],
6566
}),
6667

6768
'contract-name': Flags.string({
@@ -103,7 +104,6 @@ export default class InitCommand extends Command {
103104
summary: 'Network the contract is deployed to.',
104105
description:
105106
'Refer to https://github.com/graphprotocol/networks-registry/ for supported networks',
106-
dependsOn: ['from-contract'],
107107
}),
108108

109109
ipfs: Flags.string({
@@ -143,17 +143,14 @@ export default class InitCommand extends Command {
143143
'The --skip-git flag will be removed in the next major version. By default we will stop initializing a Git repository.',
144144
);
145145
}
146+
if ((!fromContract || !spkgPath) && !network && !fromExample) {
147+
this.error('--network is required when using --from-contract or --spkg');
148+
}
146149

147150
const { node } = chooseNodeUrl({
148151
node: nodeFlag,
149152
});
150153

151-
if (fromContract && fromExample) {
152-
this.error('Only one of "--from-example" and "--from-contract" can be used at a time.', {
153-
exit: 1,
154-
});
155-
}
156-
157154
// Detect git
158155
const git = system.which('git');
159156
if (!git) {
@@ -200,7 +197,7 @@ export default class InitCommand extends Command {
200197

201198
// If all parameters are provided from the command-line,
202199
// go straight to creating the subgraph from an existing contract
203-
if (fromContract && protocol && subgraphName && directory && network && node) {
200+
if ((fromContract || spkgPath) && protocol && subgraphName && directory && network && node) {
204201
const registry = await loadRegistry();
205202
const contractService = new ContractService(registry);
206203

@@ -225,7 +222,7 @@ export default class InitCommand extends Command {
225222
}
226223
} else {
227224
try {
228-
abi = await contractService.getABI(ABI, network, fromContract);
225+
abi = await contractService.getABI(ABI, network, fromContract!);
229226
} catch (e) {
230227
this.exit(1);
231228
}
@@ -237,11 +234,11 @@ export default class InitCommand extends Command {
237234
protocolInstance,
238235
abi,
239236
directory,
240-
source: fromContract,
237+
source: fromContract!,
241238
indexEvents,
242239
network,
243240
subgraphName,
244-
contractName,
241+
contractName: contractName || DEFAULT_CONTRACT_NAME,
245242
node,
246243
startBlock,
247244
spkgPath,
@@ -303,7 +300,7 @@ export default class InitCommand extends Command {
303300
network: answers.network,
304301
source: answers.source,
305302
indexEvents: answers.indexEvents,
306-
contractName: answers.contractName,
303+
contractName: answers.contractName || DEFAULT_CONTRACT_NAME,
307304
node,
308305
startBlock: answers.startBlock,
309306
spkgPath: answers.spkgPath,

packages/cli/src/scaffold/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export default class Scaffold {
118118

119119
async generateManifest() {
120120
const protocolManifest = this.protocol.getManifestScaffold();
121+
const name = this.contractName || getSubgraphBasename(String(this.subgraphName));
121122

122123
return await prettier.format(
123124
`
@@ -128,7 +129,7 @@ schema:
128129
file: ./schema.graphql
129130
dataSources:
130131
- kind: ${this.protocol.name}
131-
name: ${this.contractName}
132+
name: ${name}
132133
network: ${this.network}
133134
source: ${protocolManifest.source({ ...this, spkgPath: './substreams.spkg', spkgModule: 'graph_out' })}
134135
mapping: ${protocolManifest.mapping(this)}

packages/cli/tests/cli/__snapshots__/init.test.ts.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,35 @@ Next steps:
285285
Make sure to visit the documentation on https://thegraph.com/docs/ for further information.
286286
"
287287
`;
288+
289+
exports[`Init > Substreams > From package 1`] = `
290+
" › Warning: The --skip-git flag will be removed in the next major version. By
291+
› default we will stop initializing a Git repository.
292+
- Create subgraph scaffold
293+
Generate subgraph
294+
- Create subgraph scaffold
295+
Write subgraph to directory
296+
- Create subgraph scaffold
297+
✔ Create subgraph scaffold
298+
- Install dependencies with yarn
299+
✔ Install dependencies with yarn
300+
"
301+
`;
302+
303+
exports[`Init > Substreams > From package 2`] = `0`;
304+
305+
exports[`Init > Substreams > From package 3`] = `
306+
"
307+
Subgraph user/subgraph-from-substreams created in from-package
308+
309+
Next steps:
310+
311+
1. Run \`graph auth\` to authenticate with your deploy key.
312+
313+
2. Type \`cd from-package\` to enter the subgraph.
314+
315+
3. Run \`yarn deploy\` to deploy the subgraph.
316+
317+
Make sure to visit the documentation on https://thegraph.com/docs/ for further information.
318+
"
319+
`;

packages/cli/tests/cli/init.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,33 @@ describe.sequential(
210210
},
211211
);
212212
});
213+
214+
describe('Substreams', () => {
215+
const substreamsBaseDir = path.join(baseDir, 'substreams');
216+
217+
cliTest(
218+
'From package',
219+
[
220+
'init',
221+
'--skip-git',
222+
'--protocol',
223+
'substreams',
224+
'--spkg',
225+
path.join(substreamsBaseDir, 'substreams.spkg'),
226+
'--network',
227+
'mainnet',
228+
'user/subgraph-from-substreams',
229+
path.join(substreamsBaseDir, 'from-package'),
230+
],
231+
path.join('init', 'substreams', 'from-package'),
232+
{
233+
exitCode: 0,
234+
timeout: 100_000,
235+
cwd: substreamsBaseDir,
236+
deleteDir: true,
237+
},
238+
);
239+
});
213240
},
214241
{
215242
timeout: 60_000,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test file, nothing to see here

0 commit comments

Comments
 (0)