Skip to content

Commit

Permalink
feat(generator): custom headers on introspection request (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Jan 10, 2025
1 parent a7b3ec7 commit 2e5ee8e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/generator/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/generator/config/configInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ export interface ConfigInitSchemaUrl {
type: `url`
url: URL
options?: InputIntrospectionOptions
/**
* HTTP headers that will be sent with the introspection request.
*/
headers?: HeadersInit
}

export type ConfigInitSchema =
| ConfigInitSchemaSdl
| ConfigInitSchemaInstance
| ConfigInitSchemaSdlFile
| ConfigInitSchemaUrl

export interface ConfigInit {
/**
* File system API to use.
Expand Down
16 changes: 15 additions & 1 deletion src/generator/generator/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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`)
})

0 comments on commit 2e5ee8e

Please sign in to comment.