Skip to content

Commit

Permalink
Allow null element in list (#271)
Browse files Browse the repository at this point in the history
bubbleUpNullValuesInPlaceRec was incorrectly marking a nil as an error
and attempting to bubble up errors to the next field in places where a
null object was acceptable.
  • Loading branch information
benzolium authored Jul 17, 2024
1 parent f24f90e commit ae27879
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions execution_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ func bubbleUpNullValuesInPlaceRec(schema *ast.Schema, currentType *ast.Type, sel
}
errs = append(errs, lowerErrs...)
}
case nil:
if currentType.Elem.NonNull {
return nil, false, fmt.Errorf("bubbleUpNullValuesInPlaceRec: unxpected result type '%T'", result)
}
return
default:
return nil, false, fmt.Errorf("bubbleUpNullValuesInPlaceRec: unxpected result type '%T'", result)
}
Expand Down
74 changes: 74 additions & 0 deletions execution_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,80 @@ func TestBubbleUpNullValuesInPlace(t *testing.T) {
}}), errs)
require.Equal(t, jsonToInterfaceMap(`{ "critters": [ { "id": "GIZMO1", "color": "RED", "_bramble__typename": "Gizmo" }, { "id": "GREMLIN1", "name": "Spikey", "_bramble__typename": "Gremlin" }, null ] }`), result)
})

t.Run("works with nullable null in array", func(t *testing.T) {
ddl := `
type Gizmo {
id: ID!
color: String!
}
type Query {
gizmos: [Gizmo]!
}`

result := jsonToInterfaceMap(`
{
"gizmos": [
{ "id": "GIZMO1", "color": "RED" },
null,
{ "id": "GIZMO2", "color": "GREEN" }
]
}
`)

schema := gqlparser.MustLoadSchema(&ast.Source{Name: "fixture", Input: ddl})

query := `
{
gizmos {
id
color
}
}`

document := gqlparser.MustLoadQuery(schema, query)
errs, err := bubbleUpNullValuesInPlace(schema, document.Operations[0].SelectionSet, result)
require.NoError(t, err)
require.Equal(t, []*gqlerror.Error(nil), errs)
require.Equal(t, jsonToInterfaceMap(`{ "gizmos": [ { "id": "GIZMO1", "color": "RED" }, null, { "id": "GIZMO2", "color": "GREEN" } ] }`), result)
})
t.Run("works with not nullable null in array", func(t *testing.T) {
ddl := `
type Gizmo {
id: ID!
color: String!
}
type Query {
gizmos: [Gizmo!]!
}`

result := jsonToInterfaceMap(`
{
"gizmos": [
{ "id": "GIZMO1", "color": "RED" },
null,
{ "id": "GIZMO2", "color": "GREEN" }
]
}
`)

schema := gqlparser.MustLoadSchema(&ast.Source{Name: "fixture", Input: ddl})

query := `
{
gizmos {
id
color
}
}`

document := gqlparser.MustLoadQuery(schema, query)
errs, err := bubbleUpNullValuesInPlace(schema, document.Operations[0].SelectionSet, result)
require.Error(t, err)
require.Equal(t, []*gqlerror.Error(nil), errs)
})
}

func TestFormatResponseBody(t *testing.T) {
Expand Down

0 comments on commit ae27879

Please sign in to comment.