Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add consideration for pointer false directive when optional: pointer configuration option is used #280

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.