graphql-code-generator
graphql-code-generator copied to clipboard
[client-preset] potentially misleading error message
Problem
In the codegen.ts
file you have to specify a directory for the generated code, like this:
const config: CodegenConfig = {
generates: {
'.output/gql/': {
// ...
},
},
}
However, it's now very clear that you have to end your path with "/". All the error message says is:
✖ [client-preset] target output should be a directory, ex: "src/gql/"
I spent a couple of minutes trying to figure it out until I came across this discussion, where other user had the same issue.
Solution
We could enhance the error message by mentioning the need for a trailing "/".
I already made a fork with the suggested addition.
Hey @luvejo do you want to send a PR for this?
This is also a problem if you're trying to run codegen on Windows and using nodejs path
module to build folder path
Indeed, I ran into this on Windows:
[path.join(__dirname, '../../packages/client-sdk/src/gql/')]: {
// ...
}
The culprit is this function:
https://github.com/dotansimha/graphql-code-generator/blob/4acf545ab88b0dadcfd779be977e34c08bfabaf5/packages/presets/client/src/index.ts#L98
because on Windows the baseOutputDir
will look like C:\path\to\packages\client-sdk\src\gql\
.
I think this should be fixable by replacing the hard-coded '/'
character in the test with path.sep
: https://nodejs.org/api/path.html#pathsep
import path from 'node:path';
// ...
const isOutputFolderLike = (baseOutputDir: string) => baseOutputDir.endsWith(path.sep);