Skip to content

Fragment name clashes with field name #415

@krish7919

Description

@krish7919

Describe the bug

I am trying to generate the Golang model for a Graphql fragment like so:

fragment loginMethods on Customer {
    loginMethods {
        ... on PassKeyMethod {
            ...
        ... on PasswordMethod {
            ...
        }
        ... on EmailMethod {
            ...
        }
    }
}

After running $ go run github.com/Khan/genqlient@main ./genqlient.yaml, I see that there is a clash in the structs that are generated:

    func (v *LoginMethods) UnmarshalJSON(b []byte) error {
	if string(b) == "null" {
		return nil
	}

	var firstPass struct {
		*LoginMethods
		LoginMethods []json.RawMessage `json:"loginMethods"`. <<<-------- 2 fields in the struct with same name!!
		graphql.NoUnmarshalJSON
	}
	firstPass.LoginMethods = v

My genqlient.yaml is as follows:

# Base template at
# https://github.com/Khan/genqlient/blob/main/docs/genqlient.yaml; refer to the
# template for description of each field.

# Specify where the GrpahQL schema is located.
schema: ./schema.graphql

# Specify the GraphQL operations (queries, mutations, subscriptions) are
# located.
operations:
- ./**/*.graphql

# Specify the filename to which to write the generated code, relative to
# genqlient.yaml. Default: generated.go.
generated: ../../model/android/generated/genqlient.go

# Other config parameters.
use_struct_references: true
optional: pointer_omitempty
bindings:
  DateTime:
    type: time.Time
  Timestamp:
    type: time.Time
casing:
  # Specify the casing algorithm to use for all GraphQL names (fields, types,
  # etc.).
  default: auto_camel_case
  all_enums: raw

To Reproduce
Generate the Golang code for specified model; unfortunately I cannot share schema.graphql, I can spend some time to handcraft an example schema.graphql if required.

Expected behavior
Struct fields should have unique names, as otherwise this fails compilation.

genqlient version
Latest from master, my go.mod has github.com/Khan/genqlient v0.8.2-0.20251119064104-5b0aabc933fa

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions