Skip to content

Commit da75426

Browse files
Dignidanielroe
andauthored
feat: add context to query (#549)
* feat: add context to query Add Context to useAsyncQuery and useLazyAsynQuery. Extended prep function to extract context and forward it to apollo client. This allows context configuration per query, e.g. custom headers. * Apply suggestions from code review Co-authored-by: Daniel Roe <[email protected]> --------- Co-authored-by: Daniel Roe <[email protected]>
1 parent c9a9914 commit da75426

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/runtime/composables.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { hash } from 'ohash'
22
import { print } from 'graphql'
3-
import type { OperationVariables, QueryOptions } from '@apollo/client'
3+
import type { OperationVariables, QueryOptions, DefaultContext } from '@apollo/client'
44
import type { AsyncData } from 'nuxt/dist/app/composables'
55
import type { NuxtAppApollo } from '../types'
66
import { ref, useCookie, useNuxtApp, useAsyncData } from '#imports'
@@ -18,7 +18,7 @@ type TAsyncQuery<T> = {
1818

1919
export function useAsyncQuery <T> (opts: TAsyncQuery<T>): AsyncData<T, Error>
2020
export function useAsyncQuery <T> (query: TQuery<T>, clientId?: string): AsyncData<T, Error>
21-
export function useAsyncQuery <T> (query: TQuery<T>, variables?: TVariables<T>, clientId?: string): AsyncData<T, Error>
21+
export function useAsyncQuery <T> (query: TQuery<T>, variables?: TVariables<T>, clientId?: string, context?: DefaultContext): AsyncData<T, Error>
2222

2323
export function useAsyncQuery <T> (...args: any) {
2424
const { key, fn } = prep(...args)
@@ -27,7 +27,7 @@ export function useAsyncQuery <T> (...args: any) {
2727

2828
export function useLazyAsyncQuery <T> (opts: TAsyncQuery<T>): AsyncData<T, Error>
2929
export function useLazyAsyncQuery <T> (query: TQuery<T>, clientId?: string): AsyncData<T, Error>
30-
export function useLazyAsyncQuery <T> (query: TQuery<T>, variables?: TVariables<T>, clientId?: string): AsyncData<T, Error>
30+
export function useLazyAsyncQuery <T> (query: TQuery<T>, variables?: TVariables<T>, clientId?: string, context?: DefaultContext): AsyncData<T, Error>
3131

3232
export function useLazyAsyncQuery <T> (...args: any) {
3333
const { key, fn } = prep(...args)
@@ -40,6 +40,7 @@ const prep = (...args: any) => {
4040
const query = args?.[0]?.query || args?.[0]
4141
const cache = args?.[0]?.cache ?? true
4242
const variables = args?.[0]?.variables || (typeof args?.[1] !== 'string' && args?.[1]) || undefined
43+
const context = args?.[0]?.context
4344
let clientId = args?.[0]?.clientId || (typeof args?.[1] === 'string' && args?.[1]) || undefined
4445

4546
if (!clientId || !clients?.[clientId]) {
@@ -48,7 +49,7 @@ const prep = (...args: any) => {
4849

4950
const key = args?.[0]?.key || hash({ query: print(query), variables, clientId })
5051

51-
const fn = () => clients![clientId]?.query({ query, variables, fetchPolicy: 'no-cache' }).then(r => r.data)
52+
const fn = () => clients![clientId]?.query({ query, variables, fetchPolicy: 'no-cache', context }).then(r => r.data)
5253

5354
return { key, query, clientId, variables, fn }
5455
}

0 commit comments

Comments
 (0)