@@ -9,6 +9,12 @@ import { GqlSdks, GqClientOps } from '#gql'
99import type { GqlOps , GqlClients , GqlSdkFuncs } from '#gql'
1010import { useState , useNuxtApp , useAsyncData , useRuntimeConfig } from '#imports'
1111
12+ const getGqlClient = ( client ?: GqlClients , state ?: Ref < GqlState > ) : GqlClients => {
13+ if ( ! state ) { state = useGqlState ( ) }
14+
15+ return client || ( state . value ?. default ? 'default' : Object . keys ( state . value ) [ 0 ] ) as GqlClients
16+ }
17+
1218const useGqlState = ( ) : Ref < GqlState > => {
1319 const nuxtApp = useNuxtApp ( ) as Partial < { _gqlState : Ref < GqlState > } >
1420
@@ -21,9 +27,11 @@ const useGqlState = (): Ref<GqlState> => {
2127 *
2228 * */
2329// The decision was made to avert using `GraphQLClient's` `setHeader(s)` helper in favor of reactivity and more granular control.
24- const setGqlState = ( { client = 'default' , patch } : { client : GqlClients , patch : RequestInit } ) => {
30+ const setGqlState = ( { client, patch } : { client : GqlClients , patch : RequestInit } ) => {
2531 const state = useGqlState ( )
2632
33+ client = getGqlClient ( client , state )
34+
2735 const reset = ! Object . keys ( patch ) . length
2836 const partial = ! reset && Object . keys ( patch ) . some ( key => typeof patch [ key ] !== 'object'
2937 ? ! patch [ key ]
@@ -59,9 +67,6 @@ const setGqlState = ({ client = 'default', patch }: {client: GqlClients, patch:
5967 } else {
6068 state . value [ client ] . options = deepmerge ( state . value [ client ] . options , patch )
6169 }
62-
63- // @ts -ignore
64- state . value [ client ] . instance . options = state . value [ client ] . options
6570}
6671
6772/**
@@ -164,10 +169,12 @@ export function useGqlToken (...args: any[]) {
164169 args = args || [ ]
165170
166171 const token = typeof args [ 0 ] === 'string' ? args [ 0 ] : args ?. [ 0 ] ?. token
167- const client = args [ 0 ] ?. client || args ?. [ 1 ] ?. client
172+ let client = args [ 0 ] ?. client || args ?. [ 1 ] ?. client
168173 let config = args [ 0 ] ?. config || args ?. [ 1 ] ?. config
169174
170- const clientConfig = ( useRuntimeConfig ( ) ?. public ?. [ 'graphql-client' ] as GqlConfig ) ?. clients ?. [ client || 'default' ]
175+ client = getGqlClient ( client )
176+
177+ const clientConfig = ( useRuntimeConfig ( ) ?. public ?. [ 'graphql-client' ] as GqlConfig ) ?. clients ?. [ client ]
171178
172179 config = {
173180 ...DEFAULT_AUTH ,
@@ -213,9 +220,7 @@ export const useGqlCors = (cors: GqlCors) => {
213220export const useGqlHost = ( host ?: string , client ?: GqlClients ) => {
214221 const state = useGqlState ( )
215222
216- if ( ! client ) {
217- client = state . value ?. default ? 'default' : Object . keys ( state . value ) [ 0 ] as GqlClients
218- }
223+ client = getGqlClient ( client , state )
219224
220225 return state . value ?. [ client ] . instance . setEndpoint ( host )
221226}
0 commit comments