gqlgen icon indicating copy to clipboard operation
gqlgen copied to clipboard

Unpredictable and contradictory naming behavior

Open JonasDoe opened this issue 3 years ago • 1 comments

What happened?

I generated a models_gen.go based on this schema:

type EmailSmtpInfo {
  emailBCC: String
}

and got:

type EmailSMTPInfo struct {
	EmailBcc *string `json:"emailBCC"`
}

The casing was not as I expected: Parts of of the type name (the Smtp) became fully uppercase - in opposite to the GraphQL schema -, and parts of the field name (the "CC" in BCC) became lowercase (and does not comply with the Go naming standard). And a field like Tls, again, would become fully uppercase. Taken together the behavior is a bit surprising (especially regarding the Smtp) and in case of the field name deviating from the Go standard, but my main issue is that the behavior is contradictory (enforcing a uppercase sequence vs preventing it).

(I know that Smtp deviates from the Go standard as well, but I had to decide elsewhere whether I want to enforce uppercase sequences or disallow them, and with the current gqlgen behavior nothing works out.)

What did you expect?

Ideally the generator would honor my typing. If that's not possible, that it would be nice if the casing would be at least consistent (i.e. let Smtp with those lowercase letters and keep lowercase "CC" in Bcc). Since I generate the GraphQL schema itself as well as the whole service utilizing it elsewhere and the generation itself is quite abstract, it's hard to deal with that unpredictable behavior in the result - neither preventing an uppercase sequence nor enforcing it leads to a reliable outcome.

I think if you want to keep features like "automatic abbreviation detection & uppercasing", they should remain optional mappers one can register or define in the yaml.

I tried to enforce at least the predictable naming for fields via directives like @goField(name: "emailBCC"), but even then the result will look like "emailBcc".

Minimal graphql.schema and models to reproduce

Just take this GraphQL schema and generate the model with gqlgen generate.

type EmailSmtpInfo {
  emailBCC: String
}

versions

  • gqlgen version? : v0.13.0
  • go version? : 1.15
  • dep or go modules? No further dependencies in the go.mod

JonasDoe avatar Nov 03 '20 14:11 JonasDoe

Is there any plan to fix this in future versions? This caused an issue for us and to fix we've had to downgrade (from 0.16.0 back to 0.14.0). 🙏

bertiespell avatar Feb 03 '22 08:02 bertiespell