Skip to content

Commit 83d0746

Browse files
author
Michel Aquino
authored
Merge pull request #1 from michelaquino/master
Fix isNullish to not consider an empty string as Null
2 parents cf573f1 + 3364b33 commit 83d0746

File tree

6 files changed

+107
-61
lines changed

6 files changed

+107
-61
lines changed

definition.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,8 +1252,7 @@ func (gl *List) Error() error {
12521252
//
12531253
// Note: the enforcement of non-nullability occurs within the executor.
12541254
type NonNull struct {
1255-
PrivateName string `json:"name"` // added to conform with introspection for NonNull.Name = nil
1256-
OfType Type `json:"ofType"`
1255+
OfType Type `json:"ofType"`
12571256

12581257
err error
12591258
}

executor_resolve_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package graphql_test
22

33
import (
44
"encoding/json"
5-
"github.com/graphql-go/graphql"
6-
"github.com/graphql-go/graphql/testutil"
75
"reflect"
86
"testing"
7+
8+
"github.com/graphql-go/graphql"
9+
"github.com/graphql-go/graphql/testutil"
910
)
1011

1112
func testSchema(t *testing.T, testField *graphql.Field) graphql.Schema {
@@ -147,7 +148,7 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With
147148

148149
expected := map[string]interface{}{
149150
"test": map[string]interface{}{
150-
"Str": nil,
151+
"Str": "",
151152
"Int": 0,
152153
},
153154
}
@@ -223,7 +224,7 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With
223224

224225
expected := map[string]interface{}{
225226
"test": map[string]interface{}{
226-
"str": nil,
227+
"str": "",
227228
"int": 0,
228229
},
229230
}

executor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestExecutesArbitraryCode(t *testing.T) {
8585
"b": "Boring",
8686
"c": []interface{}{
8787
"Contrived",
88-
nil,
88+
"",
8989
"Confusing",
9090
},
9191
"deeper": []interface{}{

graphql_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,51 @@ func TestThreadsContextFromParamsThrough(t *testing.T) {
171171
}
172172

173173
}
174+
175+
func TestEmptyStringIsNotNull(t *testing.T) {
176+
checkForEmptyString := func(p graphql.ResolveParams) (interface{}, error) {
177+
arg := p.Args["arg"]
178+
if arg == nil || arg.(string) != "" {
179+
t.Errorf("Expected empty string for input arg, got %#v", arg)
180+
}
181+
return "yay", nil
182+
}
183+
returnEmptyString := func(p graphql.ResolveParams) (interface{}, error) {
184+
return "", nil
185+
}
186+
187+
schema, err := graphql.NewSchema(graphql.SchemaConfig{
188+
Query: graphql.NewObject(graphql.ObjectConfig{
189+
Name: "Query",
190+
Fields: graphql.Fields{
191+
"checkEmptyArg": &graphql.Field{
192+
Type: graphql.String,
193+
Args: graphql.FieldConfigArgument{
194+
"arg": &graphql.ArgumentConfig{Type: graphql.String},
195+
},
196+
Resolve: checkForEmptyString,
197+
},
198+
"checkEmptyResult": &graphql.Field{
199+
Type: graphql.String,
200+
Resolve: returnEmptyString,
201+
},
202+
},
203+
}),
204+
})
205+
if err != nil {
206+
t.Fatalf("wrong result, unexpected errors: %v", err.Error())
207+
}
208+
query := `{ checkEmptyArg(arg:"") checkEmptyResult }`
209+
210+
result := graphql.Do(graphql.Params{
211+
Schema: schema,
212+
RequestString: query,
213+
})
214+
if len(result.Errors) > 0 {
215+
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
216+
}
217+
expected := map[string]interface{}{"checkEmptyArg": "yay", "checkEmptyResult": ""}
218+
if !reflect.DeepEqual(result.Data, expected) {
219+
t.Errorf("wrong result, query: %v, graphql result diff: %v", query, testutil.Diff(expected, result))
220+
}
221+
}

0 commit comments

Comments
 (0)