Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[Proposal] Customized json tag key in generated models #1254

Closed
zdunecki opened this issue Jul 17, 2020 · 6 comments
Closed

[Proposal] Customized json tag key in generated models #1254

zdunecki opened this issue Jul 17, 2020 · 6 comments

Comments

@zdunecki
Copy link

zdunecki commented Jul 17, 2020

What happened?

I would like to get a customized JSON tag key in generated models.

What did you expect?

type Model struct {
  UserID string `json:"user_id"` // or any other format you would like to
}

Proposal

  1. via command line:
go run github.com/99designs/gqlgen generate --json-tag snakecase
  1. via gqlgen.yml with simple model key:
model:
  filename: graph/model/models_gen.go
  package: model
  json_tag: snakecase
  1. via gqlgen.yml with options key:
model:
  filename: graph/model/models_gen.go
  package: model
  options:
    json_tag: snakecase
  1. via gqlgen.yml with custom mutate hooks:
model:
  filename: graph/model/models_gen.go
  package: model
  mutate_hooks:
    - github.com/99designs/gqlgen/plugin/modelgen.SnakeCaseMutateHook
  • solution 1) and 2) looks simple but is limited.
  • solution 3) looks more customized and it extends PackageConfig struct.
  • solution 4) is inspired by https://gqlgen.com/recipes/modelgen-hook/ but instead of using API we're invoking hooks via gqlgen.yml but I'm not quite sure if it is possible to invoke function this way.

Current status

I'm using Modelgen hook to achieve this but I need separate .go file with boilerplate. I think it would be nice to have this feature in a native way.

Similar issue: #824

@stale
Copy link

stale bot commented Oct 16, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 16, 2020
@frederikhors
Copy link
Collaborator

nope

@stale stale bot removed the stale label Oct 18, 2020
@frederikhors
Copy link
Collaborator

@zdunecki can I ask you if you use the code from https://gqlgen.com/recipes/modelgen-hook/ is the generator generating resolvers files?

Thanks.

@zdunecki
Copy link
Author

zdunecki commented Dec 17, 2020

@frederikhors

Yes, I'm using modelgen-hook to achieve this case. If you want I can share the gist with the community.

This proposal ensures to make snakecase json tags as an option. The first way is to extend core implementation.
The second is more advanced and there you can use modelgen-hook via gqlgen.yml, so then you can use shared plugins.

@frederikhors
Copy link
Collaborator

If you want I can share the gist with the community.

Yes please. It can help other people too.

@zdunecki
Copy link
Author

@frederikhors

I've created a gist for that.

@frederikhors frederikhors converted this issue into discussion #1937 Feb 4, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants