Skip to content

Commit 5ec0079

Browse files
pixleightDiizzayy
andauthored
Allow additional graphql-request fetch options configuration. (#491)
* allow additional fetch option config of `graphql-request` * add documentation for `fetchOptions` --------- Co-authored-by: Conrawl Rogers <[email protected]>
1 parent b7b3d64 commit 5ec0079

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

docs/content/1.getting-started/4.configuration.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,22 @@ Configuration for the token storage.
283283
}
284284
}
285285
```
286+
287+
### `fetchOptions`
288+
289+
Specify additional fetch options to configure the GraphQL client.
290+
291+
[More information here](https://github.com/jasonkuhrt/graphql-request#configuration)
292+
293+
```ts
294+
'graphql-client': {
295+
clients: {
296+
default: {
297+
host: '<graphql_api>',
298+
fetchOptions: {
299+
errorPolicy: 'ignore' // Ignore incoming errors and resolve like no errors occurred
300+
}
301+
}
302+
}
303+
}
304+
```

src/runtime/nitro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export default defineNitroPlugin(() => {
2222
...(conf?.token?.value && { [tokenName]: authToken })
2323
}
2424

25-
GqlNitro.clients[client] = new GraphQLClient(conf.host, { headers })
25+
GqlNitro.clients[client] = new GraphQLClient(conf.host, { headers, ...conf.fetchOptions })
2626
}
2727
})

src/runtime/plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export default defineNuxtPlugin((nuxtApp) => {
4545
...serverHeaders,
4646
...(proxyCookie && { cookie: requestHeaders?.cookie })
4747
},
48-
...v?.corsOptions
48+
...v?.corsOptions,
49+
...v?.fetchOptions,
4950
}
5051

5152
nuxtApp._gqlState.value[name] = {
@@ -97,7 +98,8 @@ export default defineNuxtPlugin((nuxtApp) => {
9798

9899
if (reqOpts?.token) { delete reqOpts.token }
99100
return defu(req, reqOpts)
100-
}
101+
},
102+
...v?.fetchOptions,
101103
})
102104
}
103105
}

src/types.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GraphQLClient } from 'graphql-request'
1+
import type { GraphQLClient, FetchOptions } from 'graphql-request'
22
import type { GraphQLError } from 'graphql-request/dist/types'
33
import type { CookieOptions } from 'nuxt/dist/app/composables'
44

@@ -127,6 +127,12 @@ export interface GqlClient<T = string> {
127127
* Declare headers that should only be applied to the GraphQL Code Generator.
128128
* */
129129
codegenHeaders?: Record<string, string>
130+
131+
/**
132+
* Additional fetch options to be passed to the GraphQL client.
133+
* @see https://github.com/jasonkuhrt/graphql-request#configuration
134+
*/
135+
fetchOptions?: FetchOptions
130136
}
131137

132138
export interface GqlCodegen {

0 commit comments

Comments
 (0)