You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
When the user doesn't have control over naming conventions in the GraphQL schema, using the generated code can be awkward when the schema does not use CamelCase field names.
For example, Hasura's naming convention is to use snake_case for fields, while go convention is lowerCamelCase (or CamelCase). Accessing those fields can result in generated code like the sample below, which is not idiomatic.
Describe the solution you'd like
An option could be added for transformation of field names from other case types to camel case. This transformation would only be applied to Go field names and not the json tags. The option would be disabled by default to preserve backwards compatibility.
Adding such an option would allow users without control of the server to access field names using the conventional case.
Describe alternatives you've considered
As far as I know, it's impossible to achieve this by preprocessing the schema.
Postprocessing the generated code with gofmt seems brittle.
Yuck, those names are indeed a mess! Thanks for the clear example and suggestion; adding this is probably a necessary evil (I don't see any reasonable workarounds either). Honestly there's something to be said for making it the default (which looks to be what protoc does) but that's probably more breakage than it's worth at this point, so an option as you suggest is at least a better place to start.
Hopefully most of the complexity can be kept in names.go (you'll have to thread in the config probably via the generator). You can probably find most of the places to update by grepping for upperFirst.
For the option, maybe names: force_camel or something?
Is your feature request related to a problem? Please describe.
When the user doesn't have control over naming conventions in the GraphQL schema, using the generated code can be awkward when the schema does not use CamelCase field names.
For example, Hasura's naming convention is to use snake_case for fields, while go convention is lowerCamelCase (or CamelCase). Accessing those fields can result in generated code like the sample below, which is not idiomatic.
https://github.com/lunasec-io/lunasec/blob/fb5c86636006283f1758494ba6deab81d66c216b/lunatrace/cli/gql/gen.go#L1002-L1007
Describe the solution you'd like
An option could be added for transformation of field names from other case types to camel case. This transformation would only be applied to Go field names and not the json tags. The option would be disabled by default to preserve backwards compatibility.
Adding such an option would allow users without control of the server to access field names using the conventional case.
Describe alternatives you've considered
Additional context
The go protobuf compiler implements similar functionality:
https://github.com/protocolbuffers/protobuf-go/blob/v1.28.0/compiler/protogen/protogen.go#L731-L746
I'd be happy to help implement this! I wanted to get input from the maintainers on if this feature is within scope and useful first.
The text was updated successfully, but these errors were encountered: