question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to disable type checking on schema import

See original GitHub issue

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/add@3.2.1
├─ @graphql-codegen/cli@2.11.6
├─ @graphql-codegen/core@2.6.2
├─ @graphql-codegen/plugin-helpers@2.6.2
├─ @graphql-codegen/schema-ast@2.5.1
├─ @graphql-codegen/typescript-operations@2.5.0
│  └─ @graphql-codegen/typescript@2.7.3
│     └─ @graphql-codegen/visitor-plugin-common@2.12.1
├─ @graphql-codegen/typescript@2.7.0
└─ @graphql-codegen/visitor-plugin-common@2.11.0

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
unzicocommented, Aug 16, 2022

@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:

0reactions
tpictcommented, Sep 21, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using eslint with typescript - Unable to resolve path to module
In my eslintrc.js config file, the "import/resolver" object needed to sit within the "rules" node, not the " ...
Read more >
Fixing the "Cannot Use Namespace as a Type" Error in ...
Are you experiencing the “cannot use namespace as a type” error in TypeScript? This error can occur when you try to import types...
Read more >
Import | DataGrip Documentation - JetBrains
In the Database Explorer (View | Tool Windows | Database Explorer), right-click a schema or a table and select Import/Export | Import Data ......
Read more >
Working with JavaScript in Visual Studio Code
More Actions dropdown), then type 'typescript'. Select the TypeScript and JavaScript Language Features extension and press the Disable button. VS Code built-in ...
Read more >
Importing using Oracle Data Pump - AWS Documentation
Transfer the dump file to the target RDS for Oracle DB instance. Take a DB snapshot of your instance. Test the import to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found