Skip to content
Open
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 @@ -14,6 +14,7 @@ When releasing a new version:

### Bug fixes:

- fields with `@skip` or `@include` are now treated as nullable: under `optional: pointer` or `optional: generic` they generate as a pointer or `Option` and can represent an absent value, instead of silently decoding an absent field to the zero value (#424). This is technically a breaking change for non-null fields with `@skip`/`@include` under those configs.
-->

## next
Expand Down
12 changes: 11 additions & 1 deletion generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,18 @@ func (g *generator) convertField(

namePrefix = nextPrefix(namePrefix, field, g.Config.GetDefaultCasingAlgorithm())

// A field with @skip or @include may be absent from the response even when
// its schema type is non-null, so treat it as nullable (like `id: ID` rather
// than `id: ID!`) and let the usual optional-handling decide.
fieldType := field.Definition.Type
if field.Directives.ForName("skip") != nil || field.Directives.ForName("include") != nil {
nullableType := *fieldType
nullableType.NonNull = false
fieldType = &nullableType
}

fieldGoType, err := g.convertType(
namePrefix, field.Definition.Type, field.SelectionSet,
namePrefix, fieldType, field.SelectionSet,
fieldOptions, queryOptions)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func TestGenerateWithConfig(t *testing.T) {
"QueryWithSlices.graphql",
"SimpleQueryWithPointerFalseOverride.graphql",
"SimpleQueryNoOverride.graphql",
"SkipInclude.graphql",
}, &Config{
Optional: "pointer",
}},
Expand Down
7 changes: 7 additions & 0 deletions generate/testdata/queries/SkipInclude.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query SkipInclude($skipId: Boolean!, $includeEmails: Boolean!) {
user {
id @skip(if: $skipId)
name
emails @include(if: $includeEmails)
}
}

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": "SkipInclude",
"query": "\nquery SkipInclude ($skipId: Boolean!, $includeEmails: Boolean!) {\n\tuser {\n\t\tid @skip(if: $skipId)\n\t\tname\n\t\temails @include(if: $includeEmails)\n\t}\n}\n",
"sourceLocation": "testdata/queries/SkipInclude.graphql"
}
]
}

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