From cb423e6e1d36044a00a6d8ee62336844294e54fb Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Fri, 21 Oct 2016 13:02:06 +0200 Subject: [PATCH] improved handling of scalar types --- graphql_test.go | 12 ++++++------ internal/exec/introspection.go | 14 +++++++------- internal/schema/schema.go | 25 +++++++++++++------------ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/graphql_test.go b/graphql_test.go index 5b0e8bb09c8..aa67e62168f 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -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" }, @@ -574,6 +578,7 @@ var tests = []struct { { "name": "ReviewInput" }, { "name": "SearchResult" }, { "name": "Starship" }, + { "name": "String" }, { "name": "__Directive" }, { "name": "__DirectiveLocation" }, { "name": "__EnumValue" }, @@ -581,12 +586,7 @@ var tests = []struct { { "name": "__InputValue" }, { "name": "__Schema" }, { "name": "__Type" }, - { "name": "__TypeKind" }, - { "name": "Int" }, - { "name": "Float" }, - { "name": "String" }, - { "name": "Boolean" }, - { "name": "ID" } + { "name": "__TypeKind" } ] } } diff --git a/internal/exec/introspection.go b/internal/exec/introspection.go index 99fc1b8a9d1..12c88e5fb6f 100644 --- a/internal/exec/introspection.go +++ b/internal/exec/introspection.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" "sort" + "strings" "github.com/neelance/graphql-go/errors" "github.com/neelance/graphql-go/internal/query" @@ -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 } diff --git a/internal/schema/schema.go b/internal/schema/schema.go index 3e339636a45..2bbc34deb53 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -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 { @@ -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{} {