Skip to content

Commit

Permalink
Test fixes to use correct ResolveTypeFn and add types to Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
object88 committed Jul 7, 2016
1 parent 9054c3f commit 5c3d4ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
5 changes: 3 additions & 2 deletions examples/starwars/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package starwars

import (
"errors"

"github.com/graphql-go/graphql"
"github.com/graphql-go/relay"
"golang.org/x/net/context"
Expand Down Expand Up @@ -118,9 +119,9 @@ func init() {
return nil, errors.New("Unknown node type")
}
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
TypeResolve: func(p graphql.ResolveTypeParams) *graphql.Object {
// based on the type of the value, return GraphQLObjectType
switch value.(type) {
switch p.Value.(type) {
case *Faction:
return factionType
default:
Expand Down
12 changes: 7 additions & 5 deletions node_global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package relay_test
import (
"errors"
"fmt"
"reflect"
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/relay"
"golang.org/x/net/context"
"reflect"
"testing"
)

type photo2 struct {
Expand Down Expand Up @@ -46,14 +47,14 @@ var globalIDTestDef = relay.NewNodeDefinitions(relay.NodeDefinitionsConfig{
return nil, errors.New("Unknown node type")
}
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
switch value.(type) {
TypeResolve: func(p graphql.ResolveTypeParams) *graphql.Object {
switch p.Value.(type) {
case *user:
return globalIDTestUserType
case *photo2:
return globalIDTestPhotoType
default:
panic(fmt.Sprintf("Unknown object type `%v`", value))
panic(fmt.Sprintf("Unknown object type `%v`", p.Value))
}
},
})
Expand Down Expand Up @@ -109,6 +110,7 @@ func init() {

globalIDTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: globalIDTestQueryType,
Types: []graphql.Type{globalIDTestUserType, globalIDTestPhotoType},
})
}

Expand Down
14 changes: 8 additions & 6 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package relay_test
import (
"errors"
"fmt"
"reflect"
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/location"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/relay"
"golang.org/x/net/context"
"reflect"
"testing"
)

type user struct {
Expand Down Expand Up @@ -46,14 +47,14 @@ var nodeTestDef = relay.NewNodeDefinitions(relay.NodeDefinitionsConfig{
}
return nil, errors.New("Unknown node")
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
switch value.(type) {
TypeResolve: func(p graphql.ResolveTypeParams) *graphql.Object {
switch p.Value.(type) {
case *user:
return nodeTestUserType
case *photo:
return nodeTestPhotoType
default:
panic(fmt.Sprintf("Unknown object type `%v`", value))
panic(fmt.Sprintf("Unknown object type `%v`", p.Value))
}
},
})
Expand All @@ -64,7 +65,7 @@ var nodeTestQueryType = graphql.NewObject(graphql.ObjectConfig{
},
})

// becareful not to define schema here, since nodeTestUserType and nodeTestPhotoType wouldn't be defined till init()
// be careful not to define schema here, since nodeTestUserType and nodeTestPhotoType wouldn't be defined till init()
var nodeTestSchema graphql.Schema

func init() {
Expand Down Expand Up @@ -95,6 +96,7 @@ func init() {

nodeTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: nodeTestQueryType,
Types: []graphql.Type{nodeTestUserType, nodeTestPhotoType},
})
}
func TestNodeInterfaceAndFields_AllowsRefetching_GetsTheCorrectIDForUsers(t *testing.T) {
Expand Down

0 comments on commit 5c3d4ee

Please sign in to comment.