diff --git a/typedefs.go b/typedefs.go index f8f47e0..df8079d 100644 --- a/typedefs.go +++ b/typedefs.go @@ -2,35 +2,31 @@ package tools import ( "fmt" - "strings" "github.com/dagger/graphql/language/ast" "github.com/dagger/graphql/language/parser" - "github.com/dagger/graphql/language/printer" "github.com/dagger/graphql/language/source" ) // ConcatenateTypeDefs combines one ore more typeDefs into an ast Document func (c *ExecutableSchema) ConcatenateTypeDefs() (*ast.Document, error) { - switch c.TypeDefs.(type) { + switch defs := c.TypeDefs.(type) { case string: - return c.concatenateTypeDefs([]string{c.TypeDefs.(string)}) + return c.concatenateTypeDefs([]string{defs}) case []string: - return c.concatenateTypeDefs(c.TypeDefs.([]string)) + return c.concatenateTypeDefs(defs) case func() []string: - return c.concatenateTypeDefs(c.TypeDefs.(func() []string)()) + return c.concatenateTypeDefs(defs()) } return nil, fmt.Errorf("unsupported TypeDefs value. Must be one of string, []string, or func() []string") } -// performs the actual concatenation of the types by parsing each -// typeDefs string and converting each definition into a string -// then creating a unique list of all definitions and finally -// printing them as a single definition and returning the parsed document +// appends all type definitions together into one document func (c *ExecutableSchema) concatenateTypeDefs(typeDefs []string) (*ast.Document, error) { - resolvedTypes := map[string]interface{}{} + doc := ast.NewDocument(nil) + for _, defs := range typeDefs { - doc, err := parser.Parse(parser.ParseParams{ + sub, err := parser.Parse(parser.ParseParams{ Source: &source.Source{ Body: []byte(defs), Name: "GraphQL", @@ -40,23 +36,8 @@ func (c *ExecutableSchema) concatenateTypeDefs(typeDefs []string) (*ast.Document return nil, err } - for _, typeDef := range doc.Definitions { - if def := printer.Print(typeDef); def != nil { - stringDef := strings.TrimSpace(def.(string)) - resolvedTypes[stringDef] = nil - } - } - } - - typeArray := []string{} - for def := range resolvedTypes { - typeArray = append(typeArray, def) + doc.Definitions = append(doc.Definitions, sub.Definitions...) } - return parser.Parse(parser.ParseParams{ - Source: &source.Source{ - Body: []byte(strings.Join(typeArray, "\n")), - Name: "GraphQL", - }, - }) + return doc, nil } diff --git a/typedefs_test.go b/typedefs_test.go index ee0ab2d..b8565fe 100644 --- a/typedefs_test.go +++ b/typedefs_test.go @@ -16,7 +16,7 @@ func TestConcatenateTypeDefs(t *testing.T) { name: String! description: String } - + extend type Query { foo: Foo }`, @@ -24,12 +24,12 @@ func TestConcatenateTypeDefs(t *testing.T) { interface Named { name: String! } - + type Bar implements Named { name: String! description: String } - + extend type Query { bar: Bar }`, @@ -74,7 +74,7 @@ func TestObjectIsTypeOf(t *testing.T) { description: String } union Foo = A | B - + extend type Query { foo: Foo }`, @@ -133,7 +133,7 @@ func TestUnionResolveType(t *testing.T) { description: String } union Foo = A | B - + extend type Query { foo: Foo }`,