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

Field name tranformations #206

Open
ajvpot opened this issue Jun 6, 2022 · 3 comments
Open

Field name tranformations #206

ajvpot opened this issue Jun 6, 2022 · 3 comments
Labels
enhancement New feature or request help wanted Issues that anyone could pick up and implement if useful to them

Comments

@ajvpot
Copy link

ajvpot commented Jun 6, 2022

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.

@ajvpot ajvpot added the enhancement New feature or request label Jun 6, 2022
@benjaminjkraft
Copy link
Collaborator

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?

@benjaminjkraft benjaminjkraft added the help wanted Issues that anyone could pick up and implement if useful to them label Aug 20, 2022
@benjaminjkraft
Copy link
Collaborator

This can soon be added to the casing option section from #270!

@amass01
Copy link

amass01 commented May 17, 2023

@ajvpot are you working on this, i would love to help with this pretty necessary one!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Issues that anyone could pick up and implement if useful to them
Projects
None yet
Development

No branches or pull requests

3 participants