Khan/genqlient

Field name tranformations

Open

#206 opened on Jun 6, 2022

View on GitHub
 (3 comments) (3 reactions) (0 assignees)Go (141 forks)github user discovery
enhancementhelp wanted

Repository metrics

Stars
 (1,310 stars)
PR merge metrics
 (PR metrics pending)

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