-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
56 lines (44 loc) · 1.28 KB
/
generator.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
53
54
55
56
package entx
import (
"entgo.io/contrib/entgql"
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
)
// Extension is an implementation of entc.Extension
type Extension struct {
entc.DefaultExtension
templates []*gen.Template
gqlSchemaHooks []entgql.SchemaHook
}
// ExtensionOption allow for control over the behavior of the generator
type ExtensionOption func(*Extension) error
// WithJSONScalar adds the JSON scalar definition
func WithJSONScalar() ExtensionOption {
return func(ex *Extension) error {
ex.gqlSchemaHooks = append(ex.gqlSchemaHooks, addJSONScalar)
return nil
}
}
// NewExtension returns an entc Extension that allows the entx package to generate
// the schema changes and templates needed to function
func NewExtension(opts ...ExtensionOption) (*Extension, error) {
e := &Extension{
templates: []*gen.Template{},
gqlSchemaHooks: []entgql.SchemaHook{},
}
for _, opt := range opts {
if err := opt(e); err != nil {
return nil, err
}
}
return e, nil
}
// Templates of the extension
func (e *Extension) Templates() []*gen.Template {
return e.templates
}
// GQLSchemaHooks of the extension to seamlessly edit the final gql interface
func (e *Extension) GQLSchemaHooks() []entgql.SchemaHook {
return e.gqlSchemaHooks
}
var _ entc.Extension = (*Extension)(nil)