Edit Discount Created with a Shopify Functions - How to get the Method context? (Code vs Automatic)

When clicking a previously created Discount in Shopify backend - How do I get the Discount Method (Code vs Automatic) when loading my Shopify Discount React file edit.jsx?

Is It passed down from the Shopify Discount list to my React file and how?

Or do I need to make an additional GraphQL query to defined the the Method (Code vs Automatic) before being able to define if Im dealing with A Code or Automatic discount?

Once I know the type of discount Im dealing with I can query GraphQL with either codeDiscountNode or automaticDiscountNode

Visual context:

Hi @Nico4

Check out this example. When querying a discountNode, you can check its type name and use inline fragments to get the appropriate fields.

query GetDiscount($id: ID!) {
    discountNode(id: $id) {
      id
      configurationField: metafield(
        namespace: "${metafields.namespace}"
        key: "${metafields.key}"
      ) {
        id
        value
      }
      discount {
        __typename
        ... on DiscountAutomaticApp {
          title
          discountClass
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
          startsAt
          endsAt
        }
        ... on DiscountCodeApp {
          title
          discountClass
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
          startsAt
          endsAt
          usageLimit
          appliesOncePerCustomer
          codes(first: 1) {
            nodes {
              code
            }
          }
        }
      }
    }
  }
1 Like