dotansimha / dotansimha/graphql-code-generator

[graphql-modules-preset] When `encapsulateModuleTypes: prefix`, a type error occurs when adding a custom scalar type.

Open
#10,386 0 comments 0 reactions 1 assignee Claimed by @eddeee888 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?

`@graphql-codegen/graphql-modules-preset`

### Describe the bug

If you set `encapsulateModuleTypes: prefix` and add a custom scalar, the generated code will look like this:

```typescript
export interface Test_Resolvers {
DateTime?: Test_core.Resolvers['DateTime'];
};
```

The generated code does not contain the name `Test_core`, which appears to be a bug.

### Your Example Website or App

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAKjgQwM5wMbQKZwGZQQhwBEAdGQPQwCeY2qJA3AFCiSyIroDmxBRUjyjIwACwCOAGwC0ICABMArlIbMWbAHYxsUPMgy4AItjzBN2BQDFg2KQvQBvFnDgBBWMAyqAXHADkwAr+cAA+ATDAMKoh4f46AB4wsQHISjBi0Cn+WCAg2Nqo2UpQUv6srgCKSro0fv7Int4M2Y2RzQBCNACSwWGpTaqoXQCqqLrlLnBjuvVtXkOTrgByitj1QZMAvqxaOnoGxqbmlgCimkogAGrIUjVOUzNQANLmCvVuRgCy3cvZAOoAJW6ABVToDsoDTgBxEYAGTcENYOw05n2+kMcBMZgsCm6mjA6RsdgccGcK2wAHcPO1fBEojF+vFsEltrsWRx4LR6HAQQwYAB9GkLXAAXjgAAUvABrAA8WCg2DIwuaABoscdccT7KgANr+FUxAC6AD5WBzoFy6Lg+ahBU84OKFUqnuaEpy4NybfyBQBhIj5bSOzA4Mj+vIFGBuj1e3k+1YKMWSmXy0MJ7Dq7Enay2HX69P+U3Ry2e61xu1CwbYADyUAdTtDhprdfGUGLnFjtsFyypTeDUowcudZB71Krmc1lnxhJg2oc+d7VcLZpYFo7Za7AuqtX7KeH26gNAnOMsc71-gPNGX7atPM3T1emgUwazuPOlxudwY+ofb0L7PdEtOx9ABlDBbkadBxQHIdQzAiCoFQdV-CMZAdBBUBsGyEZAThfwVzXW9vQrVD0Mw+CpEaf1NDMHhg2HUjsAw-IKKoiAaOAHgb1LO8fRGUpWKgajaPo0N+KkQThM4gCYw3H0m0BBgICkAA3XQoOTQdU0VZUq0U1BlLUxDj2zM99SbQsmQFAVgFQEFrWrPB8O44CK0vfTDPU3ctP3GpDw81T1JMrVc3nC8-KvItV0A9deIrJ4AqMjSYO0l1W0SoKNRPHMSXPJ5r2i2S4u7NYMsQ7zYJ09MyqQrLTNC88CyiwqSzRXQMWIwUarJKYmwAfj8TcFKUwLEIqOBLwG8tBXckakvGp4pvvdK5vU8bGOY7Alp9Ycav1DbMP-R5cO2itdtWxCf1wo6URazg2oOTFNy+IIFFUSlGmwL5RB61x-AQfwpvJVwAgBqa+DIF6FDeqlPt1I1xpRVx+r8YG-rBvwIahmGPsVeHxtcIJwZASHXveuGEamVxImiLbMZJ7HybxymQc9FkYGJ0noaZ7B8aplB0kyKBOcZ2HmYJkMI0KEWybF3mWZBkopBl7m5b51wkemVsgf5-7AfprmcYpiX5maVAVaN8Wpk1ybUd1jG4Cx2Xcflk2q3Ng3RZd9WQdN1Qul6C2eZ91w-YYUZtc953jet5FWCAA

### Steps to Reproduce the Bug or Issue

1. Go to TypeScript Playground.
2. Paste the existing snapshot test. (https://github.com/dotansimha/graphql-code-generator/blob/master/packages/presets/graphql-modules/tests/__snapshots__/builder.spec.ts.snap#L76-L142)
3. A type error occurs (`Cannot find namespace 'Test_core'`).

### Expected behavior

the correctly modified Snapshot test: https://github.com/tonyfromundefined/graphql-code-generator/commit/eb4e4b3f10c6f06d7045835671790cad0954d71e#diff-3dab381552e68d80f5b1e3b996c6a9a6b05e5a6b29cc670aa171c66a49e9c2d5

### Screenshots or Videos

_No response_

### Platform

- OS: macOS
- NodeJS: 24.4.1
- `graphql` version: 16.11.0
- `@graphql-codegen/graphql-modules-preset` version(s): 4.0.17

### Codegen Config File

```typescript
// codegen.ts
import type { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
schema: "./src/graphql/**/typedefs/*.graphql",
generates: {
"./src/graphql/": {
preset: "graphql-modules",
presetConfig: {
baseTypesPath: "./__generated__/graphql.ts",
importBaseTypesFrom: "../../__generated__/graphql",
filename: "__generated__/module-types.ts",
encapsulateModuleTypes: "prefix",
},
plugins: ["typescript", "typescript-resolvers"],
config: {
contextType: "../Context#Context",
enumsAsTypes: true,
mapperTypeSuffix: "Type",
useTypeImports: true,
mappers: {
// ...
},
},
},
"./src/graphql/__generated__/schema.graphql": {
plugins: ["schema-ast"],
},
},
};

export default config;
```

### Additional context

I've already fixed this issue in my codebase with a patch (1 line). I've forked it and posted [the modified commit](https://github.com/tonyfromundefined/graphql-code-generator/commit/2cbff7345d5c37d2715b0970166e229b92d154de) to my github so you can understand the changes. I'd be happy to submit a PR if you'd like. Thanks!

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.