diff --git a/.changeset/angry-ties-kiss.md b/.changeset/angry-ties-kiss.md new file mode 100644 index 000000000..52018c225 --- /dev/null +++ b/.changeset/angry-ties-kiss.md @@ -0,0 +1,5 @@ +--- +'@graphprotocol/graph-cli': patch +--- + +Fix empty source name for substreams subgraphs #1868 diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index f1f45bc67..4c35dfd8c 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -32,6 +32,7 @@ const protocolChoices = Array.from(Protocol.availableProtocols().keys()); const initDebugger = debugFactory('graph-cli:commands:init'); const DEFAULT_EXAMPLE_SUBGRAPH = 'ethereum-gravatar'; +const DEFAULT_CONTRACT_NAME = 'Contract'; export default class InitCommand extends Command { static description = 'Creates a new subgraph with basic scaffolding.'; @@ -61,7 +62,7 @@ export default class InitCommand extends Command { description: 'Creates a scaffold based on an example subgraph.', // TODO: using a default sets the value and therefore requires not to have --from-contract // default: 'Contract', - exclusive: ['from-contract'], + exclusive: ['from-contract', 'spkg'], }), 'contract-name': Flags.string({ @@ -103,7 +104,6 @@ export default class InitCommand extends Command { summary: 'Network the contract is deployed to.', description: 'Refer to https://github.com/graphprotocol/networks-registry/ for supported networks', - dependsOn: ['from-contract'], }), ipfs: Flags.string({ @@ -143,17 +143,14 @@ export default class InitCommand extends Command { 'The --skip-git flag will be removed in the next major version. By default we will stop initializing a Git repository.', ); } + if ((!fromContract || !spkgPath) && !network && !fromExample) { + this.error('--network is required when using --from-contract or --spkg'); + } const { node } = chooseNodeUrl({ node: nodeFlag, }); - if (fromContract && fromExample) { - this.error('Only one of "--from-example" and "--from-contract" can be used at a time.', { - exit: 1, - }); - } - // Detect git const git = system.which('git'); if (!git) { @@ -200,7 +197,7 @@ export default class InitCommand extends Command { // If all parameters are provided from the command-line, // go straight to creating the subgraph from an existing contract - if (fromContract && protocol && subgraphName && directory && network && node) { + if ((fromContract || spkgPath) && protocol && subgraphName && directory && network && node) { const registry = await loadRegistry(); const contractService = new ContractService(registry); @@ -225,7 +222,7 @@ export default class InitCommand extends Command { } } else { try { - abi = await contractService.getABI(ABI, network, fromContract); + abi = await contractService.getABI(ABI, network, fromContract!); } catch (e) { this.exit(1); } @@ -237,11 +234,11 @@ export default class InitCommand extends Command { protocolInstance, abi, directory, - source: fromContract, + source: fromContract!, indexEvents, network, subgraphName, - contractName, + contractName: contractName || DEFAULT_CONTRACT_NAME, node, startBlock, spkgPath, @@ -303,7 +300,7 @@ export default class InitCommand extends Command { network: answers.network, source: answers.source, indexEvents: answers.indexEvents, - contractName: answers.contractName, + contractName: answers.contractName || DEFAULT_CONTRACT_NAME, node, startBlock: answers.startBlock, spkgPath: answers.spkgPath, diff --git a/packages/cli/src/scaffold/index.ts b/packages/cli/src/scaffold/index.ts index 1d570b514..66a14cee0 100644 --- a/packages/cli/src/scaffold/index.ts +++ b/packages/cli/src/scaffold/index.ts @@ -118,6 +118,7 @@ export default class Scaffold { async generateManifest() { const protocolManifest = this.protocol.getManifestScaffold(); + const name = this.contractName || getSubgraphBasename(String(this.subgraphName)); return await prettier.format( ` @@ -128,7 +129,7 @@ schema: file: ./schema.graphql dataSources: - kind: ${this.protocol.name} - name: ${this.contractName} + name: ${name} network: ${this.network} source: ${protocolManifest.source({ ...this, spkgPath: './substreams.spkg', spkgModule: 'graph_out' })} mapping: ${protocolManifest.mapping(this)} diff --git a/packages/cli/tests/cli/__snapshots__/init.test.ts.snap b/packages/cli/tests/cli/__snapshots__/init.test.ts.snap index 17fbfbbc3..a9fb74ba3 100644 --- a/packages/cli/tests/cli/__snapshots__/init.test.ts.snap +++ b/packages/cli/tests/cli/__snapshots__/init.test.ts.snap @@ -285,3 +285,35 @@ Next steps: Make sure to visit the documentation on https://thegraph.com/docs/ for further information. " `; + +exports[`Init > Substreams > From package 1`] = ` +" › Warning: The --skip-git flag will be removed in the next major version. By + › default we will stop initializing a Git repository. +- Create subgraph scaffold + Generate subgraph +- Create subgraph scaffold + Write subgraph to directory +- Create subgraph scaffold +✔ Create subgraph scaffold +- Install dependencies with yarn +✔ Install dependencies with yarn +" +`; + +exports[`Init > Substreams > From package 2`] = `0`; + +exports[`Init > Substreams > From package 3`] = ` +" +Subgraph user/subgraph-from-substreams created in from-package + +Next steps: + + 1. Run \`graph auth\` to authenticate with your deploy key. + + 2. Type \`cd from-package\` to enter the subgraph. + + 3. Run \`yarn deploy\` to deploy the subgraph. + +Make sure to visit the documentation on https://thegraph.com/docs/ for further information. +" +`; diff --git a/packages/cli/tests/cli/init.test.ts b/packages/cli/tests/cli/init.test.ts index efcacb183..874659a24 100644 --- a/packages/cli/tests/cli/init.test.ts +++ b/packages/cli/tests/cli/init.test.ts @@ -210,6 +210,33 @@ describe.sequential( }, ); }); + + describe('Substreams', () => { + const substreamsBaseDir = path.join(baseDir, 'substreams'); + + cliTest( + 'From package', + [ + 'init', + '--skip-git', + '--protocol', + 'substreams', + '--spkg', + path.join(substreamsBaseDir, 'substreams.spkg'), + '--network', + 'mainnet', + 'user/subgraph-from-substreams', + path.join(substreamsBaseDir, 'from-package'), + ], + path.join('init', 'substreams', 'from-package'), + { + exitCode: 0, + timeout: 100_000, + cwd: substreamsBaseDir, + deleteDir: true, + }, + ); + }); }, { timeout: 60_000, diff --git a/packages/cli/tests/cli/init/substreams/substreams.spkg b/packages/cli/tests/cli/init/substreams/substreams.spkg new file mode 100644 index 000000000..0884a0c13 --- /dev/null +++ b/packages/cli/tests/cli/init/substreams/substreams.spkg @@ -0,0 +1 @@ +test file, nothing to see here