Skip to content

Commit 778e4d4

Browse files
Add global flatten option to genqlient.yaml (fixes #404)
Applies @genqlient(flatten: true) to every operation and named fragment, flattening fragment-spreads project-wide without per-query directives. It is only applied where flattening is valid, so it is safe to enable globally.
1 parent 6bbde36 commit 778e4d4

12 files changed

Lines changed: 924 additions & 2 deletions

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Note that genqlient now requires Go 1.23 or higher, and is tested through Go 1.2
2828

2929
### New features:
3030

31+
- Added a global `flatten` option to `genqlient.yaml` that applies `@genqlient(flatten: true)` to every operation and named fragment, so flattenable fragment-spreads are flattened project-wide without per-query directives (fixes #404). It is only applied where flattening is valid, so it is safe to enable globally.
3132
- Added `--version` flag to print version information including commit hash and build date
3233

3334
### Bug fixes:

docs/genqlient.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,21 @@ client_getter: "github.com/you/yourpkg.GetClient"
9494
# Defaults to false.
9595
use_struct_references: boolean
9696

97-
# If set, generated code will have a third return parameter of type
97+
# If set, all fragment-spreads will default to having the
98+
# "flatten: true" flag, as if `# @genqlient(flatten: true)` were applied
99+
# to every operation and named fragment. The flag is only applied where
100+
# it is valid (i.e. to fields whose selection is a single fragment-spread
101+
# of a compatible type); other fields are unaffected, so it is safe to
102+
# enable project-wide. Per-node `# @genqlient(flatten: ...)` directives
103+
# still take precedence.
104+
#
105+
# See the flatten documentation in docs/genqlient_directive.graphql for
106+
# details on what flattening does.
107+
#
108+
# Defaults to false.
109+
flatten: boolean
110+
111+
# If set, generated code will have a third return parameter of type
98112
# map[string]interface{}. This will contain the optional values
99113
# of the Extensions field send from Servers.
100114
# ref.: https://spec.graphql.org/October2021/#sec-Response-Format

generate/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Config struct {
3636
OptionalGenericType string `yaml:"optional_generic_type"`
3737
StructReferences bool `yaml:"use_struct_references"`
3838
Extensions bool `yaml:"use_extensions"`
39+
Flatten bool `yaml:"flatten"`
3940

4041
// The directory of the config-file (relative to which all the other paths
4142
// are resolved). Set by ValidateAndFillDefaults.

generate/generate_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ func TestGenerateWithConfig(t *testing.T) {
274274
},
275275
},
276276
},
277+
{
278+
"Flatten", "", []string{"FlattenConfig.graphql"}, &Config{
279+
Flatten: true,
280+
},
281+
},
277282
}
278283

279284
for _, test := range tests {

generate/genqlient_directive.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,15 @@ func (g *generator) parsePrecedingComment(
505505
}
506506
}
507507

508-
if queryOptions != nil {
508+
if queryOptions == nil {
509+
// We are parsing the directive on the entire operation or fragment;
510+
// apply any project-wide defaults from genqlient.yaml. (Per-node
511+
// options always win, since fillDefault is a no-op if already set.)
512+
if g.Config != nil && g.Config.Flatten {
513+
t := true
514+
fillDefaultBool(&directive.Flatten, &t)
515+
}
516+
} else {
509517
// If we are part of an operation/fragment, merge its options in.
510518
directive.mergeOperationDirective(node, parentIfInputField, queryOptions)
511519

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fragment VideoFields on Video {
2+
id name url duration thumbnail { id }
3+
}
4+
5+
# This query has no @genqlient(flatten: ...) directives; flattening is
6+
# expected to be applied automatically via the global `flatten: true`
7+
# config option. The fragment-spread on `randomVideo` (which returns a
8+
# Video) is flattenable and should be flattened; `randomItem` mixes a
9+
# fragment-spread with other fields and so is left alone.
10+
query FlattenConfig {
11+
randomVideo {
12+
...VideoFields
13+
}
14+
randomItem {
15+
id name
16+
...VideoFields
17+
}
18+
}

0 commit comments

Comments
 (0)