Skip to content

Commit 2164a20

Browse files
authored
fix: append /api/v0 to graph IPFS endpoint (#1291)
* fix: append /api/v0 to graph IPFS endpoint * prettier
1 parent b4311d8 commit 2164a20

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

.changeset/twelve-frogs-walk.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+
append /api/v0 automatically to Graph IPFS endpoint
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { appendApiVersionForGraph } from './compiler';
2+
3+
describe('appendApiVersionForGraph', () => {
4+
it('append /api/v0 to Prod URL with trailing slash', () => {
5+
expect(appendApiVersionForGraph('https://api.thegraph.com/ipfs/')).toBe(
6+
'https://api.thegraph.com/ipfs/api/v0',
7+
);
8+
});
9+
10+
it('append /api/v0 to Prod URL without trailing slash', () => {
11+
expect(appendApiVersionForGraph('https://api.thegraph.com/ipfs')).toBe(
12+
'https://api.thegraph.com/ipfs/api/v0',
13+
);
14+
});
15+
16+
it('append /api/v0 to Staging URL without trailing slash', () => {
17+
expect(appendApiVersionForGraph('https://staging.api.thegraph.com/ipfs')).toBe(
18+
'https://staging.api.thegraph.com/ipfs/api/v0',
19+
);
20+
});
21+
22+
it('do nothing if Prod URL has /api/v0', () => {
23+
expect(appendApiVersionForGraph('https://api.thegraph.com/ipfs/api/v0')).toBe(
24+
'https://api.thegraph.com/ipfs/api/v0',
25+
);
26+
});
27+
28+
it('do nothing if Prod URL has no /ipfs', () => {
29+
expect(appendApiVersionForGraph('https://api.thegraph.com')).toBe('https://api.thegraph.com');
30+
});
31+
32+
it('do nothing for non-graph endpoint', () => {
33+
expect(appendApiVersionForGraph('https://ipfs.saihaj.dev/')).toBe('https://ipfs.saihaj.dev/');
34+
});
35+
36+
it('do nothing for non-graph endpoint ending with /ipfs', () => {
37+
expect(appendApiVersionForGraph('https://ipfs.saihaj.dev/ipfs/')).toBe(
38+
'https://ipfs.saihaj.dev/ipfs/',
39+
);
40+
});
41+
});

packages/cli/src/command-helpers/compiler.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,31 @@ import Compiler from '../compiler';
55
import Protocol from '../protocols';
66

77
interface CreateCompilerOptions {
8-
ipfs: any;
9-
headers?: any;
8+
ipfs: string | URL | undefined;
9+
headers?: Headers | Record<string, string>;
1010
outputDir: string;
1111
outputFormat: string;
1212
skipMigrations: boolean;
1313
blockIpfsMethods?: RegExpMatchArray;
1414
protocol: Protocol;
1515
}
1616

17+
/**
18+
* Appends /api/v0 to the end of a The Graph IPFS URL
19+
*/
20+
export function appendApiVersionForGraph(inputString: string) {
21+
// Check if the input string is a valid The Graph IPFS URL
22+
const pattern = /^(https?:\/\/)?([\w-]+\.)+thegraph\.com\/ipfs\/?$/;
23+
if (pattern.test(inputString)) {
24+
// account for trailing slash
25+
if (inputString.endsWith('/')) {
26+
return inputString.slice(0, -1) + '/api/v0';
27+
}
28+
return inputString + '/api/v0';
29+
}
30+
return inputString;
31+
}
32+
1733
// Helper function to construct a subgraph compiler
1834
export function createCompiler(
1935
manifest: string,
@@ -29,18 +45,20 @@ export function createCompiler(
2945
) {
3046
// Validate the IPFS URL (if a node address was provided)
3147
try {
32-
if (ipfs) new URL(ipfs);
48+
if (ipfs && typeof ipfs === 'string') new URL(ipfs);
3349
} catch (e) {
3450
toolbox.print.error(`Invalid IPFS URL: ${ipfs}
3551
The IPFS URL must be of the following format: http(s)://host[:port]/[path]`);
3652
return null;
3753
}
3854

3955
// Connect to the IPFS node (if a node address was provided)
40-
ipfs = ipfs ? create({ url: ipfs, headers }) : undefined;
56+
const ipfsClient = ipfs
57+
? create({ url: appendApiVersionForGraph(ipfs.toString()), headers })
58+
: undefined;
4159

4260
return new Compiler({
43-
ipfs,
61+
ipfs: ipfsClient,
4462
subgraphManifest: manifest,
4563
outputDir,
4664
outputFormat,

0 commit comments

Comments
 (0)