Skip to content

Commit

Permalink
optimize ConcatenateTypeDefs
Browse files Browse the repository at this point in the history
This was taking a ludicrous amount of time. Simply appending seems to
work fine, even with duplicate type definitions.
  • Loading branch information
vito committed Apr 18, 2023
1 parent e68b441 commit f5e8853
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
39 changes: 10 additions & 29 deletions typedefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
10 changes: 5 additions & 5 deletions typedefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ func TestConcatenateTypeDefs(t *testing.T) {
name: String!
description: String
}
extend type Query {
foo: Foo
}`,
`
interface Named {
name: String!
}
type Bar implements Named {
name: String!
description: String
}
extend type Query {
bar: Bar
}`,
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestObjectIsTypeOf(t *testing.T) {
description: String
}
union Foo = A | B
extend type Query {
foo: Foo
}`,
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestUnionResolveType(t *testing.T) {
description: String
}
union Foo = A | B
extend type Query {
foo: Foo
}`,
Expand Down

0 comments on commit f5e8853

Please sign in to comment.