Skip to content

Commit

Permalink
Add consideration for pointer false directive when optional: pointer …
Browse files Browse the repository at this point in the history
…configuration option is used (#280)

When support for the `optional` configuration parameter was added in
#198 it was done in a way that, if `optional: pointer` was set, it would
not consider the presence of `# @genqlient(pointer: false)` graphql file
declarations.
  • Loading branch information
spencermurray authored Jul 10, 2023
1 parent 65d934a commit 34447eb
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ When releasing a new version:
- For schemas with enum values that differ only in casing, it's now possible to disable smart-casing in genqlient.yaml; see the [documentation](genqlient.yaml) for `casing` for details.

### Bug fixes:
- The presence of negative pointer directives, i.e., `# @genqlient(pointer: false)` are now respected even in the when `optional: pointer` is set in the configuration file.

## v0.6.0

Expand Down
2 changes: 1 addition & 1 deletion generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (g *generator) convertType(
oe := true
options.Omitempty = &oe
}
} else if options.GetPointer() || (!typ.NonNull && g.Config.Optional == "pointer") {
} else if !options.PointerIsFalse() && (options.GetPointer() || (!typ.NonNull && g.Config.Optional == "pointer")) {
// Whatever we get, wrap it in a pointer. (Because of the way the
// options work, recursing here isn't as connvenient.)
// Note this does []*T or [][]*T, not e.g. *[][]T. See #16.
Expand Down
7 changes: 6 additions & 1 deletion generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ func TestGenerateWithConfig(t *testing.T) {
{"OptionalValue", "", []string{"ListInput.graphql", "QueryWithSlices.graphql"}, &Config{
Optional: "value",
}},
{"OptionalPointer", "", []string{"ListInput.graphql", "QueryWithSlices.graphql"}, &Config{
{"OptionalPointer", "", []string{
"ListInput.graphql",
"QueryWithSlices.graphql",
"SimpleQueryWithPointerFalseOverride.graphql",
"SimpleQueryNoOverride.graphql",
}, &Config{
Optional: "pointer",
}},
{"OptionalGeneric", "", []string{"ListInput.graphql", "QueryWithSlices.graphql"}, &Config{
Expand Down
9 changes: 5 additions & 4 deletions generate/genqlient_directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ func (dir *genqlientDirective) String() string {
return strings.Join(lines, "\n")
}

func (dir *genqlientDirective) GetOmitempty() bool { return dir.Omitempty != nil && *dir.Omitempty }
func (dir *genqlientDirective) GetPointer() bool { return dir.Pointer != nil && *dir.Pointer }
func (dir *genqlientDirective) GetStruct() bool { return dir.Struct != nil && *dir.Struct }
func (dir *genqlientDirective) GetFlatten() bool { return dir.Flatten != nil && *dir.Flatten }
func (dir *genqlientDirective) GetOmitempty() bool { return dir.Omitempty != nil && *dir.Omitempty }
func (dir *genqlientDirective) GetPointer() bool { return dir.Pointer != nil && *dir.Pointer }
func (dir *genqlientDirective) PointerIsFalse() bool { return dir.Pointer != nil && !*dir.Pointer }
func (dir *genqlientDirective) GetStruct() bool { return dir.Struct != nil && *dir.Struct }
func (dir *genqlientDirective) GetFlatten() bool { return dir.Flatten != nil && *dir.Flatten }

func setBool(optionName string, dst **bool, v *ast.Value, pos *ast.Position) error {
if *dst != nil {
Expand Down
6 changes: 6 additions & 0 deletions generate/testdata/queries/SimpleQueryNoOverride.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query SimpleQueryNoOverride {
user {
id
name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query SimpleQueryWithPointerFalseOverride {
user {
id
# @genqlient(pointer: false)
name
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"operations": [
{
"operationName": "SimpleQueryNoOverride",
"query": "\nquery SimpleQueryNoOverride {\n\tuser {\n\t\tid\n\t\tname\n\t}\n}\n",
"sourceLocation": "testdata/queries/SimpleQueryNoOverride.graphql"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"operations": [
{
"operationName": "SimpleQueryWithPointerFalseOverride",
"query": "\nquery SimpleQueryWithPointerFalseOverride {\n\tuser {\n\t\tid\n\t\tname\n\t}\n}\n",
"sourceLocation": "testdata/queries/SimpleQueryWithPointerFalseOverride.graphql"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34447eb

Please sign in to comment.