dotansimha / dotansimha/graphql-code-generator

Capitalization of operation name odd when using two consecutive capital letters, doesn't match SWC plugin

Open
#9,826 0 comments 0 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?

@graphql-codegen/typescript-operations, @graphql-codegen/client-preset

### Describe the bug

You might decide that this is an issue in the SWC plugin instead of the `typescript-operations` and babel-plugin packages

## Summary

When an operation name has two consecutive capital letters, the generated TS variable name for the document seems to lose the 2nd capital letter. For example, `query findUserByID` creates a variable named `const FindUserByIdDocument`.

This behaviour is consistent with the babel plugin, so not a huge issue, but it is inconsistent with the SWC plugin.

In my opinion the SWC implementation makes sense, but either one is fine as long as they are consistent.

I haven't been able to create a reproduction with the SWC plugin, since I'm not able to get it to work at all (conflict with swc_core), but I do have a fork of it that we use internally and I'm able to reproduce reliably using it.

### Your Example Website or App

https://codesandbox.io/p/devbox/goofy-agnesi-n9gkhp

### Steps to Reproduce the Bug or Issue

```
➜ /workspace git:(master) ✗ npx babel source.ts
import { FindUserByIdDocument } from "./gql/graphql";
const test = FindUserByIdDocument;
```

### Expected behavior

With input

```js
const test = graphql(`
query findUserByID {
user(id: 1) {
id
}
}
`);
```

I would expect the variable to be called `FindUserByIDDocument` (note the case of `ID`).

### Screenshots or Videos

_No response_

### Platform

- OS: Linux
- NodeJS: v20.9.0
- `graphql` version: graphql@16.6.0
- `@graphql-codegen/*` version(s): 4.0.1

### Codegen Config File

```
import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
schema: "schema.graphql",
documents: "test.ts",
generates: {
"gql/": {
preset: "client",
},
},
};

export default config;
```

### Additional context

## SWC Plugin Test Fixture

A test fixture for SWC plugin would be this, which I am able to replicate with `cargo test` locally:

```
//@ts-ignore
const DD = gql(/* GraphQL */ `
query TestDD {
dd
}
`);
```
and output
```
import { TestDdDocument } from "./graphql";
// ...
//@ts-ignore
const DD = TestDdDocument;
```

A quick fix would be changing the capitalize function to match the other two implementations

https://github.com/dotansimha/graphql-code-generator/blob/7a23422dde7229737f37038d0fa2b54e0bce843e/packages/presets/swc-plugin/src/lib.rs#L18-L20

(please also note there's a minor typo in that function name)

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.