Skip to content

Commit a9df066

Browse files
authored
Merge pull request #228 from graphql-go/augustoroman/master
Fix isNullish to not consider an empty string as Null.
2 parents 1486d45 + 2ddab1a commit a9df066

File tree

6 files changed

+102
-58
lines changed

6 files changed

+102
-58
lines changed

definition.go

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

12681267
err error
12691268
}

executor_resolve_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With
147147

148148
expected := map[string]interface{}{
149149
"test": map[string]interface{}{
150-
"Str": nil,
150+
"Str": "",
151151
"Int": 0,
152152
},
153153
}
@@ -223,7 +223,7 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With
223223

224224
expected := map[string]interface{}{
225225
"test": map[string]interface{}{
226-
"str": nil,
226+
"str": "",
227227
"int": 0,
228228
},
229229
}

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
@@ -198,3 +198,51 @@ func TestNewErrorChecksNilNodes(t *testing.T) {
198198
t.Fatalf("expected errors, got: %v", result)
199199
}
200200
}
201+
202+
func TestEmptyStringIsNotNull(t *testing.T) {
203+
checkForEmptyString := func(p graphql.ResolveParams) (interface{}, error) {
204+
arg := p.Args["arg"]
205+
if arg == nil || arg.(string) != "" {
206+
t.Errorf("Expected empty string for input arg, got %#v", arg)
207+
}
208+
return "yay", nil
209+
}
210+
returnEmptyString := func(p graphql.ResolveParams) (interface{}, error) {
211+
return "", nil
212+
}
213+
214+
schema, err := graphql.NewSchema(graphql.SchemaConfig{
215+
Query: graphql.NewObject(graphql.ObjectConfig{
216+
Name: "Query",
217+
Fields: graphql.Fields{
218+
"checkEmptyArg": &graphql.Field{
219+
Type: graphql.String,
220+
Args: graphql.FieldConfigArgument{
221+
"arg": &graphql.ArgumentConfig{Type: graphql.String},
222+
},
223+
Resolve: checkForEmptyString,
224+
},
225+
"checkEmptyResult": &graphql.Field{
226+
Type: graphql.String,
227+
Resolve: returnEmptyString,
228+
},
229+
},
230+
}),
231+
})
232+
if err != nil {
233+
t.Fatalf("wrong result, unexpected errors: %v", err.Error())
234+
}
235+
query := `{ checkEmptyArg(arg:"") checkEmptyResult }`
236+
237+
result := graphql.Do(graphql.Params{
238+
Schema: schema,
239+
RequestString: query,
240+
})
241+
if len(result.Errors) > 0 {
242+
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
243+
}
244+
expected := map[string]interface{}{"checkEmptyArg": "yay", "checkEmptyResult": ""}
245+
if !reflect.DeepEqual(result.Data, expected) {
246+
t.Errorf("wrong result, query: %v, graphql result diff: %v", query, testutil.Diff(expected, result))
247+
}
248+
}

0 commit comments

Comments
 (0)