Skip to content

Commit

Permalink
Merge pull request #228 from graphql-go/augustoroman/master
Browse files Browse the repository at this point in the history
Fix isNullish to not consider an empty string as Null.
  • Loading branch information
chris-ramon authored Aug 5, 2017
2 parents 1486d45 + 2ddab1a commit a9df066
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 58 deletions.
3 changes: 1 addition & 2 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,8 +1262,7 @@ func (gl *List) Error() error {
//
// Note: the enforcement of non-nullability occurs within the executor.
type NonNull struct {
PrivateName string `json:"name"` // added to conform with introspection for NonNull.Name = nil
OfType Type `json:"ofType"`
OfType Type `json:"ofType"`

err error
}
Expand Down
4 changes: 2 additions & 2 deletions executor_resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With

expected := map[string]interface{}{
"test": map[string]interface{}{
"Str": nil,
"Str": "",
"Int": 0,
},
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestExecutesResolveFunction_UsesProvidedResolveFunction_SourceIsStruct_With

expected := map[string]interface{}{
"test": map[string]interface{}{
"str": nil,
"str": "",
"int": 0,
},
}
Expand Down
2 changes: 1 addition & 1 deletion executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestExecutesArbitraryCode(t *testing.T) {
"b": "Boring",
"c": []interface{}{
"Contrived",
nil,
"",
"Confusing",
},
"deeper": []interface{}{
Expand Down
48 changes: 48 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,51 @@ func TestNewErrorChecksNilNodes(t *testing.T) {
t.Fatalf("expected errors, got: %v", result)
}
}

func TestEmptyStringIsNotNull(t *testing.T) {
checkForEmptyString := func(p graphql.ResolveParams) (interface{}, error) {
arg := p.Args["arg"]
if arg == nil || arg.(string) != "" {
t.Errorf("Expected empty string for input arg, got %#v", arg)
}
return "yay", nil
}
returnEmptyString := func(p graphql.ResolveParams) (interface{}, error) {
return "", nil
}

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"checkEmptyArg": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"arg": &graphql.ArgumentConfig{Type: graphql.String},
},
Resolve: checkForEmptyString,
},
"checkEmptyResult": &graphql.Field{
Type: graphql.String,
Resolve: returnEmptyString,
},
},
}),
})
if err != nil {
t.Fatalf("wrong result, unexpected errors: %v", err.Error())
}
query := `{ checkEmptyArg(arg:"") checkEmptyResult }`

result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if len(result.Errors) > 0 {
t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
}
expected := map[string]interface{}{"checkEmptyArg": "yay", "checkEmptyResult": ""}
if !reflect.DeepEqual(result.Data, expected) {
t.Errorf("wrong result, query: %v, graphql result diff: %v", query, testutil.Diff(expected, result))
}
}
Loading

0 comments on commit a9df066

Please sign in to comment.