How do i get metafields from a "discountAutomaticAppCreate" function

How do i get metafields from a "discountAutomaticAppCreate" function

Webwizzy
Shopify Partner
17 1 0

Hello, I have created a Shopify CLI Discount shipping - Function

 

The function works perfectly fine, it applies the discounts and I can read the metadata in the run.ts from the graphql.ts

 

However, I need the metafield(s) in the admin interface as well but they don't show up when I query with graphql?

This is the query:

 

query {
  discountNodes(query: "type:app AND method:automatic", first: 10) {
    nodes {
      id
      metafields(
        namespace: "$app:discount-shipping",
        first: 10
      ){
        nodes{
          jsonValue
        }
      }
      metafield(namespace: "$app:discount-shipping", key: "function-configuration") {
        jsonValue
      }
      discount {
        ... on DiscountAutomaticApp {
          __typename
          title
          startsAt
          endsAt
          endsAt
          status
          asyncUsageCount
          appliesOnSubscription
          discountId
          status
          appDiscountType {
          	title
            functionId
            description
        	}
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
        }
      }
    }
  }
}

 

 

I create the resource/node like so:

const automaticDiscount = await discountAutomaticAppCreate(
admin,
shippingDiscountFunctionId,
"$app:discount-shipping",
"function-configuration",
discountTitle,
discountStart,
discountEnd,
discountCombinesProducts === 'true',
discountCombinesOrders === 'true',
JSON.stringify({
targetSetting: targetSetting,
discountValue: Number(discountValue),
discountType: discountType,
})
);


And the helper method looks like this:

 

export async function discountAutomaticAppCreate(
    admin: AdminApiContext<RestResources>,
    functionId: string,
    namespace: string,
    key: string,
    discountTitle: string,
    discountStart: string,
    discountEnd: string,
    discountCombinesProducts: boolean,
    discountCombinesOrders: boolean,
    metafieldValue: string,

): Promise<{
    metafield?: {
        id: string,
        namespace: string,
        key: string,
        value: string,
    },
    userErrors?: {
        field: string[],
        message: string,
        code: string,
    }[]
}> {
    // Send the mutation to create or update the metafield
    const response = await admin.graphql(`
        mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
            discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
                userErrors {
                    field
                    message
                }
                automaticAppDiscount {
                    discountId
                    title
                    startsAt
                    endsAt
                    status
                    appDiscountType {
                        appKey
                        functionId
                    }
                    combinesWith {
                        orderDiscounts
                        productDiscounts
                        shippingDiscounts
                    }
                }
            }
        }
    `,
        {
            variables: {
                automaticAppDiscount: {
                    "title": discountTitle,
                    "functionId": functionId,
                    "combinesWith": {
                        "orderDiscounts": discountCombinesOrders,
                        "productDiscounts": discountCombinesProducts,
                        "shippingDiscounts": false
                    },
                    "startsAt": discountStart,
                    "endsAt": discountEnd,
                    "metafields": [
                        {
                            "namespace": namespace,
                            "key": key,
                            "type": "json",
                            "value": metafieldValue,
                        }
                    ]
                }
            }
        }
    );

    const jsonResponse = await response.json();
    return jsonResponse.data;
}

 


As mentioned above, the creation works, and the discount works, I can confirm the metafields are being used in the Shopify Partners->Ingsights->functions->myfunction

 

Here I can see the script has the metafield in the success input:

{  "cart": {    "deliveryGroups": [      {
        "id": "gid://shopify/CartDeliveryGroup/0",        "deliveryOptions": [          {
            "cost": {              "amount": "49.0",              "currencyCode": "DKK"
            }
          },
          {
            "cost": {              "amount": "666.0",              "currencyCode": "DKK"
            }
          }
        ]
      }
    ],
    "cost": {      "subtotalAmount": {        "amount": "7500.0",        "currencyCode": "DKK"
      }
    }
  },
  "discountNode": {    "metafield": {      "jsonValue": {        "targetSetting": "5555",        "discountValue": 0,        "discountType": "free"
      }
    }
  }
}


How do I get the metadata added to my query response in my admin panel with the helper function specified above?

Fullstack Developer - Founder of the cryptocurrency market data aggregator, https://www.bitculator.com
Reply 1 (1)

Webwizzy
Shopify Partner
17 1 0

Heads up, if i change the namespace at creation from "$app:discount-shipping" to a random string like: test_meta_field_test and use that in my query, then I get the metafields!

But that's not very generic/dynamic? that means I have to hardcode this none app specific namespace to my run.graphql too... Is there really no way to get the app specific metafield ?

Fullstack Developer - Founder of the cryptocurrency market data aggregator, https://www.bitculator.com