Skip to content

Commit

Permalink
Added tests for nil pointer handling of Union, Interface and List
Browse files Browse the repository at this point in the history
  • Loading branch information
sfriedel committed Jul 27, 2018
1 parent 21dfa5d commit 6b054a4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,32 @@ func TestLists_ArrayOfNullableObjects_ContainsValues(t *testing.T) {
}
checkList(t, ttype, data, expected)
}

func TestLists_ValueMayBeNilPointer(t *testing.T) {
var listTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"list": &graphql.Field{
Type: graphql.NewList(graphql.Int),
Resolve: func(_ graphql.ResolveParams) (interface{}, error) {
return []int(nil), nil
},
},
},
}),
})
query := "{ list }"
expected := &graphql.Result{
Data: map[string]interface{}{
"list": []interface{}{},
},
}
result := g(t, graphql.Params{
Schema: listTestSchema,
RequestString: query,
})
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
59 changes: 59 additions & 0 deletions union_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,62 @@ func TestUnionIntersectionTypes_GetsExecutionInfoInResolver(t *testing.T) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(schema2, encounteredSchema))
}
}

func TestUnionIntersectionTypes_ValueMayBeNilPointer(t *testing.T) {
var unionInterfaceTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"query": &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "query",
Fields: graphql.Fields{
"pet": &graphql.Field{
Type: petType,
},
"named": &graphql.Field{
Type: namedType,
},
},
}),
Resolve: func(_ graphql.ResolveParams) (interface{}, error) {
return struct {
Pet *testCat2 `graphql:"pet"`
Named *testCat2 `graphql:"named"`
}{nil, nil}, nil
},
},
},
}),
})
query := `{
query {
pet {
__typename
}
named {
__typename
name
}
}
}`
expected := &graphql.Result{
Data: map[string]interface{}{
"query": map[string]interface{}{
"pet": map[string]interface{}{
"__typename": "Cat",
},
"named": map[string]interface{}{
"__typename": "Cat",
"name": nil,
},
}},
}
result := g(t, graphql.Params{
Schema: unionInterfaceTestSchema,
RequestString: query,
})
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}

0 comments on commit 6b054a4

Please sign in to comment.