11import { useState } from 'react' ;
22import { ConnectKitButton , useModal } from 'connectkit' ;
3- import { graphql } from 'gql.tada' ;
43import { useForm } from 'react-hook-form' ;
54import semver from 'semver' ;
65import { 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
160159function 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,32 @@ 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 :
230+ 'apiKey is missing in URL params. Please add it to the URL params and try again.' ,
231+ variant : 'destructive' ,
232+ } ) ;
233+ return ;
234+ }
220235
221236 const subgraphEndpoint = chain ? getChainInfo ( chain ) ?. subgraph : null ;
222237
223238 if ( ! subgraphEndpoint ) return ;
224239
225240 const data = await networkSubgraphExecute (
226- GetSubgraphInfo ,
227- { subgraphId } ,
228- {
229- endpoint : subgraphEndpoint ,
230- } ,
241+ GetSubgraphInfo ( subgraphId ) ,
242+ subgraphEndpoint ,
243+ apiKey ,
231244 ) ;
232245
233246 const metadata = data . subgraph ?. metadata ;
@@ -251,6 +264,11 @@ function DeploySubgraph({
251264 if ( metadata . displayName ) {
252265 form . setValue ( 'displayName' , metadata . displayName ) ;
253266 }
267+
268+ if ( data . subgraph . versions ?. length > 0 ) {
269+ const version = data . subgraph . versions [ data . subgraph . versions . length - 1 ] ;
270+ form . setValue ( 'versionLabel' , version . metadata ?. label ?? '' ) ;
271+ }
254272 }
255273
256274 return data ;
@@ -264,7 +282,7 @@ function DeploySubgraph({
264282 const version = form . watch ( 'versionLabel' ) ;
265283
266284 const versionInfo = subgraphInfo . subgraph ?. versions . find (
267- ( { metadata } ) => metadata ?. label === version ,
285+ ( { metadata } : { metadata : { label : string } } ) => metadata ?. label === version ,
268286 ) ;
269287
270288 if ( ! versionInfo ) return false ;
@@ -280,7 +298,9 @@ function DeploySubgraph({
280298
281299 const version = form . watch ( 'versionLabel' ) ;
282300
283- return ! subgraphInfo . subgraph ?. versions . some ( ( { metadata } ) => metadata ?. label === version ) ;
301+ return ! subgraphInfo . subgraph ?. versions . some (
302+ ( { metadata } : { metadata : { label : string } } ) => metadata ?. label === version ,
303+ ) ;
284304 }
285305
286306 function isOwner ( ) {
@@ -375,7 +395,7 @@ function DeploySubgraph({
375395 if ( e ?. name === 'ContractFunctionExecutionError' ) {
376396 if ( e . cause . name === 'ContractFunctionRevertedError' ) {
377397 toast ( {
378- description : e . cause . reason ,
398+ description : e . cause . message ,
379399 variant : 'destructive' ,
380400 } ) ;
381401 return ;
@@ -565,7 +585,7 @@ function DeploySubgraph({
565585}
566586
567587function Page ( ) {
568- const { id, subgraphId, network } = Route . useSearch ( ) ;
588+ const { id, subgraphId, network, apiKey } = Route . useSearch ( ) ;
569589
570590 const protocolNetwork = network
571591 ? // @ts -expect-error we want to compare if it is a string or not
@@ -581,7 +601,12 @@ function Page() {
581601 < ConnectKitButton />
582602 </ nav >
583603 { id ? (
584- < DeploySubgraph deploymentId = { id } subgraphId = { subgraphId } network = { protocolNetwork } />
604+ < DeploySubgraph
605+ deploymentId = { id }
606+ subgraphId = { subgraphId }
607+ network = { protocolNetwork }
608+ apiKey = { apiKey }
609+ />
585610 ) : (
586611 < div className = "flex justify-center items-center min-h-screen -mt-16" >
587612 Unable to find the Deployment ID. Go back to CLI
0 commit comments