Skip to content

Commit

Permalink
chore(tests): test that custom config is passed to fetch
Browse files Browse the repository at this point in the history
closes #506
  • Loading branch information
jasonkuhrt committed Apr 17, 2023
1 parent b7025c8 commit dd2d3b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
7 changes: 6 additions & 1 deletion examples/configuration-fetch-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const query = gql`
`

interface Data {
Movie: { releaseDate: string; actors: Array<{ name: string }> }
Movie: {
releaseDate: string
actors: {
name: string
}[]
}
}

const data = await graphQLClient.request<Data>(query)
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ export type MaybeLazy<T> = T | (() => T)

export type RequestDocument = string | DocumentNode

export interface GraphQLClientResponse<T> {
data: T
extensions?: unknown
export interface GraphQLClientResponse<Data> {
status: number
headers: Headers
data: Data
extensions?: unknown
errors?: GraphQLError[]
status: number
}

export type HTTPMethodInput = 'GET' | 'POST' | 'get' | 'post'
Expand Down
18 changes: 18 additions & 0 deletions tests/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { gql, GraphQLClient } from '../src/index.js'
import { Headers } from 'cross-fetch'
import { expect, test, vitest } from 'vitest'

test(`custom fetch configuration is passed through`, async () => {
const fetch = vitest.fn().mockResolvedValue({ ok: true, headers: new Headers(), text: () => ``, data: {} })
const client = new GraphQLClient(`https://foobar`, {
fetch,
// @ts-expect-error extended fetch options
next: {
revalidate: 1,
},
})
await client.request(gql`foo`).catch(() => {
/* ignore */
})
expect(fetch.mock.calls).toMatchObject([[expect.stringMatching(`.*`), { next: { revalidate: 1 } }]])
})

0 comments on commit dd2d3b6

Please sign in to comment.