Skip to content

Commit 21ebb67

Browse files
authored
Merge pull request #676 from constructive-io/feat/wrap-execute-codegen
feat(codegen): add getHeaders callback for dynamic request headers
2 parents 27b059d + e00ea2b commit 21ebb67

4 files changed

Lines changed: 2473 additions & 7029 deletions

File tree

graphql/codegen/src/core/codegen/orm/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ export function generateOrm(options: GenerateOrmOptions): GenerateOrmResult {
156156
}
157157

158158
// Re-export generators for direct use
159-
export { generateOrmClientFile, generateQueryBuilderFile, generateSelectTypesFile } from './client-generator';
159+
export {
160+
generateOrmClientFile,
161+
generateQueryBuilderFile,
162+
generateSelectTypesFile,
163+
} from './client-generator';
160164
export { generateModelFile, generateAllModelFiles } from './model-generator';
161165
export { generateCustomQueryOpsFile, generateCustomMutationOpsFile } from './custom-ops-generator';
162166
export { generateModelsBarrel, generateTypesBarrel } from './barrel';

graphql/codegen/src/core/codegen/templates/client.browser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface GraphQLClientConfig {
1717
endpoint: string;
1818
/** Default headers to include in all requests */
1919
headers?: Record<string, string>;
20+
/** Dynamic headers callback called on every request */
21+
getHeaders?: () => Record<string, string>;
2022
}
2123

2224
let globalConfig: GraphQLClientConfig | null = null;
@@ -139,12 +141,14 @@ export async function execute<
139141
options?: ExecuteOptions
140142
): Promise<TData> {
141143
const config = getConfig();
144+
const dynamicHeaders = config.getHeaders?.() ?? {};
142145

143146
const response = await fetch(config.endpoint, {
144147
method: 'POST',
145148
headers: {
146149
'Content-Type': 'application/json',
147150
...config.headers,
151+
...dynamicHeaders,
148152
...options?.headers,
149153
},
150154
body: JSON.stringify({
@@ -180,12 +184,14 @@ export async function executeWithErrors<
180184
options?: ExecuteOptions
181185
): Promise<{ data: TData | null; errors: GraphQLError[] | null }> {
182186
const config = getConfig();
187+
const dynamicHeaders = config.getHeaders?.() ?? {};
183188

184189
const response = await fetch(config.endpoint, {
185190
method: 'POST',
186191
headers: {
187192
'Content-Type': 'application/json',
188193
...config.headers,
194+
...dynamicHeaders,
189195
...options?.headers,
190196
},
191197
body: JSON.stringify({

graphql/codegen/src/core/codegen/templates/client.node.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6769
let 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

Comments
 (0)