List all variants with an empty barcode

Hi all, I’m having trouble listing all of the variant with an empty barcode.
When a variant is added to an existing product the barcode is empty, not null, I want to get this one.

So i tried doing this:

{
  productVariants(first:250, query: "barcode:''") {
    edges {
      node {
        id
        title
        barcode
      }
    }
  }
}

and the response i got is this:

{
  "data": {
    "productVariants": {
      "edges": []
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 13,
      "actualQueryCost": 2,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1998,
        "restoreRate": 100
      }
    },
    "search": [
      {
        "path": [
          "productVariants"
        ],
        "query": "barcode:''",
        "parsed": {
          "field": "barcode",
          "match_phrase": ""
        }
      }
    ]
  }
}

But i’m sure i have at least one product with a missing barcode, because if i do:

query {
  product(id: "gid://shopify/Product/9827940794707") {
    variants(first: 5) {
      edges {
        node {
          displayName
          barcode
        }
      }
    }
  }
}

I get:

{
  "data": {
    "product": {
      "variants": {
        "edges": [
          {
            "node": {
              "displayName": "Maglieria - Rosa / TU",
              "barcode": "000000001559"
            }
          },
          {
            "node": {
              "displayName": "Maglieria - Celeste / TU",
              "barcode": ""
            }
          }
        ]
      }
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 6,
      "actualQueryCost": 4,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1996,
        "restoreRate": 100
      }
    }
  }
}

What can i do to get all of the variants with an empty barcode? Thanks.

Shopify’s GraphQL API doesn’t support querying empty string fields directly using query: "barcode:''". Instead, you can fetch all product variants and filter out those with an empty barcode in your code.

hope this help

### Solution:

1. Fetch all variants:

graphql

CopyEdit

** **{** ** productVariants(first: 250) {** ** edges {** ** node {** ** id** ** title** ** barcode** ** }** ** }** ** }** **}** **

2. Filter in your application:
** * Loop through the response and check if barcode === "" (empty string).**

If using REST API, fetch /admin/api/2024-04/variants.json and filter variants where barcode is an empty string.

That’s exactly what i was using, I just wanted a better solution.
I tought some days ago I made up working only with a graphql query, but maybe I’m wrong.
Thank you for your help!

1 Like