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.84k stars 1.33k forks source link

1.6.0 breaks meteor support #2502

Closed smeijer closed 5 years ago

smeijer commented 5 years ago

Describe the bug Using version 1.5.0 works fine in meteor. Imports like import { Meteor } from 'meteor/meteor' do not interfere with codegen.

Starting from 1.6.0 however, these import statements make the generator crash.

Unable to load schema from file "c:/dev/my-project/src/server/api/connectors.ts" due to import error: Cannot find module 'meteor/meteor'
Error: Unable to load schema from file "c:/dev/my-project/src/server/api/connectors.ts" due to import error: Cannot find module 'meteor/meteor'
    at tryToLoadFromExport (c:\dev\my-project\node_modules\graphql-toolkit\src\loaders\load-from-code-file.ts:74:11)
    at Object.loadFromCodeFile (c:\dev\my-project\node_modules\graphql-toolkit\src\loaders\load-from-code-file.ts:105:20)

To Reproduce Steps to reproduce the behavior:

  1. Create a meteor project
  2. Add import statement like import { Meteor } from 'meteor/meteor'
  3. Try generate files

Environment:

Additional context

The error is correct, this 'module' doesn't exists, as Meteor handles imports differently. The thing is, this wasn't an issue in 1.5. Did something here change? Can we undo it?

dotansimha commented 5 years ago

@smeijer I think your glob pattern might be too broad, because it causes to load this file, and it fails because Meteor has it's own import system that injects meteor/meteor. Can you please share you codegen config file? Also, can you try to add noRequire as described here: https://github.com/dotansimha/graphql-code-generator/blob/1b2f538dc9ea9f8fa10cf76ecf9171484107fc87/docs/getting-started/schema-field.md#glob-expression ?

smeijer commented 5 years ago

@dotansimha, you're right. Making my glob pattern more strict fixes this issue. Strange thing is, it was working in 1.5.0.

- src/server/api/**/*.ts
+ src/server/api/**/typeDef.ts

The config above, works in 1.6.1. Next step, upgrade 1.6.1 to 1.7.0:

npm install --save-dev @graphql-codegen/add@1.7.0 @graphql-codegen/cli@1.7.0 @graphql-codegen/fragment-matcher@1.7.0 @graphql-codegen/introspection@1.7.0 @graphql-codegen/schema-ast@1.7.0 @graphql-codegen/typescript@1.7.0 @graphql-codegen/typescript-document-nodes@1.7.0 @graphql-codegen/typescript-graphql-files-modules@1.7.0 @graphql-codegen/typescript-mongodb@1.7.0 @graphql-codegen/typescript-resolvers@1.7.0

npm run codegen
  × generated/introspection-result.ts
    Failed to load schema from src/server/api/**/typeDef.ts:

        Unable to load from file "c:\dev\my-project\src\server\api\typeDef.ts": Cannot find module 'c:devmy-projectsrcserverapi       ypeDef.ts'
        Error: Unable to load from file "c:\dev\my-project\src\server\api\typeDef.ts": Cannot find module 'c:devmy-projectsrcserverapi        ypeDef.ts'
    at tryToLoadFromExport (c:\dev\my-project\node_modules\graphql-toolkit\src\loaders\load-from-code-file.ts:85:11)
    at Object.loadFromCodeFile (c:\dev\my-project\node_modules\graphql-toolkit\src\loaders\load-from-code-file.ts:116:20)
    at <anonymous>

        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export "schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
          - String in config file

        Try to use one of above options and run codegen again.

config:

definitions:
  add: &top-comment
    content: >
      /**
       * NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY IT DIRECTLY.
       */

      /* eslint-disable */
  scalars: &scalars
    Date: Date
    Long: number
    JSON: '{ [key: string]: any }'
  namingConvention: &namingConvention
    enumValues: change-case#upperCase
overwrite: true
schema:
  - src/server/api/**/typeDef.ts
require:
  - ts-node/register
generates:
  generated/introspection-result.ts:
    plugins:
      - add: *top-comment
      - 'fragment-matcher'
smeijer commented 5 years ago

I think that bug in 1.7.0 is related to https://github.com/ardatan/graphql-toolkit/issues/231

jbaranski commented 5 years ago

@smeijer I got the same kind of error you did in your stack trace, the paths get mangled on the require call in tryToLoadFromExport because the Windows paths have changed from 1.6.1.

dotansimha commented 5 years ago

Fixed in graphql-toolkit, we'll release a new version of the codegen soon.

dotansimha commented 5 years ago

Fixed in 1.8.0 🚀

jbaranski commented 5 years ago

This still doesn't work for me on Windows.

See https://github.com/ardatan/graphql-toolkit/issues/260

dotansimha commented 5 years ago

@jbaranski we fixed that in graphql-toolkit, and updated here, a fix will be available in the next stable version.