How to Retrieve Specific DiscountRedeemCode Usage with GraphQL?

Hi everyone,

I’m currently migrating our discount validation logic from the REST Admin API (using PriceRules) to the GraphQL Admin API.

In one scenario, we have a discount that includes multiple DiscountRedeemCodes. I need to validate whether a specific redeem code has already reached its usage limit. To do this, I’m trying to retrieve the asyncUsageCount for a specific redeem code to block users from reusing codes that have already been exhausted.

However, I’m running into an issue when trying to filter the redeem codes by their actual code value. Here’s the query I’m using:

query GetDiscountInfo($code: String!) {
  codeDiscountNodeByCode(code: $code) {
    id
    codeDiscount {
      __typename
      ... on DiscountCodeBasic {
        asyncUsageCount
        usageLimit        
        codes(first: 1, query: "code:test123") {
          edges {
            node {
              id
              code
              asyncUsageCount
            }
          }
        }
      }
    }
  }
}

With the following variable:

{
  "code": "test123"
}

This results in the following warning and the filter I need is not being applied:

{
  "field": "code",
  "message": "Invalid search field for this query."
}

How can I fix the query so I can filter exactly the code that the user has used in the cart?

Thanks in advance for any advice or clarification!

Hi @Tiago_Esdras_st

The only supported way to get a specific redeem code and its asyncUsageCount is to fetch the codes for the discount and filter them client-side. You can use the codes connection with a reasonable first value (e.g., 50 or 100) and then search for the code you want in your application logic.

Example query:

query GetDiscountCodes {
  codeDiscountNodeByCode(code: "test123") {
    id
    codeDiscount {
      __typename
      ... on DiscountCodeBasic {
        usageLimit
        codes(first: 50) {
          edges {
            node {
              id
              code
              asyncUsageCount
            }
          }
        }
      }
    }
  }
}

Then, in your application, find the edge where node.code matches the code you want.

If you have the discount ID instead of the code, you can use the codeDiscountNode(id: ...) query in the same way.

Currently, the GraphQL Admin API does not support filtering the codes connection by code value on the server side. You must always fetch a page of codes and filter in your code.

Once you find the correct code node, you can check its asyncUsageCount and compare it to your usage limit.

@Liam-Shopify Thanks for the clarification!

This approach makes sense and I’ve been thinking about this implementation, but ideally, it would be great if we could filter the code connection by the specific code value directly in the GraphQL query.

In our case, a single discount may have a large number of DiscountRedeemCodes, and since we only have the code used by the user (not the specific ID), we may need to perform multiple requests with pagination just to find the right match. This could introduce performance issues and unnecessary complexity on our side.

As a comparison, with the Orders we can already do something like this:

query {
  orders(first: 10, query: "discount_code:Test123") {
    edges {
      node {
        id
        discountCode
      }
    }
  }
}

It would be very helpful if a similar filter supported discount redeem codes, as I tried to implement. Let me know if this is something planned or considered for future improvements.

Thanks again!

Hi @Tiago_Esdras_st

Totally get how this experience would be better if this could be accessible within a single query without needing to do additional searching and cross-referencing. This is currently not on the roadmap, but I can raise this as a feature request for the team.