99designs / gqlgen

go generate based graphql server library
https://gqlgen.com
MIT License
9.94k stars 1.16k forks source link

Non-convenient go enum value #2490

Open bynov opened 1 year ago

bynov commented 1 year ago

What happened?

I tried to generate enum, but one value after generation seems non-convenient :)

Interesting that it only occurred with SLACK value. I've used bunch of other names and it's fine. I've did several tests and seems like problem in SLA inside of SLACK. It processes it like abbreviation :)

What did you expect?

I'm expecting PlatformIDSlack instead of PlatformIDSLACk.

Minimal graphql.schema and models to reproduce

Schema:

enum PlatformID {
  ASANA
  SLACK
}

type Query {
  myUser(email: String!): User!
}

type User {
  id: String!
  email: String!
}

Generated models:

type PlatformID string

const (
    PlatformIDAsana PlatformID = "ASANA"
    PlatformIDSLACk PlatformID = "SLACK"
)

var AllPlatformID = []PlatformID{
    PlatformIDAsana,
    PlatformIDSLACk,
}

gclgen.yml:

# Where are all the schema files located? globs are supported eg  src/**/*.graphqls
schema:
  - graph/*.graphqls

# Where should the generated server code go?
exec:
  filename: graph/generated.go
  package: graph

# Uncomment to enable federation
# federation:
#   filename: graph/federation.go
#   package: graph

# Where should any generated models go?
model:
  filename: graph/model/models_gen.go
  package: model

# Where should the resolver implementations go?
resolver:
  layout: follow-schema
  dir: graph
  package: graph

# Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models
# struct_tag: json

# Optional: turn on to use []Thing instead of []*Thing
# omit_slice_element_pointers: false

# Optional: turn off to make struct-type struct fields not use pointers
# e.g. type Thing struct { FieldA OtherThing } instead of { FieldA *OtherThing }
# struct_fields_always_pointers: true

# Optional: turn off to make resolvers return values instead of pointers for structs
# resolvers_always_return_pointers: true

# Optional: set to speed up generation time by not performing a final validation pass.
# skip_validation: true

# gqlgen will search for any type names in the schema in these go packages
# if they match it will use them, otherwise it will generate them.
autobind:
#  - "github.com/bynov/test-graphql/graph/model"

# This section declares type mapping between the GraphQL and go type systems
#
# The first line in each type will be used as defaults for resolver arguments and
# modelgen, the others will be allowed when binding to fields. Configure them to
# your liking
models:
  ID:
    model:
      - github.com/99designs/gqlgen/graphql.ID
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int64
      - github.com/99designs/gqlgen/graphql.Int32
  Int:
    model:
      - github.com/99designs/gqlgen/graphql.Int
      - github.com/99designs/gqlgen/graphql.Int64
      - github.com/99designs/gqlgen/graphql.Int32

versions

jskrzypek commented 1 year ago

I think is from a feature provided by a newer version of gqlgen. This bit is in the most recent version of the configuration docs page:

# Optional: set to modify the initialisms regarded for Go names
# go_initialisms:
#   replace_defaults: false # if true, the default initialisms will get dropped in favor of the new ones instead of being added
#   initialisms: # List of initialisms to for Go names
#     - 'CC'
#     - 'BCC'

"SLA" is one of the initialisms it tries to leave capitalized (see here).

You could replace the defaults with your own set to avoid this.

mwajeeh commented 1 year ago

@jskrzypek

enum LeadPracticeType {
   SPEECH_LANGUAGE_PATHOLOGY
 }

Generates:

type LeadPracticeType string

const (
    LeadPracticeTypeSpeechLANGuagePathology           LeadPracticeType = "SPEECH_LANGUAGE_PATHOLOGY"
)

If we specify following config:

go_initialisms:
  replace_defaults: false
  initialisms: # List of initialisms to for Go names
    - 'LAN'

Is there a way to avoid to fix this?

jskrzypek commented 1 year ago

@mwajeeh I'm sorry, I'm not an owner/maintainer of this library, so I don't know the answer. I imagine you can find out if you dig through the source code for gqlgen, e.g. do a code search for "initialisms" because something has to read the yaml file into go. Or just look at the repo details and tag one of the maintainers.