Skip to content

Commit

Permalink
improved handling of scalar types
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Oct 21, 2016
1 parent 5b07780 commit cb423e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
12 changes: 6 additions & 6 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,16 @@ var tests = []struct {
{
"__schema": {
"types": [
{ "name": "Boolean" },
{ "name": "Character" },
{ "name": "Droid" },
{ "name": "Episode" },
{ "name": "Float" },
{ "name": "FriendsConnection" },
{ "name": "FriendsEdge" },
{ "name": "Human" },
{ "name": "ID" },
{ "name": "Int" },
{ "name": "LengthUnit" },
{ "name": "Mutation" },
{ "name": "PageInfo" },
Expand All @@ -574,19 +578,15 @@ var tests = []struct {
{ "name": "ReviewInput" },
{ "name": "SearchResult" },
{ "name": "Starship" },
{ "name": "String" },
{ "name": "__Directive" },
{ "name": "__DirectiveLocation" },
{ "name": "__EnumValue" },
{ "name": "__Field" },
{ "name": "__InputValue" },
{ "name": "__Schema" },
{ "name": "__Type" },
{ "name": "__TypeKind" },
{ "name": "Int" },
{ "name": "Float" },
{ "name": "String" },
{ "name": "Boolean" },
{ "name": "ID" }
{ "name": "__TypeKind" }
]
}
}
Expand Down
14 changes: 7 additions & 7 deletions internal/exec/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"sort"
"strings"

"github.com/neelance/graphql-go/errors"
"github.com/neelance/graphql-go/internal/query"
Expand Down Expand Up @@ -141,21 +142,20 @@ type schemaResolver struct {

func (r *schemaResolver) Types() []*typeResolver {
var l []*typeResolver
addTypes := func(s *schema.Schema) {
addTypes := func(s *schema.Schema, metaOnly bool) {
var names []string
for name := range s.AllTypes {
names = append(names, name)
if !metaOnly || strings.HasPrefix(name, "__") {
names = append(names, name)
}
}
sort.Strings(names)
for _, name := range names {
l = append(l, &typeResolver{s.AllTypes[name]})
}
}
addTypes(r.schema)
addTypes(metaSchema)
for _, name := range []string{"Int", "Float", "String", "Boolean", "ID"} {
l = append(l, &typeResolver{&schema.Scalar{Name: name}})
}
addTypes(r.schema, false)
addTypes(metaSchema, true)
return l
}

Expand Down
25 changes: 13 additions & 12 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ func Parse(schemaString string) (s *Schema, err *errors.GraphQLError) {
func parseSchema(l *lexer.Lexer, c *context) *Schema {
s := &Schema{
EntryPoints: make(map[string]string),
AllTypes: make(map[string]Type),
Objects: make(map[string]*Object),
Interfaces: make(map[string]*Interface),
AllTypes: map[string]Type{
"Int": &Scalar{Name: "Int"},
"Float": &Scalar{Name: "Float"},
"String": &Scalar{Name: "String"},
"Boolean": &Scalar{Name: "Boolean"},
"ID": &Scalar{Name: "ID"},
},
Objects: make(map[string]*Object),
Interfaces: make(map[string]*Interface),
}

for l.Peek() != scanner.EOF {
Expand Down Expand Up @@ -286,15 +292,10 @@ func parseType(target *Type, l *lexer.Lexer, c *context) {

name := l.ConsumeIdent()
parseNonNil()
switch name {
case "Int", "Float", "String", "Boolean", "ID":
*target = &Scalar{Name: name}
default:
c.typeRefs = append(c.typeRefs, &typeRef{
name: name,
target: target,
})
}
c.typeRefs = append(c.typeRefs, &typeRef{
name: name,
target: target,
})
}

func parseValue(l *lexer.Lexer) interface{} {
Expand Down

0 comments on commit cb423e6

Please sign in to comment.