Skip to content

refactor(clientgenv2): split NewResponseField by selection kind - #346

Merged
Yamashou merged 1 commit into
mainfrom
refactor/clientgenv2-response-field
Oct 1, 2026
Merged

Yamashou merged 1 commit into
mainfrom
refactor/clientgenv2-response-field

Conversation

@Yamashou

@Yamashou Yamashou commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Background

SourceGenerator.NewResponseField was a 198-line type switch (cognitive complexity 38) that handled *ast.Field, *ast.FragmentSpread, and *ast.InlineFragment in one body. Inside the inline fragment case, the path with a fragment spread and the path without one repeated the same struct registration, InlineFragmentAlwaysPointers handling, and graphql:"... on X" tag construction, so the two could drift independently. expandFragmentFields had no callers.

Changes

Pure refactor; the test suite is untouched.

  • NewResponseField dispatches to newFieldResponseField, newFragmentSpreadResponseField, and newInlineFragmentResponseField.
  • The field path is split further into fieldBaseType (scalar, fragment, or generated struct) and fieldTags (json and graphql tags, including the omitempty and omitzero options).
  • The inline fragment path computes its field list once (own fields, then the fields of the spreads, exactly as before) and then runs a single tail; inlineFragmentType and inlineFragmentTags hold the shared pieces. The special case that reuses a lone fragment spread's type is kept as is.
  • expandFragmentFields is deleted.
  • The duplicate-field merge from fix(clientgenv2): merge duplicate fields between an inline fragment and its spread #358 is ported into a new mergeInlineFragmentFields helper; the inline_fragment_duplicate_fields golden case verifies it.

The order of side effects on r.StructSources and the point at which SortByName runs are preserved, which is what the generated struct field order depends on.

Testing

$ go test -race ./clientgenv2/ ./generator/
ok  	github.com/gqlgo/gqlgenc/clientgenv2
ok  	github.com/gqlgo/gqlgenc/generator
$ golangci-lint run ./...   # v2.13.2
0 issues.

The golden tests in generator pass unchanged, and regenerating the seven local-schema examples under example/ produces no diff. The workflow was also run locally with act and the Build job passed.

NewResponseField was a 198-line type switch that handled fields,
fragment spreads, and inline fragments in one body, and the two inline
fragment paths (with and without a fragment spread) repeated the same
struct registration, pointer wrapping, and tag construction. Extract
newFieldResponseField, fieldBaseType, fieldTags,
newFragmentSpreadResponseField, newInlineFragmentResponseField,
mergeInlineFragmentFields, inlineFragmentType, and inlineFragmentTags,
and let the inline fragment path compute its field list once before a
single shared tail. The duplicate-field merge from #358 lives in
mergeInlineFragmentFields. Delete expandFragmentFields, which had no
callers.

No behavior change: the golden tests in generator, including the
inline_fragment_duplicate_fields case from #358, and a regeneration of
the local-schema examples produce identical output, and the test suite
is untouched.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@Yamashou
Yamashou force-pushed the refactor/clientgenv2-response-field branch from 879e2b3 to 6012290 Compare October 1, 2026 05:57
@Yamashou
Yamashou merged commit 4e79cff into main Oct 1, 2026
1 check passed
@Yamashou
Yamashou deleted the refactor/clientgenv2-response-field branch October 1, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant