dotansimha / dotansimha/graphql-code-generator

Cannot find module (calling .tsx file in graphql resolver)

Open
#9,685 0 comments 4 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

_No response_

### Describe the bug

Hopefully this is something simple:

I have a generates: {} with a pretty minimal config, everything works well but when I attempt to import an email template (.tsx file) inside my graphql resolver (.ts) which is completely valid and runs in dev, no matter what I try when I run my codegen script:

graphql-codegen --require tsconfig-paths/register --config codegen.ts

I get: Error: Cannot find module '@api/lib/templates/invite-user'

I tried all the config steps here:

https://the-guild.dev/graphql/codegen/docs/getting-started/esm-typescript-usage

to no avail.

even altering the documents doesn't seem to have an effect:

```
generates: {
// generate types for the main schema
"./src/types/autogenerated/graphql/index.ts": {
config: {
require: ["ts-node/register/transpile-only"],
inputMaybeValue: "T | null",
},
// only generate types for files that don't end in .contentful.graphql or .d.ts
documents: ["src/**/!((*.d)|(*.contentful.graphql)).{ts,tsx}"],
plugins: [
"typescript",
"typescript-operations",
"typescript-react-apollo",
"typescript-resolvers",
],
schema: printSchema(schema), // default schema must be explicitly defined outside of the child configs or codegen will fail
},
//...
}
```

it's as if something causes codegen to walk the entire src tree before it respects the documents array. I'd love to exclude the entire resolvers folder or force it to only walk the client folder where the gql definitions reside. I'm just having a tough time making any headway on this situation. Any help would be appreciated.

### Your Example Website or App

no example repo

### Steps to Reproduce the Bug or Issue

1. import a tsx file.
2. run codegen

### Expected behavior

I expect it to work even when importing tsx files but it's failing

### Screenshots or Videos

_No response_

### Platform

- OS: MacOS
- NodeJS: 18.16.0
- `graphql` version: 16.8.0
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@graphql-codegen/typescript-react-apollo": "^3.3.7",
"@graphql-codegen/typescript-resolvers": "^4.0.1",

### Codegen Config File

```
import { schema } from "./src/api/graphql/pothos-schema";
import type { CodegenConfig } from "@graphql-codegen/cli";
import { printSchema } from "graphql";

/**
* @see https://www.the-guild.dev/graphql/codegen/docs/config-reference/codegen-config
*/
const config: CodegenConfig = {
config: {
addUnderscoreToArgsType: false,
avoidOptionals: false,
hooksImportFrom: "@apollo/react-hooks",
ignoreNoDocuments: true,
maybeValue: "T",
overwrite: true,
reactApolloVersion: 3,
scalars: {
Date: "Date",
DateTime: "string",
Decimal: "number",
Float: "number",
JSON: "unknown",
UUID: "string",
},
skipTypename: true,
transformUnderscore: true,
typesSuffix: "Type",
withHOC: false,
withHooks: true,
withRefetchFn: true,
},
generates: {
// generate types for the main schema (the one in question)
"./src/types/autogenerated/graphql/index.ts": {
config: {
require: ["ts-node/register/transpile-only"],
inputMaybeValue: "T | null",
},
// only generate types for files that don't end in .contentful.graphql or .d.ts
documents: "src/features/**/!((*.d)|(*.contentful.graphql)).{ts,tsx}",
plugins: [
"typescript",
"typescript-operations",
"typescript-react-apollo",
"typescript-resolvers",
],
schema: printSchema(schema), // default schema must be explicitly defined outside of the child configs or codegen will fail
},
// generate types for contentful independent of the main schema
"./src/types/autogenerated/graphql/contentful.ts": {
config: {
avoidOptionals: true,
scalars: {
Date: "string", // contentful uses ISO 8601 strings for dates
DateTime: "string", // contentful uses ISO 8601 strings for dates
Decimal: "number",
Float: "number",
JSON: "unknown",
UUID: "string",
},
typesPrefix: "Contentful",
},
// only generate types for files that end in .contentful.graphql
documents: "src/**/*.contentful.graphql.{ts,tsx}",
plugins: [
"typescript",
"typescript-operations",
"typescript-react-apollo",
"typescript-resolvers",
],
schema:
"https://graphql.contentful.com/content/v1/spaces/hiddenprivatekey",
},
},
hooks: {
afterOneFileWrite: ["eslint --fix", "prettier --write"],
},
overwrite: true,
};

export default config;

```

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.