@@ -62,6 +62,8 @@ export interface GraphQLClientConfig {
6262 endpoint : string ;
6363 /** Default headers to include in all requests */
6464 headers ?: Record < string , string > ;
65+ /** Dynamic headers callback called on every request */
66+ getHeaders ?: ( ) => Record < string , string > ;
6567}
6668
6769let globalConfig : GraphQLClientConfig | null = null ;
@@ -183,6 +185,7 @@ export async function execute<
183185) : Promise < TData > {
184186 const config = getConfig ( ) ;
185187 const url = new URL ( config . endpoint ) ;
188+ const dynamicHeaders = config . getHeaders ?.( ) ?? { } ;
186189
187190 const body = JSON . stringify ( {
188191 query : document ,
@@ -194,6 +197,7 @@ export async function execute<
194197 headers : {
195198 'Content-Type' : 'application/json' ,
196199 ...config . headers ,
200+ ...dynamicHeaders ,
197201 ...options ?. headers ,
198202 } ,
199203 } ;
@@ -234,6 +238,7 @@ export async function executeWithErrors<
234238) : Promise < { data : TData | null ; errors : GraphQLError [ ] | null } > {
235239 const config = getConfig ( ) ;
236240 const url = new URL ( config . endpoint ) ;
241+ const dynamicHeaders = config . getHeaders ?.( ) ?? { } ;
237242
238243 const body = JSON . stringify ( {
239244 query : document ,
@@ -245,6 +250,7 @@ export async function executeWithErrors<
245250 headers : {
246251 'Content-Type' : 'application/json' ,
247252 ...config . headers ,
253+ ...dynamicHeaders ,
248254 ...options ?. headers ,
249255 } ,
250256 } ;
0 commit comments