Skip to content

Fix isNullish to not consider an empty string as Null #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2017
Merged
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
3 changes: 1 addition & 2 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,8 +1252,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
9 changes: 5 additions & 4 deletions executor_resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package graphql_test

import (
"encoding/json"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"reflect"
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
)

func testSchema(t *testing.T, testField *graphql.Field) graphql.Schema {
Expand Down Expand Up @@ -147,7 +148,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 +224,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 @@ -171,3 +171,51 @@ func TestThreadsContextFromParamsThrough(t *testing.T) {
}

}

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