diff --git a/src/generator/config/config.ts b/src/generator/config/config.ts index 260b94aec..9bbc9d249 100644 --- a/src/generator/config/config.ts +++ b/src/generator/config/config.ts @@ -298,7 +298,10 @@ const createConfigSchema = async ( const graffle = Graffle .create() .use(introspection) - .transport({ url: input.schema.url }) + .transport({ + url: input.schema.url, + headers: input.schema.headers, + }) // todo introspection method should not be available without a transport const data = await graffle.introspect() if (!data) { diff --git a/src/generator/config/configInit.ts b/src/generator/config/configInit.ts index 951cdd423..3b7060f92 100644 --- a/src/generator/config/configInit.ts +++ b/src/generator/config/configInit.ts @@ -46,6 +46,10 @@ export interface ConfigInitSchemaUrl { type: `url` url: URL options?: InputIntrospectionOptions + /** + * HTTP headers that will be sent with the introspection request. + */ + headers?: HeadersInit } export type ConfigInitSchema = @@ -53,6 +57,7 @@ export type ConfigInitSchema = | ConfigInitSchemaInstance | ConfigInitSchemaSdlFile | ConfigInitSchemaUrl + export interface ConfigInit { /** * File system API to use. diff --git a/src/generator/generator/generate.test.ts b/src/generator/generator/generate.test.ts index ee29d1cf8..a886430f6 100644 --- a/src/generator/generator/generate.test.ts +++ b/src/generator/generator/generate.test.ts @@ -2,7 +2,8 @@ import { globby } from 'globby' import * as Memfs from 'memfs' import { readFile } from 'node:fs/promises' import * as Path from 'node:path' -import { describe, expect, test } from 'vitest' +import { describe, expect } from 'vitest' +import { test } from '../../../tests/_/helpers.js' import type { ConfigInitSchemaSdl } from '../_.js' import { generate } from './generate.js' @@ -96,3 +97,16 @@ test(`custom scalars module results in client prefilling those custom scalars`, const ScalarTs = Memfs.fs.readFileSync(`./graffle/modules/scalar.ts`, `utf8`) expect(ScalarTs).toMatchSnapshot() }) + +test(`custom headers can be set on introspection request`, async ({ fetch }) => { + await generate({ + schema: { + type: `url`, + url: new URL(`https://example.com`), + headers: { + 'x-custom': `test`, + }, + }, + }).catch(() => {/* ignore */}) + expect(fetch.mock.calls[0]?.[0].headers.get(`x-custom`)).toBe(`test`) +})