@@ -5,8 +5,8 @@ import type { GqlState, GqlConfig, GqlError, OnGqlError } from '../../types'
55import { deepmerge } from '../utils'
66// @ts -ignore
77// eslint-disable-next-line import/named
8- import { GqlSdks , GqlInstance , GqlOperations } from '#gql'
9- import type { GqlClients , GqlSdkValues , GqlSdkFuncs } from '#gql'
8+ import { GqlSdks , GqlOperations } from '#gql'
9+ import type { GqlClients , GqlSdkFuncs } from '#gql'
1010import { useState , useNuxtApp , useAsyncData , useRuntimeConfig } from '#imports'
1111
1212const useGqlState = ( ) : Ref < GqlState > => {
@@ -204,12 +204,25 @@ export const useGqlCors = (cors: GqlCors) => {
204204 setGqlState ( { client, patch : { mode, credentials } } )
205205}
206206
207- export const useGql = ( ) => {
207+ export const useGql = ( ) : ( <
208+ T extends keyof GqlSdkFuncs ,
209+ R extends ReturnType < GqlSdkFuncs [ T ] > ,
210+ P extends Parameters < GqlSdkFuncs [ T ] > [ '0' ] ,
211+ > ( args : { operation : T , variables ?: P } ) => R ) &
212+ ( <
213+ T extends keyof GqlSdkFuncs ,
214+ R extends ReturnType < GqlSdkFuncs [ T ] > ,
215+ P extends Parameters < GqlSdkFuncs [ T ] > [ '0' ] ,
216+ > ( operation : T , variables ?: P ) => R ) => {
208217 const state = useGqlState ( )
209218 const errState = useGqlErrorState ( )
210219
211- const handle = < T extends GqlClients > ( client ?: T ) => {
212- client = client || 'default' as T
220+ return ( ...args : any [ ] ) => {
221+ const operation = ( typeof args ?. [ 0 ] !== 'string' && 'operation' in args ?. [ 0 ] ? args [ 0 ] . operation : args [ 0 ] ) ?? undefined
222+ const variables = ( typeof args ?. [ 0 ] !== 'string' && 'variables' in args ?. [ 0 ] ? args [ 0 ] . variables : args [ 1 ] ) ?? undefined
223+
224+ const client = Object . keys ( GqlOperations ) . find ( k => GqlOperations [ k ] . includes ( operation ) ) ?? 'default'
225+
213226 const { instance } = state . value ?. [ client ]
214227
215228 return GqlSdks [ client ] ( instance , async ( action , operationName , operationType ) : Promise < any > => {
@@ -230,10 +243,8 @@ export const useGql = () => {
230243
231244 throw errState . value
232245 }
233- } ) as GqlSdkValues < T >
246+ } ) [ operation ] ( variables )
234247 }
235-
236- return { handle }
237248}
238249
239250/**
@@ -294,7 +305,6 @@ export function useAsyncGql (...args: any[]) {
294305 const operation = ( typeof args ?. [ 0 ] !== 'string' && 'operation' in args ?. [ 0 ] ? args [ 0 ] . operation : args [ 0 ] ) ?? undefined
295306 const variables = ( typeof args ?. [ 0 ] !== 'string' && 'variables' in args ?. [ 0 ] ? args [ 0 ] . variables : args [ 1 ] ) ?? undefined
296307 const options = ( typeof args ?. [ 0 ] !== 'string' && 'options' in args ?. [ 0 ] ? args [ 0 ] . options : args [ 2 ] ) ?? undefined
297- const client = Object . keys ( GqlOperations ) . find ( k => GqlOperations [ k ] . includes ( operation ) ) ?? 'default'
298- const key = hash ( { operation, client, variables } )
299- return useAsyncData ( key , ( ) => GqlInstance ( ) . handle ( client as GqlClients ) [ operation ] ( variables ) , options )
308+ const key = hash ( { operation, variables } )
309+ return useAsyncData ( key , ( ) => useGql ( ) ( operation , variables ) , options )
300310}
0 commit comments