Discussing APIs and development related to customers, discounts, and order management.
I got this error `Variable $automaticAppDiscount of type DiscountAutomaticAppInput! was provided invalid value` when run the Admin GraphQL API discountAutomaticAppCreate. How to fix that?
API version was 2024-04.
My input was
{ "automaticAppDiscount": { "title": "some random name", "functionId": "<my function ID>", "combinesWith": { "orderDiscounts": false, "productDiscounts": true, "shippingDiscounts": false }, "startsAt": "2024-06-02T17:09:21Z", "endsAt": null, "metafields": null } }
I used postman to make an API call successfully. Then I called another request using Golang code, just replace the title because it must be unique. The error occurred.
My code look something like below:
package shopify
import (
"context"
"fmt"
"go-shopify-graphql-model/graph/model"
)
type DiscountService interface {
AutomaticAppCreate(ctx context.Context, discount model.DiscountAutomaticAppInput) (output *model.DiscountAutomaticApp, err error)
}
type DiscountServiceOp struct {
client *Client
}
var _ DiscountService = &DiscountServiceOp{}
type mutationDiscountAutomaticAppCreate struct {
DiscountAutomaticCreateAppPayload model.DiscountAutomaticAppCreatePayload `json:"discountAutomaticAppCreate"`
}
var discountAutomaticAppCreate = `
mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
userErrors {
field
message
}
automaticAppDiscount {
discountId
title
startsAt
endsAt
status
appDiscountType {
appKey
functionId
}
combinesWith {
orderDiscounts
productDiscounts
shippingDiscounts
}
}
}
}
`
func (s *DiscountServiceOp) AutomaticAppCreate(ctx context.Context, input model.DiscountAutomaticAppInput) (output *model.DiscountAutomaticApp, err error) {
out := mutationDiscountAutomaticAppCreate{}
vars := map[string]interface{}{
"title": input.Title,
"functionId": input.FunctionID,
"startsAt": input.StartsAt,
"endsAt": input.EndsAt,
"combinesWith": input.CombinesWith,
"metafields": input.Metafields,
}
if err := s.client.gql.MutateString(ctx, discountAutomaticAppCreate, vars, &out); err != nil {
return nil, fmt.Errorf("gql.MutateString: %w", err)
}
if len(out.DiscountAutomaticCreateAppPayload.UserErrors) > 0 {
return nil, fmt.Errorf("%+v", out.DiscountAutomaticCreateAppPayload.UserErrors)
}
return out.DiscountAutomaticCreateAppPayload.AutomaticAppDiscount, nil
}
Solved! Go to the solution
This is an accepted solution.
I found an error in `vars`. It should be
vars := map[string]interface{}{
"automaticAppDiscount": map[string]interface{} {
"title": input.Title,
"functionId": input.FunctionID,
"startsAt": input.StartsAt,
"endsAt": input.EndsAt,
"combinesWith": input.CombinesWith,
"metafields": input.Metafields,
}
}
This is an accepted solution.
I found an error in `vars`. It should be
vars := map[string]interface{}{
"automaticAppDiscount": map[string]interface{} {
"title": input.Title,
"functionId": input.FunctionID,
"startsAt": input.StartsAt,
"endsAt": input.EndsAt,
"combinesWith": input.CombinesWith,
"metafields": input.Metafields,
}
}