|
@@ -124,6 +124,80 @@ func (r *productResolver) Variants(ctx context.Context, product *generated.Produ
|
|
return helper.NewProductVariantConnection(variants, after, before, first, last, reverse, sort)
|
|
return helper.NewProductVariantConnection(variants, after, before, first, last, reverse, sort)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (r *productResolver) VariantBySelectedOptions(ctx context.Context, product *generated.Product, input []*generated.SelectedOptionInput) (*generated.ProductVariant, error) {
|
|
|
|
+ options, err := r.Options(ctx, product, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ size := len(options)
|
|
|
|
+ if size != len(input) {
|
|
|
|
+ return nil, fmt.Errorf("illegal SelectedOptionInput")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ type ext struct {
|
|
|
|
+ id string
|
|
|
|
+ index int
|
|
|
|
+ }
|
|
|
|
+ var selected = make([]ext, 0)
|
|
|
|
+
|
|
|
|
+ for i := range input {
|
|
|
|
+ for k := range options {
|
|
|
|
+ if options[k].Name != input[i].Name {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for j, value := range options[k].Values {
|
|
|
|
+ if value != input[i].Value {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ selected = append(selected, ext{
|
|
|
|
+ id: options[k].ID,
|
|
|
|
+ index: j + 1,
|
|
|
|
+ })
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if size != len(selected) {
|
|
|
|
+ return nil, fmt.Errorf("illegal SelectedOptionInput")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ variants, err := r.Variants(ctx, product, nil, nil, nil, nil, nil, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, node := range variants.Nodes {
|
|
|
|
+ var i = 0
|
|
|
|
+
|
|
|
|
+ for _, e := range selected {
|
|
|
|
+ for _, meta := range node.Metafields {
|
|
|
|
+ if meta.Namespace != model.GidOption.String() {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if t := model.MetaFieldType(meta.Type); !t.IsValid() || model.MetaFieldTypeNumberInteger != t {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if e.id == meta.Key && fmt.Sprintf("%d", e.index) == meta.Value {
|
|
|
|
+ i++
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if i == size {
|
|
|
|
+ return node, nil
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return nil, nil
|
|
|
|
+}
|
|
|
|
+
|
|
func (r *productVariantResolver) Product(ctx context.Context, variant *generated.ProductVariant) (*generated.Product, error) {
|
|
func (r *productVariantResolver) Product(ctx context.Context, variant *generated.ProductVariant) (*generated.Product, error) {
|
|
var (
|
|
var (
|
|
inContext *middleware.GShopifyContext
|
|
inContext *middleware.GShopifyContext
|