Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vikstrous committed May 1, 2022
1 parent ec5c463 commit 524ec52
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions codegen/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,19 @@ type Data struct {
QueryRoot *Object
MutationRoot *Object
SubscriptionRoot *Object
AugmentedSources []AugmentedSource
}

func (d *Data) HasEmbeddableSources() bool {
hasEmbeddableSources := false
for _, s := range d.AugmentedSources() {
for _, s := range d.AugmentedSources {
if s.Embeddable {
hasEmbeddableSources = true
}
}
return hasEmbeddableSources
}

func (d *Data) AugmentedSources() []AugmentedSource {
sources := []AugmentedSource{}
for _, s := range d.Config.Sources {

wd, err := os.Getwd()
if err != nil {
panic(err)
}
outputDir := d.Config.Exec.Dir()
relative, err := filepath.Rel(outputDir, filepath.Join(wd, s.Name))
if err != nil {
panic("TODO: handle" + err.Error() + " inputs:" + d.Config.Exec.Filename + " and " + filepath.Join(wd, s.Name))
}
embeddable := true
if strings.HasPrefix(relative, "..") {
embeddable = false
}
sources = append(sources, AugmentedSource{
RelativePath: relative,
Embeddable: embeddable,
BuiltIn: s.BuiltIn,
Source: s.Input,
})
}
return sources
}

// AugmentedSource contains extra information about graphql schema files which is not known directly from the Config.Sources data
type AugmentedSource struct {
// path relative to Config.Exec.Filename
Expand Down Expand Up @@ -196,6 +170,30 @@ func BuildData(cfg *config.Config) (*Data, error) {
// otherwise show a generic error message
return nil, fmt.Errorf("invalid types were encountered while traversing the go source code, this probably means the invalid code generated isnt correct. add try adding -v to debug")
}
aSources := []AugmentedSource{}
for _, s := range cfg.Sources {
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get working directory: %w", err)
}
outputDir := cfg.Exec.Dir()
sourcePath := filepath.Join(wd, s.Name)
relative, err := filepath.Rel(outputDir, sourcePath)
if err != nil {
return nil, fmt.Errorf("failed to compute path of %s relative to %s: %w"+sourcePath, outputDir, err)
}
embeddable := true
if strings.HasPrefix(relative, "..") {
embeddable = false
}
aSources = append(aSources, AugmentedSource{
RelativePath: relative,
Embeddable: embeddable,
BuiltIn: s.BuiltIn,
Source: s.Input,
})
}
s.AugmentedSources = aSources

return &s, nil
}
Expand Down

0 comments on commit 524ec52

Please sign in to comment.