Skip to content

Commit

Permalink
feat: singleton graph subs
Browse files Browse the repository at this point in the history
  • Loading branch information
mguleryuz committed Oct 9, 2024
1 parent 8029fa8 commit 2f6143f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 445 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
276 changes: 13 additions & 263 deletions src/hooks/use-graphql-query.ts
Original file line number Diff line number Diff line change
@@ -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<keyof GQLBondingCurve>
params?: FormattedGraphQLParams<GQLBondingCurve>
dependencies?: any[]
options?: Except<
UseQueryOptions<Record<string, GQLBondingCurve[]> | 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<keyof GQLLinearVesting>
params?: FormattedGraphQLParams<GQLLinearVesting>
dependencies?: any[]
options?: Except<
UseQueryOptions<Record<string, GQLLinearVesting[]> | 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<keyof GQLStreamingPaymentProcessor>
params?: FormattedGraphQLParams<GQLStreamingPaymentProcessor>
dependencies?: any[]
options?: Except<
UseQueryOptions<
Record<string, GQLStreamingPaymentProcessor[]> | 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<keyof GQLSwap>
params?: FormattedGraphQLParams<GQLSwap>
dependencies?: any[]
options?: Except<
UseQueryOptions<Record<string, GQLSwap[]> | undefined, Error>,
'queryKey' | 'queryFn' | 'enabled'
>
}

export type UseSwapQueryReturnType = ReturnType<typeof useSwapQuery>

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<keyof GQLWorkflow>
params?: FormattedGraphQLParams<GQLWorkflow>
dependencies?: any[]
options?: Except<
UseQueryOptions<Record<string, GQLWorkflow[]> | undefined, Error>,
'queryKey' | 'queryFn' | 'enabled'
>
}

export type UseWorkflowQueryReturnType = ReturnType<typeof useWorkflowQuery>

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<keyof GQLWorkflowModule>
params?: FormattedGraphQLParams<GQLWorkflowModule>
dependencies?: any[]
options?: Except<
UseQueryOptions<Record<string, GQLWorkflowModule[]> | 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<keyof GQLWorkflowModuleType>
params?: FormattedGraphQLParams<GQLWorkflowModuleType>
export type UseGraphQLQueryParams<T extends GraphQLQueryArgs> = {
fields: T
dependencies?: any[]
options?: Except<
UseQueryOptions<Record<string, GQLWorkflowModuleType[]> | undefined, Error>,
UseQueryOptions<GraphQLQueryResult<T> | undefined, Error>,
'queryKey' | 'queryFn' | 'enabled'
>
}

export type UseWorkflowModuleTypeQueryReturnType = ReturnType<
typeof useWorkflowModuleTypeQuery
>
export type UseGraphQLQueryReturnType = ReturnType<typeof useGraphQLQuery>

export const useWorkflowModuleTypeQuery = ({
project,
params,
export const useGraphQLQuery = <T extends GraphQLQueryArgs>({
fields,
dependencies,
options = {},
}: UseWorkflowModuleTypeQueryParams) => {
}: UseGraphQLQueryParams<T>) => {
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,
})
Expand Down
Loading

0 comments on commit 2f6143f

Please sign in to comment.