Skip to content

Commit cbbb0fc

Browse files
authored
fix: send graph cli version in header of cli (#1461)
* fix: send graph cli version as user agent * fix: send graph cli version as user agent * remove json import
1 parent de8abe0 commit cbbb0fc

File tree

14 files changed

+107
-26
lines changed

14 files changed

+107
-26
lines changed

.changeset/beige-mails-turn.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+
include user agent for CLI fetch calls

.changeset/rare-goats-crash.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+
send graph cli version as user agent for all third party API calls

.changeset/rotten-islands-love.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+
send graph cli version as user agent for all ipfs requests

.eslintrc.cjs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ module.exports = {
2929
'sonarjs/no-inverted-boolean-check': 'warn',
3030
// TODO: warning for now, clean up
3131
'@typescript-eslint/no-loss-of-precision': 'warn',
32-
// AssemblyScript types are different from TS and in cases we want to use what TS may think we should not
32+
// AssemblyScript types are different from TS and in cases we want to use what TS may think we should not,
33+
},
34+
},
35+
{
36+
files: ['packages/cli/**'],
37+
rules: {
38+
'no-restricted-imports': [
39+
'error',
40+
{
41+
patterns: [
42+
{
43+
group: ['@whatwg-node/fetch'],
44+
message: 'Please use `fetch` from `./packages/cli/src/fetch.ts`.',
45+
},
46+
],
47+
},
48+
],
3349
},
3450
},
3551
],

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import immutable from 'immutable';
2-
import { fetch } from '@whatwg-node/fetch';
2+
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
3+
import fetch from '../fetch';
34
import ABI from '../protocols/ethereum/abi';
45
import { withSpinner } from './spinner';
56

@@ -87,6 +88,7 @@ export const fetchTransactionByHashFromRPC = async (
8788
method: 'POST',
8889
headers: {
8990
'Content-Type': 'application/json',
91+
...GRAPH_CLI_SHARED_HEADERS,
9092
},
9193
body: JSON.stringify({
9294
jsonrpc: '2.0',

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { URL } from 'url';
22
import * as toolbox from 'gluegun';
33
import { create } from 'ipfs-http-client';
44
import Compiler from '../compiler';
5+
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
56
import Protocol from '../protocols';
67

78
interface CreateCompilerOptions {
@@ -54,7 +55,13 @@ The IPFS URL must be of the following format: http(s)://host[:port]/[path]`);
5455

5556
// Connect to the IPFS node (if a node address was provided)
5657
const ipfsClient = ipfs
57-
? create({ url: appendApiVersionForGraph(ipfs.toString()), headers })
58+
? create({
59+
url: appendApiVersionForGraph(ipfs.toString()),
60+
headers: {
61+
...headers,
62+
...GRAPH_CLI_SHARED_HEADERS,
63+
},
64+
})
5865
: undefined;
5966

6067
return new Compiler({

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { URL } from 'url';
22
import { print } from 'gluegun';
3+
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
34

45
const SUBGRAPH_STUDIO_URL = 'https://api.studio.thegraph.com/deploy/';
56
const HOSTED_SERVICE_URL = 'https://api.thegraph.com/deploy/';
@@ -73,7 +74,7 @@ export async function getHostedServiceSubgraphId({
7374
}),
7475
headers: {
7576
'content-type': 'application/json',
76-
'User-Agent': '@graphprotocol/graph-cli',
77+
...GRAPH_CLI_SHARED_HEADERS,
7778
},
7879
});
7980

packages/cli/src/commands/create.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Args, Command, Flags } from '@oclif/core';
44
import { identifyDeployKey as identifyAccessToken } from '../command-helpers/auth';
55
import { createJsonRpcClient } from '../command-helpers/jsonrpc';
66
import { validateNodeUrl } from '../command-helpers/node';
7+
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
78

89
export default class CreateCommand extends Command {
910
static description = 'Registers a subgraph name';
@@ -54,7 +55,10 @@ export default class CreateCommand extends Command {
5455
const accessToken = await identifyAccessToken(node, accessTokenFlag);
5556
if (accessToken !== undefined && accessToken !== null) {
5657
// @ts-expect-error options property seems to exist
57-
client.options.headers = { Authorization: `Bearer ${accessToken}` };
58+
client.options.headers = {
59+
...GRAPH_CLI_SHARED_HEADERS,
60+
Authorization: `Bearer ${accessToken}`,
61+
};
5862
}
5963

6064
const spinner = print.spin(`Creating subgraph in Graph node: ${requestUrl}`);

packages/cli/src/commands/deploy.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { updateSubgraphNetwork } from '../command-helpers/network';
1212
import { chooseNodeUrl, getHostedServiceSubgraphId } from '../command-helpers/node';
1313
import { validateStudioNetwork } from '../command-helpers/studio';
1414
import { assertGraphTsVersion, assertManifestApiVersion } from '../command-helpers/version';
15+
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
1516
import Protocol from '../protocols';
1617

1718
const headersFlag = Flags.custom<Record<string, string>>({
@@ -170,7 +171,10 @@ export default class DeployCommand extends Command {
170171
}
171172

172173
// @ts-expect-error options property seems to exist
173-
client.options.headers = { Authorization: 'Bearer ' + deployKey };
174+
client.options.headers = {
175+
...GRAPH_CLI_SHARED_HEADERS,
176+
Authorization: 'Bearer ' + deployKey,
177+
};
174178

175179
const subgraphName = await ux.prompt('What is the name of the subgraph you want to deploy?', {
176180
required: true,
@@ -299,7 +303,10 @@ export default class DeployCommand extends Command {
299303
deployKey = await identifyDeployKey(node, deployKey);
300304
if (deployKey !== undefined && deployKey !== null) {
301305
// @ts-expect-error options property seems to exist
302-
client.options.headers = { Authorization: 'Bearer ' + deployKey };
306+
client.options.headers = {
307+
...GRAPH_CLI_SHARED_HEADERS,
308+
Authorization: 'Bearer ' + deployKey,
309+
};
303310
}
304311

305312
// Ask for label if not on hosted service
@@ -384,7 +391,10 @@ export default class DeployCommand extends Command {
384391
// Connect to the IPFS node (if a node address was provided)
385392
const ipfsClient = create({
386393
url: appendApiVersionForGraph(ipfs.toString()),
387-
headers,
394+
headers: {
395+
...headers,
396+
...GRAPH_CLI_SHARED_HEADERS,
397+
},
388398
});
389399

390400
// Fetch the manifest from IPFS

packages/cli/src/commands/remove.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Args, Command, Flags } from '@oclif/core';
44
import { identifyDeployKey as identifyAccessToken } from '../command-helpers/auth';
55
import { createJsonRpcClient } from '../command-helpers/jsonrpc';
66
import { validateNodeUrl } from '../command-helpers/node';
7+
import { GRAPH_CLI_SHARED_HEADERS } from '../constants';
78

89
export default class RemoveCommand extends Command {
910
static description = 'Unregisters a subgraph name';
@@ -54,7 +55,10 @@ export default class RemoveCommand extends Command {
5455
const accessToken = await identifyAccessToken(node, accessTokenFlag);
5556
if (accessToken !== undefined && accessToken !== null) {
5657
// @ts-expect-error options property seems to exist
57-
client.options.headers = { Authorization: `Bearer ${accessToken}` };
58+
client.options.headers = {
59+
...GRAPH_CLI_SHARED_HEADERS,
60+
Authorization: `Bearer ${accessToken}`,
61+
};
5862
}
5963

6064
const spinner = print.spin(`Creating subgraph in Graph node: ${requestUrl}`);

0 commit comments

Comments
 (0)