Unable to disable type checking on schema import
See original GitHub issueDescribe 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:
- Create a commit with new fields on the schema and corresponding resolver implementations
- Perform git rebase
- Conflict in generated types - too tedious to fix by hand, so I would just accept “theirs” and regenerate types
- 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.
- 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
- 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
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:
- Created a year ago
- Comments:5 (1 by maintainers)
@edorivai You should be able to ‘fix’ this by either setting the
transpileOnly
option in yourtsconfig.json
or setting the env varTS_NODE_TRANSPILE_ONLY
.tsconfig.json
or
package.json
Despite this fix, this issue is still important because you cannot use the
require
option. In addition, the documentation leads in a wrong direction:I think I have a related issue. This config
used to work fine.
my-custom-plugin
is implemented inmy-custom-plugin.ts
.Now, including the
register
directive results ingetSchema.ts
being transpiled twice, and codegen fails. Removing it preventsmy-custom-plugin.ts
from being loaded. What’s the correct configuration if both your schema and plugins are written in TypeScript?