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

should be able to override go field capitalization #1447

Closed
dncohen opened this issue Jan 26, 2021 · 2 comments · Fixed by #2237
Closed

should be able to override go field capitalization #1447

dncohen opened this issue Jan 26, 2021 · 2 comments · Fixed by #2237

Comments

@dncohen
Copy link

dncohen commented Jan 26, 2021

What happened?

I've inherited code that currently uses gqlgen v0.7.1. I'm trying to upgrade to 0.13.0, to take advantage of new features.

There's a change to generated field capitalization which ripples through this inherited code base. I'm hoping to override gqlgen's current preferred capitalization. I haven't found any way to do that (short of defining the models manually).

For example, if I define a gql type...

type Something {
  somethingDDL: Int!
  somethingGB: Int!
}

the generated go type is...

type Something struct {
  SomethingDdl int // I want "SomethingDDL" here
  SomethingGb int // I want "SomethingGB" here
}

I've tried to set the name using @goField(name: "SomethingDDL") directives. And I've tried to set the field name in the gqlgen.yml file. Either I haven't done it just right, or the capitalization of generated names cannot be overridden.

What did you expect?

I'd like to better control the capitalization of generated field names. If I could, I'd make the generated code capitalize the first letter of the GQL field name, and leave the other letters unchanged.

It would be a nice feature to have, because gqlgen is opinionated when it comes to capitalizing field names. It insists that "Id" (not an acronym) be spelled "ID", while "GB" (an acronym) be spelled "Gb". And this has changed sometime between version 0.7 and 0.13.

Minimal graphql.schema and models to reproduce

Sorry I haven't put together a minimal schema.

versions

  • gqlgen version?
    0.13.0

  • go version?
    1.15.2

  • dep or go modules?
    modules

@ianling
Copy link
Contributor

ianling commented Jun 8, 2022

@frederikhors I tested this and it works as expected. I think this can be closed, unless the existing solution is not adequate.

edit: I misunderstood the problem. I have recreated the problem below.

Example:

Schema:

type RenameFieldTest {
    badName: String!
    otherField: String!
}

With this block included in gqlgen.yml:

models:
  RenameFieldTest:
    fields:
      badName:
        fieldName: GOODnaME

Generated model:

type RenameFieldTest struct {
	GOODnaMe   string `json:"badName"`  // <----- field name should be GOODnaME, but e is lowercase!
	OtherField string `json:"otherField"`
}

I'll see if I can find a solution to this.

@dncohen
Copy link
Author

dncohen commented Jul 28, 2022

Awesome, thank you.

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

Successfully merging a pull request may close this issue.

2 participants