Skip to content

Commit

Permalink
feat: Embed template for root file generation (#3319)
Browse files Browse the repository at this point in the history
* feat: Embed template for root file generation

* fix: Linting

---------

Co-authored-by: Victoria <[email protected]>
  • Loading branch information
barr-code and Victoria authored Oct 9, 2024
1 parent 83a5251 commit 354ef60
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions codegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"embed"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/vektah/gqlparser/v2/ast"
Expand Down Expand Up @@ -129,24 +127,18 @@ func addBuild(filename string, p *ast.Position, data *Data, builds *map[string]*
}
}

//go:embed root_.gotpl
var rootTemplate string

// Root file contains top-level definitions that should not be duplicated across the generated
// files for each schema file.
func generateRootFile(data *Data) error {
dir := data.Config.Exec.DirName
path := filepath.Join(dir, "root_.generated.go")

_, thisFile, _, _ := runtime.Caller(0)
rootDir := filepath.Dir(thisFile)
templatePath := filepath.Join(rootDir, "root_.gotpl")
templateBytes, err := os.ReadFile(templatePath)
if err != nil {
return err
}
template := string(templateBytes)

return templates.Render(templates.Options{
PackageName: data.Config.Exec.Package,
Template: template,
Template: rootTemplate,
Filename: path,
Data: data,
RegionTags: false,
Expand Down

0 comments on commit 354ef60

Please sign in to comment.