forked from entkit/entkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entgql.go
62 lines (52 loc) · 1.38 KB
/
entgql.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
57
58
59
60
61
62
package entkit
import (
"entgo.io/contrib/entgql"
_ "entgo.io/contrib/entgql"
"entgo.io/ent/entc/gen"
"fmt"
"github.com/vektah/gqlparser/v2/ast"
)
func NewEntgqlExtension(opts ...entgql.ExtensionOption) (*entgql.Extension, error) {
plural := gen.Funcs["plural"].(func(string) string)
camel := gen.Funcs["camel"].(func(string) string)
return entgql.NewExtension(
append(
opts,
entgql.WithSchemaGenerator(),
entgql.WithWhereInputs(true),
entgql.WithSchemaHook(
func(graph *gen.Graph, s *ast.Schema) error {
for _, n := range graph.Nodes {
name := camel(plural(n.Name))
f := s.Types["Query"].Fields.ForName(name)
if f == nil {
return fmt.Errorf("missing query field %q", name)
}
f.Arguments = append(f.Arguments, &ast.ArgumentDefinition{
Description: "Search query",
Name: "q",
Type: ast.NamedType("String", nil),
})
}
return nil
},
),
)...,
)
}
type Mutation struct {
description string
isCreate bool
}
func NewMutation(description string, isCreate bool) Mutation {
return Mutation{
description: description,
isCreate: isCreate,
}
}
func (v Mutation) IsCreate() bool { return v.isCreate }
func (v Mutation) GetDescription() string { return v.description }
func (v Mutation) Description(desc string) entgql.MutationOption {
v.description = desc
return v
}