graphql-code-generator
graphql-code-generator copied to clipboard
Custom TS schema loaders are not working
Which packages are impacted by your issue?
@graphql-codegen/cli
Describe the bug
I am following the guide for creating a custom loader using TS.
It appears that the codegen fails to load the .ts loader file yet works ok with a .js one.
Your Example Website or App
https://codesandbox.io/p/sandbox/graphql-codegen-custom-schema-loader-issue-hkve7f
Steps to Reproduce the Bug or Issue
-
Run
yarn generateThe command fails with an error -
Change
loader.tstoloader.jsincodegen.ts -
Run
yarn generateThis time the command will work
Expected behavior
As a user, I expected that the custom schema loader written in TS will work if I follow the guide
Screenshots or Videos
No response
Platform
- OS: [e.g. macOS, Linux]
- NodeJS: 16.17.0
graphqlversion: 16.2.0@graphql-codegen/*version(s): 2.6.2
Codegen Config File
import { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: {
"schema.graphql": {
// loader: "./loader.js", // JS - ✅ working
loader: "./loader.ts", // TS - ❌ not working
},
},
documents: "document.graphql",
generates: {
"types.ts": { plugins: ["typescript", "typescript-operations"] },
},
};
export default config;
Additional context
Please, note that I am registering the ts-node for the codegen command.
graphql-codegen -r ts-node/register --config ./codegen.ts
Just a note on reproduction details. This is happening to us explicitly on 3.2.2
We have this condegen config:
const config = {
schema: {
[schemaUrl]: {
headers: {
Authorization: AUTHORIZATION
}
}
},
documents: {
'path/to/my/query': {
templateValues: 'my/template/values.json',
loader: './documentLoader.ts'
}
},
config: {
skipTypeName: true
},
generates: {
....
}
}
Which runs just fine on codegen 3.2.1 and lower, but we started a new project where we installed 3.2.2 and it fails not able to find the loader:
On the initial run of generate:local package.json was set to install codegen ^3.2.1 which would make npm fetch the 3.2.2 version. When forcing npm to install 3.2.1 the command ran without issues (yes, we only had a single ^3.2.1 package, so sed worked just fine)
Yes I'm seeing the same 3.2.1 works fine but upgrading to 3.2.2 gives an error of Failed to load custom loader
Was able to fix this by doing the following in codegen.ts in version 5.0.6
graphql-codegen-esm --config ./codegen.ts
import loader from './loader'
import { type CodegenConfig } from '@graphql-codegen/cli'
const config: CodegenConfig = {
...
// @ts-expect-error loader can be a function, even though the type doesn't allow it
documents: {
'path/to/my/query': {
templateValues: 'my/template/values.json',
loader
},
...
}