Przeglądaj źródła

ProductTags query

- impl
Alexey Kim 2 lat temu
rodzic
commit
481dda7168
2 zmienionych plików z 46 dodań i 2 usunięć
  1. 39 0
      graphql/helper/product_tag.go
  2. 7 2
      graphql/query_product.go

+ 39 - 0
graphql/helper/product_tag.go

@@ -0,0 +1,39 @@
+package helper
+
+import (
+	"github.com/gshopify/service-wrapper/fun"
+	"github.com/gshopify/service-wrapper/model"
+	"gshopper.com/gshopify/products/graphql/generated"
+)
+
+func NewProductTagConnection(src []model.ProductTag,
+	first *int, last *int, reverse *bool) (*generated.StringConnection, error) {
+	var connection = &generated.StringConnection{}
+
+	if len(src) == 0 {
+		return connection, nil
+	}
+
+	if reverse != nil && *reverse {
+		src = fun.Reverse(src)
+	}
+
+	src = fun.First(src, first)
+	src = fun.Last(src, last)
+	connection.PageInfo = model.NewPageInfo(src, "", src[0], src[len(src)-1])
+
+	// Edges
+	for i := range src {
+		e := &generated.StringEdge{
+			Node: src[i].Tag,
+		}
+
+		if c, err := model.NewSimpleCursor(src[i].Tag, ""); err == nil {
+			e.Cursor = *c.String()
+		}
+
+		connection.Edges = append(connection.Edges, e)
+	}
+
+	return connection, nil
+}

+ 7 - 2
graphql/query_product.go

@@ -273,8 +273,13 @@ func (r *productVariantResolver) Title(ctx context.Context, variant *generated.P
 	return strings.Join(s, " / "), nil
 }
 
-func (r *queryResolver) ProductTags(ctx context.Context, first int) (*generated.StringConnection, error) {
-	panic("not implemented")
+func (r *queryResolver) ProductTags(_ context.Context, first int) (*generated.StringConnection, error) {
+	tags, err := r.db.ProductTags()
+	if err != nil {
+		return nil, err
+	}
+
+	return helper.NewProductTagConnection(tags, &first, nil, nil)
 }
 
 func (r *queryResolver) ProductTypes(ctx context.Context, first int) (*generated.StringConnection, error) {