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

Unable to disable type checking on schema import

Open edorivai opened this issue 3 years ago • 5 comments

Describe the bug

tl;dr

See minimal reproduction: https://github.com/edorivai/graphql-codegen-transpile-only

# this fails to load the schema due to a TS error in schema.ts
yarn generate

# now try with v1 of the cli - this seems to work
yarn add "@graphql-codegen@1"
yarn generate

Details

When importing the schema from a TS code file, it is not possible to ignore TS errors.

Previously (with @graphql-codegen@1) we could specify a transpile-only ts-node loader:

require:
  - ts-node/register/transpile-only

This would load the schema as long as it was able to construct the schema at runtime, ignoring TS errors.

With version 2 of the codegen CLI, it seems that regardless of setting the ts-node/register/transpile-only require option, it will fail when there are errors in the schema.

This is a very practical problem, because I am loading our schema by requiring our graphql-modules graph, which naturally hooks into our app code. As I'm developing, our app code could contain (intermediate) TS bugs, but it would still be able to generate the schema for codegen to run.

Now that I transpile-only is "ignored", I am forced to fix all TS errors before being able to generate.

One particularly gnarly problem that this causes is when rebasing:

  1. Create a commit with new fields on the schema and corresponding resolver implementations
  2. Perform git rebase
  3. Conflict in generated types - too tedious to fix by hand, so I would just accept "theirs" and regenerate types
  4. Because I picked "theirs" in the previous step, the resolver types are not available any more, this results in TS errors on my resolver implementation.
  5. Due to these TS errors, I now cannot regenerate the types

Ideal fix

Ideally I would be able to use @swc-node/register - since I want to bypass Typechecking, and swc is faster anyway. If that is difficult, ts-node/register/transpile-only would be a good second solution.

Your Example Website or App

https://github.com/edorivai/graphql-codegen-transpile-only

Steps to Reproduce the Bug or Issue

  1. this fails to load the schema due to a TS error in schema.ts
yarn generate
  1. now try with v1 of the cli - this seems to work
yarn add "@graphql-codegen@1"
yarn generate

Expected behavior

I expect graphql-codegen to respect the use of ts-node/register/transpile-only in config.require. Instead, it seems that internally, TS schema files are loaded through ts-node with typechecking enabled.

Screenshots or Videos

No response

Platform

  • OS: Ubuntu 20.04
  • NodeJS: 16.3.2
  • graphql version: 16.5.0
  • @graphql-codegen/* version(s):
├─ @graphql-codegen/[email protected]
├─ @graphql-codegen/[email protected]
├─ @graphql-codegen/[email protected]
├─ @graphql-codegen/[email protected]
├─ @graphql-codegen/[email protected]
├─ @graphql-codegen/[email protected]
│  └─ @graphql-codegen/[email protected]
│     └─ @graphql-codegen/[email protected]
├─ @graphql-codegen/[email protected]
└─ @graphql-codegen/[email protected]

Codegen Config File

schema: schema.ts
documents: document.graphql
require:
  - "ts-node/register/transpile-only"
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-operations

Additional context

No response

edorivai avatar Aug 16 '22 12:08 edorivai

We have exactly the same problem and it's very difficult to identify the actual problem. It seems that the require option is completely ignored. This is currently blocking our development flow.

From our package.json:

    "@graphql-codegen/cli": "2.11.6",
    "@graphql-codegen/core": "2.6.2",
    "@graphql-codegen/introspection": "2.2.1",
    "@graphql-codegen/schema-ast": "2.5.1",
    "@graphql-codegen/typescript": "2.7.3",
    "@graphql-codegen/typescript-resolvers": "2.7.3",

unzico avatar Aug 16 '22 14:08 unzico

@edorivai You should be able to 'fix' this by either setting the transpileOnly option in your tsconfig.json or setting the env var TS_NODE_TRANSPILE_ONLY.

tsconfig.json

{
  "ts-node": { "transpileOnly": true },
}

or

package.json

{
  "scripts": {
    "generate": "cross-env TS_NODE_TRANSPILE_ONLY=true graphql-codegen --config codegen.yml",
  }
}

Despite this fix, this issue is still important because you cannot use the require option. In addition, the documentation leads in a wrong direction:

unzico avatar Aug 16 '22 15:08 unzico

@unzico Thanks, that workaround works great! I agree that the disconnect between documentation and reality is a problem. It would also be nice to be able to swap out ts-node for swc, but this is definitely more of a papercut.

edorivai avatar Aug 17 '22 14:08 edorivai

Running into the same issue. Is the documentation out of date or is this a bug? are you no longer able to use require property anymore? I tried both -r arg and require and neither work

tadhglewis avatar Aug 18 '22 06:08 tadhglewis

I think I have a related issue. This config

require:
  - ts-node/register
schema: ./src/graphql/getSchema.ts
documents: './src/**/*.graphql'
generates:
  ./src/graphql/generated.ts:
    plugins:
      - typescript
      - typescript-operations
      - typed-document-node
      - my-custom-plugin

used to work fine. my-custom-plugin is implemented in my-custom-plugin.ts.

Now, including the register directive results in getSchema.ts being transpiled twice, and codegen fails. Removing it prevents my-custom-plugin.ts from being loaded. What's the correct configuration if both your schema and plugins are written in TypeScript?

tpict avatar Sep 21 '22 16:09 tpict