Skip to content

Commit dd2d3b6

Browse files
committed
chore(tests): test that custom config is passed to fetch
closes #506
1 parent b7025c8 commit dd2d3b6

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

examples/configuration-fetch-options.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ const query = gql`
1919
`
2020

2121
interface Data {
22-
Movie: { releaseDate: string; actors: Array<{ name: string }> }
22+
Movie: {
23+
releaseDate: string
24+
actors: {
25+
name: string
26+
}[]
27+
}
2328
}
2429

2530
const data = await graphQLClient.request<Data>(query)

src/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ export type MaybeLazy<T> = T | (() => T)
8181

8282
export type RequestDocument = string | DocumentNode
8383

84-
export interface GraphQLClientResponse<T> {
85-
data: T
86-
extensions?: unknown
84+
export interface GraphQLClientResponse<Data> {
85+
status: number
8786
headers: Headers
87+
data: Data
88+
extensions?: unknown
8889
errors?: GraphQLError[]
89-
status: number
9090
}
9191

9292
export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post'

tests/fetch.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { gql, GraphQLClient } from '../src/index.js'
2+
import { Headers } from 'cross-fetch'
3+
import { expect, test, vitest } from 'vitest'
4+
5+
test(`custom fetch configuration is passed through`, async () => {
6+
const fetch = vitest.fn().mockResolvedValue({ ok: true, headers: new Headers(), text: () => ``, data: {} })
7+
const client = new GraphQLClient(`https://foobar`, {
8+
fetch,
9+
// @ts-expect-error extended fetch options
10+
next: {
11+
revalidate: 1,
12+
},
13+
})
14+
await client.request(gql`foo`).catch(() => {
15+
/* ignore */
16+
})
17+
expect(fetch.mock.calls).toMatchObject([[expect.stringMatching(`.*`), { next: { revalidate: 1 } }]])
18+
})

0 commit comments

Comments
 (0)