Discussing Shopify Functions development, deployment, and usage in Shopify apps.
Hi everyone,
I'm currently working on Shopify function where I need to dynamically replace a static tag name in a GraphQL query with values stored in metafields. For example, in the code below, I'm checking the product tag statically ("winter-sale") using the hasAnyTag function. However, I want to check this tag dynamically using a metafield value instead.
I am using the Remix template to create a discount function and querying the above code in run.graphql.
query RunInput {
cart {
lines {
quantity
merchandise {
__typename
...on ProductVariant {
id
product {
id,
tags,
hasAnyTag(tags: ["winter-sale"]),
}
}
}
}
}
discountNode {
metafield(namespace: "$app:shopify-discount-new", key: "shopify-configuration-new") {
value
}
}
}
Here is the code for creating a discount and storing values in a metafield.
const response = await admin.graphql(
`#graphql
mutation CreateCodeDiscount($discount: DiscountCodeAppInput!) {
discountCreate: discountCodeAppCreate(codeAppDiscount: $discount) {
codeAppDiscount{
discountId
}
userErrors {
code
message
field
}
}
}`,
{
variables: {
discount: {
...baseCodeDiscount,
metafields: [
{
namespace: "$app:shopify-discount-new",
key: "shopify-configuration-new",
type: "json",
value: JSON.stringify({
quantity: configuration.quantity,
percentage: configuration.percentage,
tag: configuration.tag
}),
},
],
},
},
},
);
Could someone help me figure out how to replace the static tag ("winter-sale") with a value retrieved from a metafield?