Skip to content

Commit

Permalink
refactor: improved type system
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 25, 2016
1 parent 1d03e66 commit 4fd3395
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 49 deletions.
8 changes: 4 additions & 4 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import "github.com/neelance/graphql-go/internal/lexer"

type Type interface {
IsType()
Kind() string
}

type List struct {
Expand All @@ -18,9 +18,9 @@ type TypeName struct {
Name string
}

func (*List) IsType() {}
func (*NonNull) IsType() {}
func (*TypeName) IsType() {}
func (*List) Kind() string { return "LIST" }
func (*NonNull) Kind() string { return "NON_NULL" }
func (*TypeName) Kind() string { panic("TypeName needs to be resolved to actual type") }

func ParseType(l *lexer.Lexer) Type {
t := parseNullType(l)
Expand Down
40 changes: 5 additions & 35 deletions internal/exec/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,45 +254,15 @@ type typeResolver struct {
}

func (r *typeResolver) Kind() string {
switch r.typ.(type) {
case *schema.Scalar:
return "SCALAR"
case *schema.Object:
return "OBJECT"
case *schema.Interface:
return "INTERFACE"
case *schema.Union:
return "UNION"
case *schema.Enum:
return "ENUM"
case *schema.InputObject:
return "INPUT_OBJECT"
case *common.List:
return "LIST"
case *common.NonNull:
return "NON_NULL"
default:
panic("unreachable")
}
return r.typ.Kind()
}

func (r *typeResolver) Name() *string {
switch t := r.typ.(type) {
case *schema.Scalar:
return &t.Name
case *schema.Object:
return &t.Name
case *schema.Interface:
return &t.Name
case *schema.Union:
return &t.Name
case *schema.Enum:
return &t.Name
case *schema.InputObject:
return &t.Name
default:
return nil
if named, ok := r.typ.(schema.NamedType); ok {
name := named.TypeName()
return &name
}
return nil
}

func (r *typeResolver) Description() *string {
Expand Down
32 changes: 22 additions & 10 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import (
)

type Schema struct {
EntryPoints map[string]common.Type
Types map[string]common.Type
EntryPoints map[string]NamedType
Types map[string]NamedType

entryPointNames map[string]string
objects []*Object
unions []*Union
}

type NamedType interface {
common.Type
TypeName() string
}

type Scalar struct {
Name string
}
Expand Down Expand Up @@ -57,12 +62,19 @@ type InputObject struct {
InputFieldOrder []string
}

func (*Scalar) IsType() {}
func (*Object) IsType() {}
func (*Interface) IsType() {}
func (*Union) IsType() {}
func (*Enum) IsType() {}
func (*InputObject) IsType() {}
func (*Scalar) Kind() string { return "SCALAR" }
func (*Object) Kind() string { return "OBJECT" }
func (*Interface) Kind() string { return "INTERFACE" }
func (*Union) Kind() string { return "UNION" }
func (*Enum) Kind() string { return "ENUM" }
func (*InputObject) Kind() string { return "INPUT_OBJECT" }

func (t *Scalar) TypeName() string { return t.Name }
func (t *Object) TypeName() string { return t.Name }
func (t *Interface) TypeName() string { return t.Name }
func (t *Union) TypeName() string { return t.Name }
func (t *Enum) TypeName() string { return t.Name }
func (t *InputObject) TypeName() string { return t.Name }

type Field struct {
Name string
Expand Down Expand Up @@ -97,7 +109,7 @@ func Parse(schemaString string) (s *Schema, err *errors.GraphQLError) {
}
}

s.EntryPoints = make(map[string]common.Type)
s.EntryPoints = make(map[string]NamedType)
for key, name := range s.entryPointNames {
t, ok := s.Types[name]
if !ok {
Expand Down Expand Up @@ -216,7 +228,7 @@ func resolveTypeName(s *Schema, t common.Type) (common.Type, *errors.GraphQLErro
func parseSchema(l *lexer.Lexer) *Schema {
s := &Schema{
entryPointNames: make(map[string]string),
Types: map[string]common.Type{
Types: map[string]NamedType{
"Int": &Scalar{Name: "Int"},
"Float": &Scalar{Name: "Float"},
"String": &Scalar{Name: "String"},
Expand Down

0 comments on commit 4fd3395

Please sign in to comment.