Skip to content

Commit af4513f

Browse files
typify graphql response (#1955)
1 parent ff6a269 commit af4513f

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

website/src/lib/graphql.ts

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

6-
export async function networkSubgraphExecute(query: string, endpoint: string, apiKey: string) {
9+
export async function networkSubgraphExecute<T, V>(
10+
query: TypedDocumentNode<T, V>,
11+
variables: V,
12+
endpoint: string,
13+
apiKey: string,
14+
) {
715
const response = await fetch(endpoint.replace('{api-key}', apiKey), {
816
method: 'POST',
917
body: JSON.stringify({
10-
query,
18+
query: print(query),
19+
variables,
1120
}),
1221
});
13-
return (await response.json()).data;
22+
const result = await response.json();
23+
return result.data as T;
1424
}

website/src/routes/publish.lazy.tsx

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

131-
const GetSubgraphInfo = (subgraphId: string) => `
132-
{
133-
subgraph(id: "${subgraphId}") {
132+
const GetSubgraphInfoQuery = graphql(`
133+
query GetSubgraphInfo($subgraphId: ID!) {
134+
subgraph(id: $subgraphId) {
134135
id
135136
owner {
136137
id
@@ -154,7 +155,7 @@ const GetSubgraphInfo = (subgraphId: string) => `
154155
}
155156
}
156157
}
157-
`;
158+
`);
158159

159160
function getEtherscanUrl({ chainId, hash }: { chainId: number; hash: string }) {
160161
switch (chainId) {
@@ -238,7 +239,8 @@ function DeploySubgraph({
238239
if (!subgraphEndpoint) return;
239240

240241
const data = await networkSubgraphExecute(
241-
GetSubgraphInfo(subgraphId),
242+
GetSubgraphInfoQuery,
243+
{ subgraphId },
242244
subgraphEndpoint,
243245
apiKey,
244246
);
@@ -265,7 +267,7 @@ function DeploySubgraph({
265267
form.setValue('displayName', metadata.displayName);
266268
}
267269

268-
if (data.subgraph.versions?.length > 0) {
270+
if (data.subgraph?.versions?.length) {
269271
const version = data.subgraph.versions[data.subgraph.versions.length - 1];
270272
form.setValue('versionLabel', version.metadata?.label ?? '');
271273
}
@@ -282,7 +284,7 @@ function DeploySubgraph({
282284
const version = form.watch('versionLabel');
283285

284286
const versionInfo = subgraphInfo.subgraph?.versions.find(
285-
({ metadata }: { metadata: { label: string } }) => metadata?.label === version,
287+
ver => ver.metadata?.label === version,
286288
);
287289

288290
if (!versionInfo) return false;
@@ -298,9 +300,7 @@ function DeploySubgraph({
298300

299301
const version = form.watch('versionLabel');
300302

301-
return !subgraphInfo.subgraph?.versions.some(
302-
({ metadata }: { metadata: { label: string } }) => metadata?.label === version,
303-
);
303+
return !subgraphInfo.subgraph?.versions.some(ver => ver.metadata?.label === version);
304304
}
305305

306306
function isOwner() {

0 commit comments

Comments
 (0)