back out interfaces/unions, in preparation for new approach

This commit is contained in:
Ben Kraft
2020-07-16 09:53:21 -07:00
parent e9b90d861f
commit af4a765a32
10 changed files with 87 additions and 138 deletions
+5 -23
View File
@@ -29,9 +29,8 @@ type generator struct {
// The list of operations for which to generate code.
Operations []operation
// The types needed for these operations.
typeMap map[string]string
schema *ast.Schema
fragments []*ast.FragmentDefinition
typeMap map[string]string
schema *ast.Schema
}
type operation struct {
@@ -112,15 +111,12 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
// TODO: we may have to actually get the precise query text, in case we
// want to be hashing it or something like that. This is a bit tricky
// because gqlparser's ast doesn't provide node end-position (only
// token end-position). (And with fragments it's not even clear what would
// be right.) Maybe add as a config option, and allow only if the document
// has exactly one query?
// token end-position).
var builder strings.Builder
f := formatter.NewFormatter(&builder)
f.FormatQueryDocument(&ast.QueryDocument{
Operations: ast.OperationList{op},
// TODO(benkraft): Only include relevant fragments.
Fragments: g.fragments,
// TODO: handle fragments
})
args := make([]argument, len(op.VariableDefinitions))
@@ -150,7 +146,7 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
return nil
}
func generate(config *Config) (*generator, error) {
func Generate(config *Config) ([]byte, error) {
schema, err := getSchema(config.Schema)
if err != nil {
return nil, err
@@ -162,26 +158,12 @@ func generate(config *Config) (*generator, error) {
}
g := newGenerator(config, schema)
for _, frag := range document.Fragments {
if err = g.addFragment(frag); err != nil {
return nil, err
}
}
for _, op := range document.Operations {
if err = g.addOperation(op); err != nil {
return nil, err
}
}
return g, nil
}
func Generate(config *Config) ([]byte, error) {
g, err := generate(config)
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, g)
if err != nil {