dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.86k stars 1.33k forks source link

Preset Loading is masking module import errors #9436

Open lachieh opened 1 year ago

lachieh commented 1 year ago

Which packages are impacted by your issue?

@graphql-codegen/cli

Describe the bug

When using a preset that itself has missing dependencies, the preset loading function masks the error saying that it could not find the preset when, in fact, it could not find one of the preset's dependencies.

In my case, the error being covered up is below. This is a separate issue which I haven't solved yet, but it took a fair bit of work to even discover why the @graphql-codegen/client-preset appeared to not be loading. (The underlying issue with @ardatan/relay-compiler not loading the babel deps is something I still need to resolve, so if anyone has pointers there, I'm all ears. I'll open a separate issue for that if I'm not able to resolve it today.)

As you can see from the stack, the preset loads fine, but then another dependency fails to load.

Error: Cannot find module '@babel/runtime/helpers/objectSpread2'
Require stack:
- /Users/lheywood/project-path/node_modules/@ardatan/relay-compiler/lib/transforms/SkipRedundantNodesTransform.js
- /Users/lheywood/project-path/node_modules/@graphql-tools/relay-operation-optimizer/cjs/index.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/visitor-plugin-common/cjs/optimize-operations.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/visitor-plugin-common/cjs/index.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/gql-tag-operations/cjs/index.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/client-preset/cjs/index.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/codegen.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/generate-and-save.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/cli.js
- /Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/bin.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/lheywood/project-path/node_modules/@ardatan/relay-compiler/lib/transforms/SkipRedundantNodesTransform.js:15:45)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/lheywood/project-path/node_modules/@ardatan/relay-compiler/lib/transforms/SkipRedundantNodesTransform.js',
    '/Users/lheywood/project-path/node_modules/@graphql-tools/relay-operation-optimizer/cjs/index.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/visitor-plugin-common/cjs/optimize-operations.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/visitor-plugin-common/cjs/index.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/gql-tag-operations/cjs/index.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/client-preset/cjs/index.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/codegen.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/generate-and-save.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/cli.js',
    '/Users/lheywood/project-path/node_modules/@graphql-codegen/cli/cjs/bin.js'
  ]
}

Your Example Website or App

https://stackblitz.com/edit/github-qesmwt?file=package.json,codegen.ts,custom/index.js

Steps to Reproduce the Bug or Issue

  1. run yarn generate to see command fail
  2. comment out the broken import in custom/index.js
  3. run yarn generate again to see command pass

Expected behavior

I expected to see an error making mention of the missing module, however because the preset loader function looks for any MODULE_NOT_FOUND error, the displayed message is that it could not find the preset at all, not it's dependency.

Screenshots or Videos

No response

Platform

Codegen Config File

import {CodegenConfig} from '@graphql-codegen/cli';

const config: CodegenConfig = {
    schema: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
    documents: ['src/**/*.tsx'],
    ignoreNoDocuments: true, // for better experience with the watcher
    generates: {
        './src/gql/': {
            preset: 'client',
        },
    },
};

export default config;
darklight721 commented 1 year ago

I was also having the same issue. But solved mine by installing @babel/runtime.