Khan / Khan/genqlient

Fragment name clashes with field name

Open
#415 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
1.3k
Forks
143
Avg merge
4h 50m
Merged PRs (30d)
1

Description

**Describe the bug**

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

```json
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:

```golang
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:

```yaml
# 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`

Contributor guide

Open the contributing guide

Research direction

Start with the GraphQL fragment and field names in the issue, then run `go run github.com/Khan/genqlient@main ./genqlient.yaml` using the described `genqlient.yaml` and a minimized `schema.graphql`. Inspect the generated `genqlient.go` at the configured output path and the generated `LoginMethods` struct. Done means the generated Go code gives the fragment and field unique struct names and compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, graphql
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.