graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

[client-preset] potentially misleading error message

Open larxn opened this issue 1 year ago • 3 comments

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.

larxn avatar Apr 07 '23 19:04 larxn

Hey @luvejo do you want to send a PR for this?

saihaj avatar Apr 17 '23 12:04 saihaj

This is also a problem if you're trying to run codegen on Windows and using nodejs path module to build folder path

nikolay-emrikh-stenn avatar Apr 19 '23 10:04 nikolay-emrikh-stenn

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); 

michaelbromley avatar Feb 12 '24 11:02 michaelbromley