What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

GraphQL Storefront API call to apply a Discount.

GraphQL Storefront API call to apply a Discount.

Kai_Yu
Tourist
4 0 1

Im making a private App that applies a discount to the Checkout.

Im have to use the Stroefront API since private Apps cant get

var discountUrl = TestStoreURL + '/api/graphql.json';
        var base64Id = Buffer.from(id.toString()).toString('base64');
        const checkoutQuery = `
        mutation checkoutDiscountCodeApplyV2($discountCode: String!, $checkoutId: ID!) {
            checkoutDiscountCodeApplyV2(discountCode: $discountCode, checkoutId: $checkoutId) {
              checkout {
                id
              }
              checkoutUserErrors {
                code
                field
                message
              }
            }
          }
          
          {
            checkoutId: ` + base64Id + `,
            discountCode: ` + discountCode + `
          }
          `;
        const results = await fetch(discountUrl, {
            method: 'POST', 
            uri: discountUrl,
            headers : {
                'X-Shopify-Storefront-Access-Token' : SHOPIFY_ACCESS_STOREFRONT_TOKEN_TEST,
                'Accept': 'application/json, text/plain, ',
                'Content-Type': 'application/json'
            },
            body : JSON.stringify(checkoutQuery)
          })
          .then(response => response.json())
          .then(json => {
            log.info(json);
          });

permisson for the Checkout scope of the Admin API.

So im trying to use the checkoutDiscountCodeApplyV2 mutation. But im always getting errors: 'Parameter Missing or Invalid'  and no futher information.

Here is the relevant code:

 

 

Replies 2 (2)

c10s
Shopify Partner
67 12 27

You likely need to pass the variables and query separately when using fetch.

const checkoutQuery = `
    mutation checkoutDiscountCodeApplyV2($discountCode: String!, $checkoutId: ID!) { 
        checkoutDiscountCodeApplyV2(discountCode: $discountCode, checkoutId: $checkoutId) {
            checkout {
                id
            } 
            checkoutUserErrors {
                code
                field
                message
            }
        }
    }
`;

 

body: JSON.stringify({
    query: checkoutQuery,
    variables: {
        checkoutId: base64Id,
        discountCode
    }
})

 

Kai_Yu
Tourist
4 0 1

Thanks for your reply I changed my code according to your suggestion and the variables are now send correctly.

Im currently sending the checkoutId as such:

Buffer.from('gid://shopify/Checkout/'+id.toString()).toString('base64');

 But i get the following Error as a response:

{
  code: 'INVALID',
  field: [ 'checkoutId' ],
  message: 'Checkout existiert nicht'
}

And i get the checkout if directly from the ctx.request.body.id.

Do you might know why i cant find the checkout?.

Do i hve to use the Checkout update instead of the Checkout creation webhook?