Skip to content

Commit e324382

Browse files
ArminWiebigkeArmin WiebigkeArmin Wiebigkeeddeee888
authored
Allow function as valid type for UrlSchemaOptions.customFetch (#10150)
* Add type CustomSchemaFetcher * Add test for using function as customFetch * Fix linting * Add documentation * Add changeset * Update changeset to remove website as it is not a package --------- Co-authored-by: Armin Wiebigke <[email protected]> Co-authored-by: Armin Wiebigke <[email protected]> Co-authored-by: Eddy Nguyen <[email protected]> Co-authored-by: Eddy Nguyen <[email protected]>
1 parent 98392fc commit e324382

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

.changeset/big-kiwis-move.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-codegen/cli": patch
3+
"@graphql-codegen/plugin-helpers": patch
4+
---
5+
6+
Allow functions to be passed as valid values for `UrlSchemaOptions.customFetch`. This was already possible, but the type definitions did not reflect that correctly.

packages/graphql-codegen-cli/tests/codegen.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,31 @@ describe('Codegen Executor', () => {
967967
expect((global as any).CUSTOM_FETCH_FN_CALLED).toBeTruthy();
968968
});
969969

970+
it('should load schema with custom fetch function', async () => {
971+
let fetchCalledFor = null;
972+
973+
async function myCustomFetch(url: string, _options?: RequestInit): Promise<Response> {
974+
fetchCalledFor = url;
975+
return Promise.resolve(new Response());
976+
}
977+
978+
try {
979+
await executeCodegen({
980+
schema: ['http://www.dummyschema.com/graphql'],
981+
customFetch: myCustomFetch,
982+
documents: ['./tests/test-documents/valid.graphql'],
983+
generates: {
984+
'out1.ts': {
985+
plugins: ['typescript'],
986+
},
987+
},
988+
});
989+
} catch (error) {
990+
expect(error.message).toContain('Failed to load schema from http://www.dummyschema.com/graphql');
991+
}
992+
expect(fetchCalledFor).toBe('http://www.dummyschema.com/graphql');
993+
});
994+
970995
it('should evaluate glob expressions correctly', async () => {
971996
try {
972997
await executeCodegen({

packages/utils/plugins-helpers/src/types.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export namespace Types {
7575
[path: string]: SchemaFromCodeFileOptions;
7676
}
7777

78+
/**
79+
* @description A function to use for fetching the schema.
80+
* @see fetch
81+
*/
82+
export type CustomSchemaFetcher = (url: string, options?: RequestInit) => Promise<Response>;
83+
7884
/**
7985
* @additionalProperties false
8086
* @description Loads a schema from remote endpoint, with custom http options.
@@ -85,9 +91,9 @@ export namespace Types {
8591
*/
8692
headers?: { [headerName: string]: string };
8793
/**
88-
* @description Specify a Node module name, or a custom file, to be used instead of standard `fetch`
94+
* @description Specify a Node module name, a custom file, or a function, to be used instead of a standard `fetch`.
8995
*/
90-
customFetch?: string;
96+
customFetch?: string | CustomSchemaFetcher;
9197
/**
9298
* @description HTTP Method to use, either POST (default) or GET.
9399
*/
@@ -407,10 +413,9 @@ export namespace Types {
407413
*/
408414
require?: RequireExtension;
409415
/**
410-
* @description Name for a library that implements `fetch`.
411-
* Use this to tell codegen to use that to fetch schemas in a custom way.
416+
* @description Specify a Node module name, a custom file, or a function, to be used instead of a standard `fetch`.
412417
*/
413-
customFetch?: string;
418+
customFetch?: string | CustomSchemaFetcher;
414419
/**
415420
* @description A pointer(s) to your GraphQL documents: query, mutation, subscription and fragment. These documents will be loaded into for all your output files.
416421
* You can use one of the following:

website/src/pages/docs/config-reference/schema-field.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,23 @@ const config: CodegenConfig = {
141141
export default config;
142142
```
143143

144+
Alternatively, you can specify a custom fetch function directly:
145+
146+
```ts {7}
147+
import { CodegenConfig } from '@graphql-codegen/cli';
148+
import { myCustomFetch } from './my-custom-fetch.ts'
149+
150+
const config: CodegenConfig = {
151+
schema: [
152+
{
153+
'http://localhost:3000/graphql': {
154+
customFetch: myCustomFetch,
155+
}
156+
}
157+
]
158+
};
159+
export default config;
160+
```
144161

145162
##### `method`
146163

0 commit comments

Comments
 (0)