From 2f6143fbb8c66c8de37e30b14646ea1cad5cb338 Mon Sep 17 00:00:00 2001 From: mguleryuz Date: Wed, 9 Oct 2024 14:29:17 -0500 Subject: [PATCH] feat: singleton graph subs --- bun.lockb | Bin 579988 -> 579988 bytes package.json | 2 +- src/hooks/use-graphql-query.ts | 276 ++------------------------ src/hooks/use-graphql-subscription.ts | 190 +----------------- 4 files changed, 23 insertions(+), 445 deletions(-) diff --git a/bun.lockb b/bun.lockb index b5721eaf044cb42ad3aa9ece5f9e872ad61d059c..5c54f0e7bc0fcf3bb6bdd8e95517a8d151942f5c 100755 GIT binary patch delta 185 zcmV;q07n0mvm=zVBakj2yZ1cc4UZMUq(A+!kB4#6RejZuPU#!zX>@OZ`*Y(0u}(OY z3O4|`&D7-jCIR`A&{i0OsFb&;lmV5+Kt-9v&c6%O%dR&=YmSr;55El$z%50n9>#t( zk_k(_8x)Ed?OLbgHG?GBHkacctq1<_%KeYTtpFdM?uUI^0*8HC1BZQD1c!ZE n1-E@#26?psH@Ei>2YOBgH!gH%dYA5G2Ns8nUI({~UI++#@i0|^ delta 185 zcmV;q07n0mvm=zVBakj2I%(Sw>+X22d*jgGOc)y?7kX(APojJ+=!Lgp_J#-Iu}(OY z3N-*?fYP)J@yz0r&{i0OsFb&;lmV5+KsNK%q8Qs^!k(&h;tA*3sf3TfPn!}cs(Fsi zQVY1Gw&ao7LRcG-x%sqi^+>2x3?4$NsE5}_2y?$GX90ES2Zw!H0*8HC1BZQD1c!ZE n1-E@#26?psHMjQ<2YOBgH7;~#dYA5G2Ns8nUI({~UI++#p-fc? diff --git a/package.json b/package.json index 634bca9..2285b9e 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ }, "dependencies": { "@hookform/resolvers": "^3.9.0", - "@inverter-network/sdk": "0.3.0-alpha.5", + "@inverter-network/sdk": "0.3.0-alpha.7", "@radix-ui/react-accordion": "^1.2.1", "@radix-ui/react-alert-dialog": "^1.1.2", "@radix-ui/react-checkbox": "^1.1.2", diff --git a/src/hooks/use-graphql-query.ts b/src/hooks/use-graphql-query.ts index 2685752..4f59705 100644 --- a/src/hooks/use-graphql-query.ts +++ b/src/hooks/use-graphql-query.ts @@ -1,288 +1,38 @@ 'use client' import { useInverter } from './use-inverter' +import { useQuery } from '@tanstack/react-query' + import type { - FormattedGraphQLParams, - GQLBondingCurve, - GQLLinearVesting, - GQLStreamingPaymentProcessor, - GQLSwap, - GQLWorkflow, - GQLWorkflowModule, - GQLWorkflowModuleType, + GraphQLQueryArgs, + GraphQLQueryResult, } from '@inverter-network/sdk' -import { useQuery } from '@tanstack/react-query' import type { UseQueryOptions } from '@tanstack/react-query' import type { Except } from 'type-fest-4' -export type UseBondingCurveQueryParams = { - project: Array - params?: FormattedGraphQLParams - dependencies?: any[] - options?: Except< - UseQueryOptions | undefined, Error>, - 'queryKey' | 'queryFn' | 'enabled' - > -} - -export type UseBondingCurveQueryReturnType = ReturnType< - typeof useBondingCurveQuery -> - -export const useBondingCurveQuery = ({ - project, - params, - dependencies, - options = {}, -}: UseBondingCurveQueryParams) => { - const inverter = useInverter() - - const enabled = !!inverter.data - - const query = useQuery({ - queryKey: ['bonding-curve', project, params, ...(dependencies || [])], - queryFn: () => - inverter.data!.graphql.query.BondingCurve({ - project, - params, - }), - enabled, - ...options, - }) - - return query -} - -export type UseLinearVestingQueryParams = { - project: Array - params?: FormattedGraphQLParams - dependencies?: any[] - options?: Except< - UseQueryOptions | undefined, Error>, - 'queryKey' | 'queryFn' | 'enabled' - > -} - -export type UseLinearVestingQueryReturnType = ReturnType< - typeof useLinearVestingQuery -> - -export const useLinearVestingQuery = ({ - project, - params, - dependencies, - options = {}, -}: UseLinearVestingQueryParams) => { - const inverter = useInverter() - - const enabled = !!inverter.data - - const query = useQuery({ - queryKey: ['linear-vesting', project, params, ...(dependencies || [])], - queryFn: () => - inverter.data!.graphql.query.LinearVesting({ - project, - params, - }), - enabled, - ...options, - }) - - return query -} - -export type UseStreamingPaymentProcessorQueryParams = { - project: Array - params?: FormattedGraphQLParams - dependencies?: any[] - options?: Except< - UseQueryOptions< - Record | undefined, - Error - >, - 'queryKey' | 'queryFn' | 'enabled' - > -} - -export type UseStreamingPaymentProcessorQueryReturnType = ReturnType< - typeof useStreamingPaymentProcessorQuery -> - -export const useStreamingPaymentProcessorQuery = ({ - project, - params, - dependencies, - options = {}, -}: UseStreamingPaymentProcessorQueryParams) => { - const inverter = useInverter() - - const enabled = !!inverter.data - - const query = useQuery({ - queryKey: [ - 'streaming-payment-processor', - project, - params, - ...(dependencies || []), - ], - queryFn: () => - inverter.data!.graphql.query.StreamingPaymentProcessor({ - project, - params, - }), - enabled, - ...options, - }) - - return query -} - -export type UseSwapQueryParams = { - project: Array - params?: FormattedGraphQLParams - dependencies?: any[] - options?: Except< - UseQueryOptions | undefined, Error>, - 'queryKey' | 'queryFn' | 'enabled' - > -} - -export type UseSwapQueryReturnType = ReturnType - -export const useSwapQuery = ({ - project, - params, - dependencies, - options = {}, -}: UseSwapQueryParams) => { - const inverter = useInverter() - - const enabled = !!inverter.data - - const query = useQuery({ - queryKey: ['swap', project, params, ...(dependencies || [])], - queryFn: () => - inverter.data!.graphql.query.Swap({ - project, - params, - }), - enabled, - ...options, - }) - - return query -} - -export type UseWorkflowQueryParams = { - project: Array - params?: FormattedGraphQLParams - dependencies?: any[] - options?: Except< - UseQueryOptions | undefined, Error>, - 'queryKey' | 'queryFn' | 'enabled' - > -} - -export type UseWorkflowQueryReturnType = ReturnType - -export const useWorkflowQuery = ({ - project, - params, - dependencies, - options = {}, -}: UseWorkflowQueryParams) => { - const inverter = useInverter() - - const enabled = !!inverter.data - - const query = useQuery({ - queryKey: ['workflow', project, params, ...(dependencies || [])], - queryFn: () => - inverter.data!.graphql.query.Workflow({ - project, - params, - }), - enabled, - ...options, - }) - - return query -} - -export type UseWorkflowModuleQueryParams = { - project: Array - params?: FormattedGraphQLParams - dependencies?: any[] - options?: Except< - UseQueryOptions | undefined, Error>, - 'queryKey' | 'queryFn' | 'enabled' - > -} - -export type UseWorkflowModuleQueryReturnType = ReturnType< - typeof useWorkflowModuleQuery -> - -export const useWorkflowModuleQuery = ({ - project, - params, - dependencies, - options = {}, -}: UseWorkflowModuleQueryParams) => { - const inverter = useInverter() - - const enabled = !!inverter.data - - const query = useQuery({ - queryKey: ['workflow-module', project, params, ...(dependencies || [])], - queryFn: () => - inverter.data!.graphql.query.WorkflowModule({ - project, - params, - }), - enabled, - ...options, - }) - - return query -} - -export type UseWorkflowModuleTypeQueryParams = { - project: Array - params?: FormattedGraphQLParams +export type UseGraphQLQueryParams = { + fields: T dependencies?: any[] options?: Except< - UseQueryOptions | undefined, Error>, + UseQueryOptions | undefined, Error>, 'queryKey' | 'queryFn' | 'enabled' > } -export type UseWorkflowModuleTypeQueryReturnType = ReturnType< - typeof useWorkflowModuleTypeQuery -> +export type UseGraphQLQueryReturnType = ReturnType -export const useWorkflowModuleTypeQuery = ({ - project, - params, +export const useGraphQLQuery = ({ + fields, dependencies, options = {}, -}: UseWorkflowModuleTypeQueryParams) => { +}: UseGraphQLQueryParams) => { const inverter = useInverter() const enabled = !!inverter.data const query = useQuery({ - queryKey: [ - 'workflow-module-type', - project, - params, - ...(dependencies || []), - ], - queryFn: () => - inverter.data!.graphql.query.WorkflowModuleType({ - project, - params, - }), + queryKey: ['graphql-query', fields, ...(dependencies || [])], + queryFn: () => inverter.data!.graphql.query(fields), enabled, ...options, }) diff --git a/src/hooks/use-graphql-subscription.ts b/src/hooks/use-graphql-subscription.ts index 15116b7..19c11f2 100644 --- a/src/hooks/use-graphql-subscription.ts +++ b/src/hooks/use-graphql-subscription.ts @@ -2,197 +2,25 @@ import * as React from 'react' import { useInverter } from './use-inverter' -import type { - FormattedGraphQLParams, - GQLBondingCurve, - GQLLinearVesting, - GQLStreamingPaymentProcessor, - GQLSwap, - GQLWorkflow, - GQLWorkflowModule, - GQLWorkflowModuleType, -} from '@inverter-network/sdk' +import type { GraphQLSubscriptionArgs } from '@inverter-network/sdk' -export type UseBondingCurveSubscriptionParams = { - project: Array - params?: FormattedGraphQLParams +export type UseGraphQLSubscriptionParams = { + fields: GraphQLSubscriptionArgs } export type UseBondingCurveSubscriptionReturnType = ReturnType< - typeof useBondingCurveSubscription + typeof useGraphQLSubscription > -export const useBondingCurveSubscription = ({ - project, - params, -}: UseBondingCurveSubscriptionParams) => { +export const useGraphQLSubscription = ({ + fields, +}: UseGraphQLSubscriptionParams) => { const inverter = useInverter() const memo = React.useMemo(() => { if (!inverter.data) return null - return inverter.data!.graphql.subscription.BondingCurveSubscription({ - project, - params, - }) - }, [inverter.data, project, params]) - - return memo -} - -export type UseLinearVestingSubscriptionParams = { - project: Array - params?: FormattedGraphQLParams -} - -export type UseLinearVestingSubscriptionReturnType = ReturnType< - typeof useLinearVestingSubscription -> - -export const useLinearVestingSubscription = ({ - project, - params, -}: UseLinearVestingSubscriptionParams) => { - const inverter = useInverter() - - const memo = React.useMemo(() => { - if (!inverter.data) return null - return inverter.data.graphql.subscription.LinearVestingSubscription({ - project, - params, - }) - }, [inverter.data, project, params]) - - return memo -} - -export type UseStreamingPaymentProcessorSubscriptionParams = { - project: Array - params?: FormattedGraphQLParams -} - -export type UseStreamingPaymentProcessorSubscriptionReturnType = ReturnType< - typeof useStreamingPaymentProcessorSubscription -> - -export const useStreamingPaymentProcessorSubscription = ({ - project, - params, -}: UseStreamingPaymentProcessorSubscriptionParams) => { - const inverter = useInverter() - - const memo = React.useMemo(() => { - if (!inverter.data) return null - return inverter.data.graphql.subscription.StreamingPaymentProcessorSubscription( - { - project, - params, - } - ) - }, [!inverter.data, project, params]) - - return memo -} - -export type UseSwapSubscriptionParams = { - project: Array - params?: FormattedGraphQLParams -} - -export type UseSwapSubscriptionReturnType = ReturnType< - typeof useSwapSubscription -> - -export const useSwapSubscription = ({ - project, - params, -}: UseSwapSubscriptionParams) => { - const inverter = useInverter() - - const memo = React.useMemo(() => { - if (!inverter.data) return null - return inverter.data.graphql.subscription.SwapSubscription({ - project, - params, - }) - }, [!inverter.data, project, params]) - - return memo -} - -export type UseWorkflowSubscriptionParams = { - project: Array - params?: FormattedGraphQLParams -} - -export type UseWorkflowSubscriptionReturnType = ReturnType< - typeof useWorkflowSubscription -> - -export const useWorkflowSubscription = ({ - project, - params, -}: UseWorkflowSubscriptionParams) => { - const inverter = useInverter() - - const memo = React.useMemo(() => { - if (!inverter.data) return null - return inverter.data.graphql.subscription.WorkflowSubscription({ - project, - params, - }) - }, [!inverter.data, project, params]) - - return memo -} - -export type UseWorkflowModuleSubscriptionParams = { - project: Array - params?: FormattedGraphQLParams -} - -export type UseWorkflowModuleSubscriptionReturnType = ReturnType< - typeof useWorkflowModuleSubscription -> - -export const useWorkflowModuleSubscription = ({ - project, - params, -}: UseWorkflowModuleSubscriptionParams) => { - const inverter = useInverter() - - const memo = React.useMemo(() => { - if (!inverter.data) return null - return inverter.data.graphql.subscription.WorkflowModuleSubscription({ - project, - params, - }) - }, [!inverter.data, project, params]) - - return memo -} - -export type UseWorkflowModuleTypeSubscriptionParams = { - project: Array - params?: FormattedGraphQLParams -} - -export type UseWorkflowModuleTypeSubscriptionReturnType = ReturnType< - typeof useWorkflowModuleTypeSubscription -> - -export const useWorkflowModuleTypeSubscription = ({ - project, - params, -}: UseWorkflowModuleTypeSubscriptionParams) => { - const inverter = useInverter() - - const memo = React.useMemo(() => { - if (!inverter.data) return null - return inverter.data.graphql.subscription.WorkflowModuleTypeSubscription({ - project, - params, - }) - }, [!inverter.data, project, params]) + return inverter.data!.graphql.subscription(fields) + }, [inverter.data, fields]) return memo }