Skip to content

Commit

Permalink
Allow additional graphql-request fetch options configuration. (#491)
Browse files Browse the repository at this point in the history
* allow additional fetch option config of `graphql-request`

* add documentation for `fetchOptions`

---------

Co-authored-by: Conrawl Rogers <me@diizzayy.com>
  • Loading branch information
pixleight and Diizzayy authored Jul 23, 2024
1 parent b7b3d64 commit 5ec0079
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
19 changes: 19 additions & 0 deletions docs/content/1.getting-started/4.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,22 @@ Configuration for the token storage.
}
}
```

### `fetchOptions`

Specify additional fetch options to configure the GraphQL client.

[More information here](https://github.com/jasonkuhrt/graphql-request#configuration)

```ts
'graphql-client': {
clients: {
default: {
host: '<graphql_api>',
fetchOptions: {
errorPolicy: 'ignore' // Ignore incoming errors and resolve like no errors occurred
}
}
}
}
```
2 changes: 1 addition & 1 deletion src/runtime/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export default defineNitroPlugin(() => {
...(conf?.token?.value && { [tokenName]: authToken })
}

GqlNitro.clients[client] = new GraphQLClient(conf.host, { headers })
GqlNitro.clients[client] = new GraphQLClient(conf.host, { headers, ...conf.fetchOptions })
}
})
6 changes: 4 additions & 2 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export default defineNuxtPlugin((nuxtApp) => {
...serverHeaders,
...(proxyCookie && { cookie: requestHeaders?.cookie })
},
...v?.corsOptions
...v?.corsOptions,
...v?.fetchOptions,
}

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

if (reqOpts?.token) { delete reqOpts.token }
return defu(req, reqOpts)
}
},
...v?.fetchOptions,
})
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GraphQLClient } from 'graphql-request'
import type { GraphQLClient, FetchOptions } from 'graphql-request'
import type { GraphQLError } from 'graphql-request/dist/types'
import type { CookieOptions } from 'nuxt/dist/app/composables'

Expand Down Expand Up @@ -127,6 +127,12 @@ export interface GqlClient<T = string> {
* Declare headers that should only be applied to the GraphQL Code Generator.
* */
codegenHeaders?: Record<string, string>

/**
* Additional fetch options to be passed to the GraphQL client.
* @see https://github.com/jasonkuhrt/graphql-request#configuration
*/
fetchOptions?: FetchOptions
}

export interface GqlCodegen {
Expand Down

0 comments on commit 5ec0079

Please sign in to comment.