Skip to content

Commit

Permalink
type check for scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 22, 2016
1 parent 17034fe commit 2b6460a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions example/starwars/starwars.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func resolveCharacters(ids []string) *[]characterResolver {
type reviewResolver struct {
}

func (r *reviewResolver) Stars() int {
func (r *reviewResolver) Stars() int32 {
panic("TODO")
}

Expand All @@ -520,7 +520,7 @@ func (r *reviewResolver) Commentary() *string {
type friendsConnectionResolver struct {
}

func (r *friendsConnectionResolver) TotalCount() int {
func (r *friendsConnectionResolver) TotalCount() int32 {
panic("TODO")
}

Expand Down
15 changes: 15 additions & 0 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func makeExec(target *iExec, s *schema.Schema, t schema.Type, resolverType refle
return nil
}

var scalarTypes = map[string]reflect.Type{
"Int": reflect.TypeOf(int32(0)),
"Float": reflect.TypeOf(float64(0)),
"String": reflect.TypeOf(""),
"Boolean": reflect.TypeOf(true),
"ID": reflect.TypeOf(""),
}

func makeExec2(s *schema.Schema, t schema.Type, resolverType reflect.Type, typeRefMap map[typeRefMapKey]*typeRef) (iExec, error) {
nonNull := false
if nn, ok := t.(*schema.NonNull); ok {
Expand All @@ -90,6 +98,13 @@ func makeExec2(s *schema.Schema, t schema.Type, resolverType reflect.Type, typeR

switch t := t.(type) {
case *schema.Scalar:
if !nonNull {
resolverType = resolverType.Elem()
}
scalarType := scalarTypes[t.Name]
if resolverType != scalarType {
return nil, fmt.Errorf("expected %s, got %s", scalarType, resolverType)
}
return &scalarExec{}, nil

case *schema.Object:
Expand Down

0 comments on commit 2b6460a

Please sign in to comment.