Field name tranformations
- Dominant language
- Go
- Stars
- 1.3k
- Forks
- 143
- Avg merge
- 4h 50m
- Merged PRs (30d)
- 1
Description
**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**
* As far as I know, it's impossible to achieve this by preprocessing the schema.
* Postprocessing the generated code with gofmt seems brittle.
**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.
Contributor guide
Research direction
Review the generated Go example in lunatrace/cli/gql/gen.go#L1002-L1007 and the referenced protobuf compiler logic in compiler/protogen/protogen.go#L731-L746. Trace where genqlient emits Go field names and JSON tags, then define an opt-in transformation whose completed behavior preserves schema names in tags while using conventional Go casing for fields.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, graphql
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100