Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
louy opened this issue Jan 24, 2024 · 0 comments

Comments

@louy
Copy link

louy commented Jan 24, 2024

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

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

fn capetalize(s: &str) -> String {
format!("{}{}", (&s[..1].to_string()).to_uppercase(), &s[1..])
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant