forked from r0busta/go-shopify-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.go
184 lines (150 loc) · 3.96 KB
/
collection.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package shopify
import (
"context"
"fmt"
"github.com/r0busta/go-shopify-graphql-model/v3/graph/model"
log "github.com/sirupsen/logrus"
)
//go:generate mockgen -destination=./mock/collection_service.go -package=mock . CollectionService
type CollectionService interface {
ListAll() ([]model.Collection, error)
Get(id string) (*model.Collection, error)
Create(collection model.CollectionInput) (*string, error)
CreateBulk(collections []model.CollectionInput) error
Update(collection model.CollectionInput) error
}
type CollectionServiceOp struct {
client *Client
}
var _ CollectionService = &CollectionServiceOp{}
type mutationCollectionCreate struct {
CollectionCreateResult struct {
Collection *struct {
ID string `json:"id,omitempty"`
} `json:"collection,omitempty"`
UserErrors []model.UserError `json:"userErrors,omitempty"`
} `graphql:"collectionCreate(input: $input)" json:"collectionCreate"`
}
type mutationCollectionUpdate struct {
CollectionCreateResult struct {
UserErrors []model.UserError `json:"userErrors,omitempty"`
} `graphql:"collectionUpdate(input: $input)" json:"collectionUpdate"`
}
var collectionQuery = `
id
handle
title
products(first:250, after: $cursor){
edges{
node{
id
}
cursor
}
pageInfo{
hasNextPage
}
}
`
var collectionBulkQuery = `
id
handle
title
`
func (s *CollectionServiceOp) ListAll() ([]model.Collection, error) {
q := fmt.Sprintf(`
{
collections{
edges{
node{
%s
}
}
}
}
`, collectionBulkQuery)
res := []model.Collection{}
err := s.client.BulkOperation.BulkQuery(q, &res)
if err != nil {
return nil, fmt.Errorf("bulk query: %w", err)
}
return res, nil
}
func (s *CollectionServiceOp) Get(id string) (*model.Collection, error) {
out, err := s.getPage(id, "")
if err != nil {
return nil, err
}
nextPageData := out
hasNextPage := out.Products.PageInfo.HasNextPage
for hasNextPage && len(nextPageData.Products.Edges) > 0 {
cursor := nextPageData.Products.Edges[len(nextPageData.Products.Edges)-1].Cursor
nextPageData, err := s.getPage(id, cursor)
if err != nil {
return nil, err
}
out.Products.Edges = append(out.Products.Edges, nextPageData.Products.Edges...)
hasNextPage = nextPageData.Products.PageInfo.HasNextPage
}
return out, nil
}
func (s *CollectionServiceOp) getPage(id string, cursor string) (*model.Collection, error) {
q := fmt.Sprintf(`
query collection($id: ID!, $cursor: String) {
collection(id: $id){
%s
}
}
`, collectionQuery)
vars := map[string]interface{}{
"id": id,
}
if cursor != "" {
vars["cursor"] = cursor
}
out := struct {
Collection *model.Collection `json:"collection"`
}{}
err := s.client.gql.QueryString(context.Background(), q, vars, &out)
if err != nil {
return nil, fmt.Errorf("query: %w", err)
}
return out.Collection, nil
}
func (s *CollectionServiceOp) CreateBulk(collections []model.CollectionInput) error {
for _, c := range collections {
_, err := s.client.Collection.Create(c)
if err != nil {
log.Warnf("Couldn't create collection (%v): %s", c, err)
}
}
return nil
}
func (s *CollectionServiceOp) Create(collection model.CollectionInput) (*string, error) {
m := mutationCollectionCreate{}
vars := map[string]interface{}{
"input": collection,
}
err := s.client.gql.Mutate(context.Background(), &m, vars)
if err != nil {
return nil, fmt.Errorf("mutation: %w", err)
}
if len(m.CollectionCreateResult.UserErrors) > 0 {
return nil, fmt.Errorf("%+v", m.CollectionCreateResult.UserErrors)
}
return &m.CollectionCreateResult.Collection.ID, nil
}
func (s *CollectionServiceOp) Update(collection model.CollectionInput) error {
m := mutationCollectionUpdate{}
vars := map[string]interface{}{
"input": collection,
}
err := s.client.gql.Mutate(context.Background(), &m, vars)
if err != nil {
return fmt.Errorf("mutation: %w", err)
}
if len(m.CollectionCreateResult.UserErrors) > 0 {
return fmt.Errorf("%+v", m.CollectionCreateResult.UserErrors)
}
return nil
}