-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_one.go
More file actions
101 lines (87 loc) · 2.29 KB
/
insert_one.go
File metadata and controls
101 lines (87 loc) · 2.29 KB
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
package eywa
import (
"context"
"encoding/json"
"fmt"
)
func InsertOne[M Model, MP ModelPtr[M]](field Field[M], fields ...Field[M]) InsertOneQueryBuilder[M] {
arr := FieldArray[M](fields)
arr = append(arr, field)
return InsertOneQueryBuilder[M]{
QuerySkeleton: QuerySkeleton[M]{
ModelName: (*new(M)).ModelName(),
// fields: append(fields, field),
queryArgs: queryArgs[M]{
object: &object[M]{arr},
},
},
}
}
type InsertOneQueryBuilder[M Model] struct {
QuerySkeleton[M]
}
func (iq InsertOneQueryBuilder[M]) OnConflict(constraint Constraint[M], fields ...FieldName[M]) InsertOneQueryBuilder[M] {
iq.QuerySkeleton.queryArgs.onConflict = &onConflict[M]{
constraint: constraint,
updateColumns: fields,
}
return iq
}
func (iq *InsertOneQueryBuilder[M]) MarshalGQL() string {
return fmt.Sprintf(
"insert_%s_one%s",
iq.QuerySkeleton.ModelName,
iq.queryArgs.MarshalGQL(),
)
}
func (iq InsertOneQueryBuilder[M]) Select(field FieldName[M], fields ...FieldName[M]) InsertOneQuery[M] {
return InsertOneQuery[M]{
iq: &iq,
fields: append(fields, field),
}
}
type InsertOneQuery[M Model] struct {
iq *InsertOneQueryBuilder[M]
fields []FieldName[M]
}
func (iq InsertOneQuery[M]) MarshalGQL() string {
return fmt.Sprintf(
"%s {\n%s\n}",
iq.iq.MarshalGQL(),
FieldNameArray[M](iq.fields).MarshalGQL(),
)
}
func (iq InsertOneQuery[M]) Query() string {
return fmt.Sprintf(
"mutation insert_%s_one%s {\n%s\n}",
iq.iq.ModelName,
iq.iq.queryVars.MarshalGQL(),
iq.MarshalGQL(),
)
}
func (iq InsertOneQuery[M]) Variables() map[string]interface{} {
vars := map[string]interface{}{}
for _, var_ := range iq.iq.queryVars {
vars[var_.name] = var_.value.Value()
}
return vars
}
func (iq InsertOneQuery[M]) Exec(client *Client) (*M, error) {
return iq.ExecWithContext(context.Background(), client)
}
func (iq InsertOneQuery[M]) ExecWithContext(ctx context.Context, client *Client) (*M, error) {
respBytes, err := client.Do(ctx, iq)
if err != nil {
return nil, err
}
type graphqlResponse struct {
Data map[string]*M `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("insert_%s_one", iq.iq.ModelName)], nil
}