graphql-code-generator: More verbose error messages.

Is your feature request related to a problem? Please describe. Currently it’s quite difficult to discern what exactly is going wrong if there’s an error. Error messages are simply along the lines of: An error has occurred in one of your documents.

Describe the solution you’d like I’d prefer the ability to either set a flag that makes it more verbose, or simply enable it by default to include the exact file and lines that are erroneous.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 24
  • Comments: 35 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Good god please fix this. I love you guys but this makes be cry.

Thank you all and sorry for the inconvenience. @DAB0mB is working on a fix now 😃

I Have the same issue. I’ve tracked the problem to checkValidationErrors in graphql-toolkit

Problem is in throw new Error("Found " + errorCount + " error" + (errorCount > 1 ? 's' : '') + " in your documents!");. The function collects the errors but does not pass them to the exception.

This did the trick for me: throw new Error("Found " + errorCount + " error" + (errorCount > 1 ? 's' : '') + " in your documents!" + errors.join());

Fixed in 0.18.0 🎉

Setting verbose:true in config file fixed this for me

I wound up writing something like @williamluke4:

errors.forEach(file => {
    console.warn(`${file.filePath}:`);
    file.errors.forEach(e =>{
        const locs = e.locations.map(l => `${l.line}:${l.column}`).join(", ")
        console.warn(`  ${e.message} (line ${locs})`);
    })
})

Running with e.g. ./node_modules/.bin/gql-gen |less, this will present errors like this:

modules/client/graphql/queries/FooBar.graphql:
  Variable "$baz is not defined by operation "FooBar". (line 2:15, 1:1)

Hopefully this helps others get unstuck, but for graphql-code-generator we should figure out how to present errors nicely both with and without a TTY.

For now, a quick and easy way to get something in both scenarios is to catch the exception from graphql-toolkit’s checkValidationErrors() and rethrow with more detail:

try {
    graphql_toolkit_1.checkValidationErrors(errors);
} catch (e) {
    const errorsString = errors.map(file => {
        return `  ${file.filePath}:\n` +
            file.errors.map(err => {
                const locs = err.locations.map(l => `${l.line}:${l.column}`).join(", ")
                    return `  ${err.message} (line ${locs})`
            }).join("\n")
    }).join("\n")
    throw new Error(errorsString)
}

Have the same problem.

@graphql-codegen/cli”: “3.0.0”, “@graphql-codegen/typescript”: “3.0.0”, “@graphql-codegen/typescript-document-nodes”: “3.0.0”, “@graphql-codegen/typescript-operations”: “3.0.0”,

Windows 11, Node v18.1.0

output looks like this:

yarn run regenerate-schema
yarn run v1.22.18
$ graphql-codegen --config graphql-codegen.yml
✔ Parse Configuration
⚠ Generate outputs
  ❯ Generate to src/shared/graphql.ts
    ✔ Load GraphQL schemas
    ✔ Load GraphQL documents
    ✖ GraphQL Document Validation failed with 1 errors;
      Error 0: Fields "identifier" confli…
error Command failed with exit code 1.

Not much info… “Solved” as described above, with adding console.log to \node_modules@graphql-codegen\core\cjs\codegen.js

We’ve encountered the same issue in our team and traced it down to a downstream change. codegen-cli makes use of the listr2 package to render the console output. Listr2 made in change beginning with v3.3.0 introducing the “formatOutput” render-parameter on its DefaultRenderer with the default option of “truncate”. This truncates any output that exceeds the current console width… 2 possible solutions here…

  • quick user side fix: configure your codegen session with “verbose: true” as this uses some unformatted console output
  • fix for the developers: set the formatOutput to “wrap” on the renderOptions in the codegen.ts file

Cheers Alex

I’m adding tests to codegen and then we can release

FWIW, I was able to figure out my bug by editing node_modules/graphql-code-generator/dist/execute-plugin.js and adding a console.log():

I changed your code to be console.log(JSON.stringify(errors)); so that I could see the expanded error messages.

Have the same problem.

@graphql-codegen/cli”: “3.0.0”, “@graphql-codegen/typescript”: “3.0.0”, “@graphql-codegen/typescript-document-nodes”: “3.0.0”, “@graphql-codegen/typescript-operations”: “3.0.0”,

Windows 11, Node v18.1.0

output looks like this:

yarn run regenerate-schema
yarn run v1.22.18
$ graphql-codegen --config graphql-codegen.yml
✔ Parse Configuration
⚠ Generate outputs
  ❯ Generate to src/shared/graphql.ts
    ✔ Load GraphQL schemas
    ✔ Load GraphQL documents
    ✖ GraphQL Document Validation failed with 1 errors;
      Error 0: Fields "identifier" confli…
error Command failed with exit code 1.

Not much info… “Solved” as described above, with adding console.log to \node_modules@graphql-codegen\core\cjs\codegen.js

@dotansimha update: we use concurrently package to run gql-gen and a couple of other tasks in parallel. It’s not working with it, but a standalone command works fine.

I’m on Windows - and the most I can get out of it is: Found 2 errors in your documents I can see that the validator throws a DetailedError with actual detail - but I can’t get the console to actually log it. (unless I hack the source)

@dotansimha I’m using bash

I’m having trouble with the download of a remote schema failing, and I don’t know exactly what it’s trying to download, nor do I know what the server is saying back. It’d be awesome if there was a --debug flag or something so I could see what it’s doing to help me troubleshoot.