Skip to content

Commit 92b0390

Browse files
committed
update NETWORK_SUBGRAPH_MAINNET & add api-key to CLI
1 parent 9675000 commit 92b0390

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

packages/cli/src/commands/publish.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export default class PublishCommand extends Command {
4646
required: false,
4747
default: 'https://cli.thegraph.com/publish',
4848
}),
49+
'api-key': Flags.string({
50+
summary: 'The API key to use for the Subgraph queries.',
51+
required: false,
52+
}),
4953
};
5054

5155
/**
@@ -56,11 +60,13 @@ export default class PublishCommand extends Command {
5660
webapp,
5761
subgraphId,
5862
protocolNetwork,
63+
apiKey,
5964
}: {
6065
ipfsHash: string;
6166
webapp: string;
6267
subgraphId: string | undefined;
6368
protocolNetwork: string | undefined;
69+
apiKey: string | undefined;
6470
}) {
6571
const answer = await ux.prompt(
6672
`Press ${chalk.green(
@@ -84,6 +90,9 @@ export default class PublishCommand extends Command {
8490
if (protocolNetwork) {
8591
searchParams.set('network', protocolNetwork);
8692
}
93+
if (apiKey) {
94+
searchParams.set('apiKey', apiKey);
95+
}
8796

8897
url.search = searchParams.toString();
8998

@@ -105,11 +114,12 @@ export default class PublishCommand extends Command {
105114
ipfs,
106115
'subgraph-id': subgraphId,
107116
'protocol-network': protocolNetwork,
117+
'api-key': apiKey,
108118
},
109119
} = await this.parse(PublishCommand);
110120

111121
if (ipfsHash) {
112-
await this.publishWithBrowser({ ipfsHash, webapp: webUiUrl, subgraphId, protocolNetwork });
122+
await this.publishWithBrowser({ ipfsHash, webapp: webUiUrl, subgraphId, protocolNetwork, apiKey });
113123
return;
114124
}
115125

@@ -146,6 +156,7 @@ export default class PublishCommand extends Command {
146156
webapp: webUiUrl,
147157
subgraphId,
148158
protocolNetwork,
159+
apiKey,
149160
});
150161
return;
151162
}

packages/cli/src/validation/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ const gatherLocalTypes = (defs: readonly graphql.DefinitionNode[]) => {
227227
)
228228
.map(
229229
def =>
230-
// @ts-expect-error TODO: name field does not exist on definition, really?
230+
// TODO: name field does not exist on definition, really?
231231
def.name.value,
232232
);
233233
};

website/src/lib/graphql.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
import { print } from 'graphql';
2-
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
1+
export const NETWORK_SUBGRAPH_MAINNET = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp';
2+
export const NETWORK_SUBGRAPH_SEPOLIA = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/3xQHhMudr1oh69ut36G2mbzpYmYxwqCeU6wwqyCDCnqV';
33

4-
export const NETWORK_SUBGRAPH_MAINNET =
5-
'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-arbitrum';
6-
export const NETWORK_SUBGRAPH_SEPOLIA =
7-
'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-arbitrum-sepolia';
8-
9-
export async function networkSubgraphExecute<Result = unknown, Variables = unknown>(
10-
query: TypedDocumentNode<Result, Variables>,
11-
variables: Variables,
12-
{
13-
endpoint,
14-
}: {
15-
endpoint: string;
16-
},
17-
): Promise<Result> {
18-
const response = await fetch(endpoint, {
4+
export async function networkSubgraphExecute(
5+
query: string,
6+
endpoint: string,
7+
apiKey: string,
8+
) {
9+
const response = await fetch(endpoint.replace("{api-key}", apiKey), {
1910
method: 'POST',
2011
body: JSON.stringify({
21-
query: print(query),
22-
variables,
12+
query,
2313
}),
2414
});
2515
return (await response.json()).data;

website/src/routes/publish.lazy.tsx

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useState } from 'react';
22
import { ConnectKitButton, useModal } from 'connectkit';
3-
import { graphql } from 'gql.tada';
43
import { useForm } from 'react-hook-form';
54
import semver from 'semver';
65
import { Address } from 'viem';
@@ -129,9 +128,9 @@ const Manifest = z.object({
129128
repository: z.string().describe('An optional link to where the subgraph lives.').optional(),
130129
});
131130

132-
const GetSubgraphInfo = graphql(/* GraphQL */ `
133-
query GetSubgraphInfo($subgraphId: ID!) {
134-
subgraph(id: $subgraphId) {
131+
const GetSubgraphInfo = (subgraphId: string) => `
132+
{
133+
subgraph(id: "${subgraphId}") {
135134
id
136135
owner {
137136
id
@@ -155,7 +154,7 @@ const GetSubgraphInfo = graphql(/* GraphQL */ `
155154
}
156155
}
157156
}
158-
`);
157+
`;
159158

160159
function getEtherscanUrl({ chainId, hash }: { chainId: number; hash: string }) {
161160
switch (chainId) {
@@ -172,10 +171,12 @@ function DeploySubgraph({
172171
deploymentId,
173172
subgraphId,
174173
network,
174+
apiKey
175175
}: {
176176
deploymentId: string;
177177
subgraphId: string | undefined;
178178
network: (typeof CHAINS)[number] | undefined;
179+
apiKey: string | undefined;
179180
}) {
180181
const { writeContractAsync, isPending, error: contractError } = useWriteContract({});
181182
const { setOpen } = useModal();
@@ -214,20 +215,31 @@ function DeploySubgraph({
214215
const chain = form.watch('chain');
215216

216217
const { data: subgraphInfo } = useQuery({
217-
queryKey: ['subgraph-info', subgraphId, chain, chainId],
218+
queryKey: ['subgraph-info', subgraphId, chain, chainId, apiKey],
218219
queryFn: async () => {
219-
if (!subgraphId) return;
220+
if (!subgraphId) {
221+
toast({
222+
description: 'Subgraph ID is missing. Please add it to the URL params and try again.',
223+
variant: 'destructive',
224+
});
225+
return;
226+
}
227+
if (!apiKey) {
228+
toast({
229+
description: "apiKey is missing in URL params. Please add it to the URL params and try again.",
230+
variant: 'destructive',
231+
});
232+
return;
233+
}
220234

221235
const subgraphEndpoint = chain ? getChainInfo(chain)?.subgraph : null;
222236

223237
if (!subgraphEndpoint) return;
224238

225239
const data = await networkSubgraphExecute(
226-
GetSubgraphInfo,
227-
{ subgraphId },
228-
{
229-
endpoint: subgraphEndpoint,
230-
},
240+
GetSubgraphInfo(subgraphId),
241+
subgraphEndpoint,
242+
apiKey
231243
);
232244

233245
const metadata = data.subgraph?.metadata;
@@ -264,7 +276,7 @@ function DeploySubgraph({
264276
const version = form.watch('versionLabel');
265277

266278
const versionInfo = subgraphInfo.subgraph?.versions.find(
267-
({ metadata }) => metadata?.label === version,
279+
({ metadata }: { metadata: { label: string } }) => metadata?.label === version,
268280
);
269281

270282
if (!versionInfo) return false;
@@ -280,7 +292,7 @@ function DeploySubgraph({
280292

281293
const version = form.watch('versionLabel');
282294

283-
return !subgraphInfo.subgraph?.versions.some(({ metadata }) => metadata?.label === version);
295+
return !subgraphInfo.subgraph?.versions.some(({ metadata }: { metadata: { label: string } }) => metadata?.label === version);
284296
}
285297

286298
function isOwner() {
@@ -375,7 +387,7 @@ function DeploySubgraph({
375387
if (e?.name === 'ContractFunctionExecutionError') {
376388
if (e.cause.name === 'ContractFunctionRevertedError') {
377389
toast({
378-
description: e.cause.reason,
390+
description: e.cause.message,
379391
variant: 'destructive',
380392
});
381393
return;
@@ -565,7 +577,7 @@ function DeploySubgraph({
565577
}
566578

567579
function Page() {
568-
const { id, subgraphId, network } = Route.useSearch();
580+
const { id, subgraphId, network, apiKey } = Route.useSearch();
569581

570582
const protocolNetwork = network
571583
? // @ts-expect-error we want to compare if it is a string or not
@@ -581,7 +593,7 @@ function Page() {
581593
<ConnectKitButton />
582594
</nav>
583595
{id ? (
584-
<DeploySubgraph deploymentId={id} subgraphId={subgraphId} network={protocolNetwork} />
596+
<DeploySubgraph deploymentId={id} subgraphId={subgraphId} network={protocolNetwork} apiKey={apiKey} />
585597
) : (
586598
<div className="flex justify-center items-center min-h-screen -mt-16">
587599
Unable to find the Deployment ID. Go back to CLI

website/src/routes/publish.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const Route = createFileRoute('/publish')({
66
subgraphId: z.string().optional(),
77
// Transforming string to enum here doesn't actually work
88
network: z.string().optional(),
9-
id: z.string(),
9+
id: z.string().optional(),
10+
apiKey: z.string().optional(),
1011
}),
1112
});

0 commit comments

Comments
 (0)