Skip to content

Commit 492439f

Browse files
committed
perf: improve request handling
1 parent b164825 commit 492439f

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/runtime/composables/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { GqlSdks, GqClientOps } from '#gql'
99
import type { GqlOps, GqlClients, GqlSdkFuncs } from '#gql'
1010
import { 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+
1218
const 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) => {
213220
export 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
}

src/runtime/plugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ export default defineNuxtPlugin(() => {
3737
nuxtApp._gqlState.value[name] = {
3838
options: opts,
3939
instance: new GraphQLClient(host, {
40-
...opts,
4140
...(v?.preferGETQueries && {
4241
method: 'GET',
4342
jsonSerializer: { parse: JSON.parse, stringify: JSON.stringify }
44-
})
43+
}),
44+
requestMiddleware: (req) => {
45+
const reqOpts = nuxtApp._gqlState.value?.[name]?.options || {}
46+
47+
return defu<RequestInit, [RequestInit]>(req, reqOpts)
48+
}
4549
})
4650
}
4751
}

src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { GraphQLClient } from 'graphql-request'
2-
import type { GraphQLError, PatchedRequestInit } from 'graphql-request/dist/types'
2+
import type { GraphQLError } from 'graphql-request/dist/types'
33

44
type TokenOpts = { name?: string, value?: string, type?: string}
55

@@ -194,5 +194,5 @@ export type GqlError = {
194194

195195
export type OnGqlError = <T>(error: GqlError) => Promise<T> | any
196196

197-
type GqlStateOpts = {instance?: GraphQLClient, options?: PatchedRequestInit}
197+
type GqlStateOpts = {instance?: GraphQLClient, options?: RequestInit}
198198
export type GqlState = Record<string, GqlStateOpts> & { onError?: OnGqlError }

0 commit comments

Comments
 (0)