-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
109 lines (93 loc) · 2.37 KB
/
update.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package eywa
import (
"context"
"encoding/json"
"fmt"
)
func Update[M Model, MP ModelPtr[M]]() UpdateQueryBuilder[M] {
return UpdateQueryBuilder[M]{
QuerySkeleton: QuerySkeleton[M]{
ModelName: (*new(M)).ModelName(),
// fields: append(fields, field),
},
}
}
type UpdateQueryBuilder[M Model] struct {
QuerySkeleton[M]
}
func (uq UpdateQueryBuilder[M]) Set(fields ...Field[M]) UpdateQueryBuilder[M] {
uq.set = &set[M]{FieldArray[M](fields)}
for _, f := range fields {
if var_, ok := f.GetRawValue().(queryVar); ok {
uq.queryVars = append(uq.queryVars, var_)
}
}
return uq
}
func (uq UpdateQueryBuilder[M]) Where(w *WhereExpr) UpdateQueryBuilder[M] {
uq.where = &where{w}
return uq
}
func (uq *UpdateQueryBuilder[M]) MarshalGQL() string {
if uq.where == nil {
uq.where = &where{Not(&WhereExpr{})}
}
return fmt.Sprintf(
"update_%s",
uq.QuerySkeleton.MarshalGQL(),
)
}
func (uq UpdateQueryBuilder[M]) Select(field FieldName[M], fields ...FieldName[M]) UpdateQuery[M] {
return UpdateQuery[M]{
uq: &uq,
fields: append(fields, field),
}
}
type UpdateQuery[M Model] struct {
uq *UpdateQueryBuilder[M]
fields []FieldName[M]
}
func (uq UpdateQuery[M]) MarshalGQL() string {
return fmt.Sprintf(
"%s {\nreturning {\n%s\n}\n}",
uq.uq.MarshalGQL(),
FieldNameArray[M](uq.fields).MarshalGQL(),
)
}
func (uq UpdateQuery[M]) Query() string {
return fmt.Sprintf(
"mutation update_%s%s {\n%s\n}",
uq.uq.ModelName,
uq.uq.queryVars.MarshalGQL(),
uq.MarshalGQL(),
)
}
func (uq UpdateQuery[M]) Variables() map[string]interface{} {
vars := map[string]interface{}{}
for _, var_ := range uq.uq.queryVars {
vars[var_.name] = var_.value.Value()
}
return vars
}
func (uq UpdateQuery[M]) Exec(client *Client) ([]M, error) {
return uq.ExecWithContext(context.Background(), client)
}
func (uq UpdateQuery[M]) ExecWithContext(ctx context.Context, client *Client) ([]M, error) {
respBytes, err := client.Do(ctx, uq)
if err != nil {
return nil, err
}
type mutationReturning struct {
Returning []M `json:"returning"`
}
type graphqlResponse struct {
Data map[string]mutationReturning `json:"data"`
Errors []GraphQLError `json:"errors"`
}
respObj := graphqlResponse{}
err = json.NewDecoder(respBytes).Decode(&respObj)
if err != nil {
return nil, err
}
return respObj.Data[fmt.Sprintf("update_%s", uq.uq.ModelName)].Returning, nil
}