Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow function as valid type for UrlSchemaOptions.customFetch #10150

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/big-kiwis-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphql-codegen/cli": patch
"@graphql-codegen/plugin-helpers": patch
"website": patch
---

Allow functions to be passed as valid values for `UrlSchemaOptions.customFetch`. This was already possible, but the type definitions did not reflect that correctly.
25 changes: 25 additions & 0 deletions packages/graphql-codegen-cli/tests/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,31 @@ describe('Codegen Executor', () => {
expect((global as any).CUSTOM_FETCH_FN_CALLED).toBeTruthy();
});

it('should load schema with custom fetch function', async () => {
let fetchCalledFor = null;

async function myCustomFetch(url: string, _options?: RequestInit): Promise<Response> {
fetchCalledFor = url;
return Promise.resolve(new Response());
}

try {
await executeCodegen({
schema: ['http://www.dummyschema.com/graphql'],
customFetch: myCustomFetch,
documents: ['./tests/test-documents/valid.graphql'],
generates: {
'out1.ts': {
plugins: ['typescript'],
},
},
});
} catch (error) {
expect(error.message).toContain('Failed to load schema from http://www.dummyschema.com/graphql');
}
expect(fetchCalledFor).toBe('http://www.dummyschema.com/graphql');
});

it('should evaluate glob expressions correctly', async () => {
try {
await executeCodegen({
Expand Down
15 changes: 10 additions & 5 deletions packages/utils/plugins-helpers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export namespace Types {
[path: string]: SchemaFromCodeFileOptions;
}

/**
* @description A function to use for fetching the schema.
* @see fetch
*/
export type CustomSchemaFetcher = (url: string, options?: RequestInit) => Promise<Response>;

/**
* @additionalProperties false
* @description Loads a schema from remote endpoint, with custom http options.
Expand All @@ -85,9 +91,9 @@ export namespace Types {
*/
headers?: { [headerName: string]: string };
/**
* @description Specify a Node module name, or a custom file, to be used instead of standard `fetch`
* @description Specify a Node module name, a custom file, or a function, to be used instead of a standard `fetch`.
*/
customFetch?: string;
customFetch?: string | CustomSchemaFetcher;
/**
* @description HTTP Method to use, either POST (default) or GET.
*/
Expand Down Expand Up @@ -407,10 +413,9 @@ export namespace Types {
*/
require?: RequireExtension;
/**
* @description Name for a library that implements `fetch`.
* Use this to tell codegen to use that to fetch schemas in a custom way.
* @description Specify a Node module name, a custom file, or a function, to be used instead of a standard `fetch`.
*/
customFetch?: string;
customFetch?: string | CustomSchemaFetcher;
/**
* @description A pointer(s) to your GraphQL documents: query, mutation, subscription and fragment. These documents will be loaded into for all your output files.
* You can use one of the following:
Expand Down
17 changes: 17 additions & 0 deletions website/src/pages/docs/config-reference/schema-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ const config: CodegenConfig = {
export default config;
```

Alternatively, you can specify a custom fetch function directly:

```ts {7}
import { CodegenConfig } from '@graphql-codegen/cli';
import { myCustomFetch } from './my-custom-fetch.ts'

const config: CodegenConfig = {
schema: [
{
'http://localhost:3000/graphql': {
customFetch: myCustomFetch,
}
}
]
};
export default config;
```

##### `method`

Expand Down