Skip to content

Commit a074696

Browse files
committed
Address review comments
1 parent 3a75a7f commit a074696

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

packages/cli/src/commands/init.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,17 @@ async function processInitForm(
594594
message: `IPFS node to use for fetching subgraph manifest`,
595595
initial: ipfsUrl,
596596
skip: () => !isComposedSubgraph,
597+
validate: value => {
598+
if (!value) {
599+
return 'IPFS node URL cannot be empty';
600+
}
601+
try {
602+
new URL(value);
603+
return true;
604+
} catch {
605+
return 'Please enter a valid URL';
606+
}
607+
},
597608
result: value => {
598609
ipfsNode = value;
599610
initDebugger.extend('processInitForm')('ipfs: %O', value);
@@ -615,9 +626,6 @@ async function processInitForm(
615626
initial: initContract,
616627
validate: async (value: string): Promise<string | boolean> => {
617628
if (isComposedSubgraph) {
618-
if (!ipfsNode) {
619-
return true;
620-
}
621629
const ipfs = createIpfsClient(ipfsNode);
622630
const { valid, error } = await validateSubgraphNetworkMatch(ipfs, value, network.id);
623631
if (!valid) {
@@ -834,22 +842,6 @@ async function processInitForm(
834842

835843
await promptManager.executeInteractive();
836844

837-
// Validate network matches if loading from IPFS
838-
if (ipfsNode && source?.startsWith('Qm')) {
839-
const ipfs = createIpfsClient(ipfsNode);
840-
try {
841-
const { valid, error } = await validateSubgraphNetworkMatch(ipfs, source!, network.id);
842-
if (!valid) {
843-
throw new Error(error || 'Invalid subgraph network match');
844-
}
845-
} catch (e) {
846-
if (e instanceof Error) {
847-
print.error(`Failed to validate subgraph network: ${e?.message}`);
848-
}
849-
throw e;
850-
}
851-
}
852-
853845
return {
854846
abi: (abiFromApi || abiFromFile)!,
855847
protocolInstance,
@@ -1207,10 +1199,19 @@ async function initSubgraphFromContract(
12071199
},
12081200
});
12091201

1202+
// Validate network match first
1203+
const { valid, error } = await validateSubgraphNetworkMatch(ipfsClient, source, network);
1204+
if (!valid) {
1205+
throw new Error(error || 'Invalid subgraph network match');
1206+
}
1207+
12101208
const schemaString = await loadSubgraphSchemaFromIPFS(ipfsClient, source);
12111209
const schema = await Schema.loadFromString(schemaString);
12121210
entities = schema.getEntityNames();
12131211
} catch (e) {
1212+
if (e instanceof Error) {
1213+
print.error(`Failed to validate subgraph: ${e?.message}`);
1214+
}
12141215
this.error(`Failed to load and parse subgraph schema: ${e.message}`, { exit: 1 });
12151216
}
12161217
}

packages/cli/src/utils.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,8 @@ export async function validateSubgraphNetworkMatch(
7575

7676
// Get network from first data source
7777
const sourceNetwork = allSources[0].network;
78-
if (!sourceNetwork) {
79-
return { valid: true }; // Network not specified in source, skip validation
80-
}
81-
82-
const normalizedSourceNetwork = sourceNetwork.toLowerCase();
83-
const normalizedTargetNetwork = targetNetwork.toLowerCase();
8478

85-
if (normalizedSourceNetwork !== normalizedTargetNetwork) {
79+
if (sourceNetwork !== targetNetwork) {
8680
return {
8781
valid: false,
8882
error: `Network mismatch: The source subgraph is indexing the '${sourceNetwork}' network, but you're creating a subgraph for '${targetNetwork}' network. When composing subgraphs, they must index the same network.`,

0 commit comments

Comments
 (0)