Getting customer.state from customerSegmentMembers

Hi all,

I need to get the value of customer.state only on select customers with a specific metafield value, and from what I can see you can only do this filter via customerSegmentMembers while the customer.state is only accessible either from customer or customers query.

While I can run the customerSegmentMembers first to get a list of customer ids before running them against customer or customers, this can take a while especially when there’s 200-300 customer ids to check. My current thought is to aggregate all ids and use them as filter to run the customers query (for example id:1000 OR id:1001 OR id:1002 etc), but I don’t currently know the limit of how far I can chain this.

Does anyone have a more elegant solution?

You can query the customers using the GraphQL customers query.
That allows filtering by state, if you’ve allowed the metafield to be used as a filter you can do that here too. e.g. state:X and metafield.name:Y
This will then return a list of customers which you can use customers - GraphQL Admin

Thanks Jordan, but that doesn’t seem to work on 2024-10.

The payload returns warning as follow:

“warnings”: [
{
“field”: “state”,
“message”: “Invalid search field for this query.”
},
{
“field”: “metafield.customer_fields.sales_rep”,
“message”: “Invalid search field for this query.”
}
]

Although API version 2024-04 accepts the state field, but neither this nor the 2024-10 accepts the metafield field.

Tried all combinations for metafield filter:

  • Metafield.namespace.key
  • metafield.key
  • metafields.namespace.key
  • metafields.key

None seems to work.

Ah my mistake, I was on the older API version, sorry about that.
I looked into this more for you and I think I’ve figured it out.

Firstly I think you need to create a segment for your customers with a specific metafield value, here’s Shopifys docs on how to do that https://help.shopify.com/en/manual/customers/customer-segmentation/reference-guide/metafield-segments
I think your metafield should not be prefixed with $app for this.

Once you’ve got that you’ll have a segment with customers with that specific metafield value.
You can then use the customerSegmentMembers - GraphQL Admin query, with the ID of the segment you created and I think this is the segment query you’ll need Segment query language reference


Separately, if you do want to filter by metafields outside of segments this is how you can do that Query by metafield value just for reference

Thanks Jordan.

Seems like filtering is only available on product metafields, not customers. That’s annoying :sweat_smile:

Yeah I figured that even without creating a segment prior, you can still filter customers directly using customerSegmentMembers like so:

{
  customerSegmentMembers(
    query: "metafields.customer_fields.sales_rep = 'Example Value'"
    first: 250
  ) {
    edges {
      node {
        id
      }
    }
    totalCount
  }
}

(this is just an example, in production mode I’ll have to apply pagination)

Sadly this query doesn’t have the same output options as customer or customers query, namely the customer.state that I require. Returned payload of customerSegmentMembers query.


I guess I wasn’t making myself clear enough in the OP.

What I want is to get a list of customers with specific metafield value (which can only be done via customerSegmentMember it seems), then output their email and account state.

As customer.state is only accessible via customer or customers query unless I miss something, this is my current proposed pipeline:

  1. Do customerSegmentMembers only to get the CustomerSegmentMember ID (gid://shopify/CustomerSegmentMember/xxx123), which is equivalent to their customer ID
  2. Chain those IDs as filter arguments to customers query, like so:
{
  customers(first: 10, query: "id:7260490137886 OR id:7260490170654 OR {{...chain all IDs like this}}") {
    edges {
      node {
        email
        displayName
        state
      }
    }
  }
}

I don’t know the limit of how many arguments can I chain in a single query, but I’ve tried 10 ORs in the graphiql app and it seems to work fine. I’m planning to try using python when we’re back in the office next year to see the limit of this filter chaining method.

As you can see this method is ugly and there’s so much issue with this I don’t even know where to start, but at the moment this seems to be the only solution.

I’m just looking if anyone has brighter idea than me :smiley: