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.
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.”
}
]
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
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:
Do customerSegmentMembers only to get the CustomerSegmentMember ID (gid://shopify/CustomerSegmentMember/xxx123), which is equivalent to their customer ID
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