-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin_model.go
52 lines (43 loc) · 1.2 KB
/
plugin_model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gbgen
import (
"fmt"
"syscall"
"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin"
"github.com/99designs/gqlgen/plugin/modelgen"
)
func NewModelPlugin() *ModelPlugin {
return &ModelPlugin{}
}
type ModelPlugin struct {
}
func (m *ModelPlugin) GenerateCode(cfg *config.Config) (*codegen.Data, error) {
_ = syscall.Unlink(cfg.Exec.Filename)
if cfg.Model.IsDefined() {
_ = syscall.Unlink(cfg.Model.Filename)
}
// LoadSchema again now we have everything
if err := cfg.LoadSchema(); err != nil {
return nil, fmt.Errorf("failed to load schema: %w", err)
}
if err := cfg.Init(); err != nil {
return nil, fmt.Errorf("generating core failed: %w", err)
}
p := modelgen.New()
if mut, ok := p.(plugin.ConfigMutator); ok {
err := mut.MutateConfig(cfg)
if err != nil {
return nil, err
}
}
// Merge again now that the generated structs have been injected into the typemap
data, err := codegen.BuildData(cfg)
if err != nil {
return nil, fmt.Errorf("merging type systems failed: %w", err)
}
if err = codegen.GenerateCode(data); err != nil {
return nil, fmt.Errorf("generating core failed: %w", err)
}
return data, nil
}