Shopify Functions Input Variables

I am trying to give an automatic discount to the customer with a particular tag
my run.graphql -

query RunInput($tags: [String!]!) { 
  cart {
    buyerIdentity {
      customer {
        metafield(namespace: "$app:my-namespace", key: "my-key") {
          value
        }
        hasTags(tags: $tags) { 
          hasTag
          tag
        }
      }
    }
    lines {
      id
      merchandise {
        ... on ProductVariant {
          id
          title
        }
        __typename
      }
    }
  }
}

first I run a graphql-

mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
  discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
    userErrors {
      field
      message
    }
    automaticAppDiscount {
      discountId
      title
      startsAt
      status
      appDiscountType {
        appKey
        functionId
      }
    }
  }
}
variables - 
{
  "automaticAppDiscount": {
    "title": "Take 5$ from discount",
    "functionId": "654dd37d-6384-4392-b05d-8e03f9f383d9",
    "startsAt": "2024-12-02T00:00:00Z"
  }
}

shopify.extension.toml -

[extensions.input.variables]
namespace = "$app:my-namespace"
key = "my-key"

then I run this mutation for metafield to set the variables -

mutation SetMetafield {
  metafieldsSet(metafields: [
    {
      namespace: "$app:my-namespace",
      key: "my-key",
      ownerId: "gid://shopify/DiscountAutomaticNode/1447703314665",
      type: "json",
      value: "{\"tags\": [\"trade_program\"]}"
    }
  ]) {
    metafields {
      id
    }
  }
}

but the issue comes when I run the above mutation to create metafield in GraphiQL built into ‘shopify app dev’ then it works fine but when I run it in the post man or the GraphiQL app it doesn’t work and gives the error of InvalidVariableValueError, so I am having no issue running it in development store but having issue on live stores because there I can not run GraphiQL built into ‘shopify app dev’

Here’s a couple things you can try:

1. Namespace and Key Validation

  • Ensure that the namespace ("$app:my-namespace") and key ("my-key") are correctly formatted and exist in the live store’s metafield definitions. If they are not defined in the live store, the mutation will fail.

2. Metafield Type

  • Confirm that the metafield type ("json") is correctly set up in the live store. If the metafield type doesn’t match the expected type, it will result in an error.

3. Owner ID Validation

  • The ownerId you are using ("gid://shopify/DiscountAutomaticNode/1447703314665") must correspond to a valid resource in the live store. Ensure that this ID exists and is accessible in the live store.

4. Access Scopes

  • Verify that your app has the required access scopes (write_discounts and write_metafields) enabled for the live store.

5. API Version Compatibility

  • Ensure that the API version you are using in Postman or the GraphiQL app matches the one used in the development store. Differences in API versions can lead to unexpected errors.

6. Variable Formatting

  • Double-check the formatting of the value field in your mutation. It should be a valid JSON string. For example:
     "{\"tags\": [\"trade_program\"]}"

Debugging Steps

To debug and resolve the issue:

  1. Test Namespace and Key: Use the metafieldDefinitions query to confirm the namespace and key exist in the live store.
  2. Validate Owner ID: Use the node query to check if the ownerId is valid in the live store.
  3. Check API Version: Ensure consistency in API versions across environments.
  4. Log Errors: Capture the full error response from Postman or the GraphiQL app to identify the exact issue.

The issue is likely due to metafield app ownership. You are using the $app namespace on your metafield, so the metafield value must be set by the same app that contains the function. The GraphiQL app is its own app, so $app will resolve to a different app. Using Postman might work, but you’d need to ensure that the API Key used is from your function app.

In production, better would be to create a UI Extension for your discount, which will set the metafield properly. You could create a configuration UI for the tag that way as well.