Hi,
I’m building a storefront with Shopify GraphQL API and PHP. So far, everything is working fine, I’ve managed to add variables to my queries (collection id, sortKey…) but whenever I try to filter products, I get error responses.
Here is my code:
$graphql_url = 'https://'.$API_KEY.':'.$PASSWORD.'@'.$SHOP_URL.'/admin/api/2022-10/graphql.json';
$id = $collection_id;
$sortKey = 'BEST_SELLING';
$filters = '{
productMetafield:{
namespace:"custom",
key:"color",
value:"Noir"
}
}';
/* GraphQL */
$query = <<<'GQL'
query MyRequest($id: ID!, $sortKey: ProductCollectionSortKeys, $filters: ProductFilter) {
collection(id: $id) {
products(first:12, sortKey:$sortKey, filters: $filters) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
handle
vendor
totalInventory
images (first:1) {
edges{
node{ url }
}
}
metafields (first: 7){
edges {
node {
namespace
key
value
}
}
}
variants (first:1) {
edges {
node {
price
id
}
}
}
}
}
}
}
}
GQL;
$post_data = array();
$post_data['query'] = $query;
$post_data['variables'] = [
'id' => $id,
'sortKey' => $sortKey,
'filters' => $filters,
];
Even when “filters” is just a string, I get this error:
ProductFilter isn't a defined input type (on $filters)
Do you have any idea why?
Is there anywhere I could find a example code of filters using PHP?
Thanks a lot for your help!
Hello there
To filter products using GraphQL and PHP, you can follow these steps:
- Define a GraphQL query that includes arguments for the filters you want to apply. For example, you might define a products query that takes arguments for the minimum and maximum price, as well as a search string:
query products($minPrice: Float, $maxPrice: Float, $search: String) {
products(minPrice: $minPrice, maxPrice: $maxPrice, search: $search) {
id
name
price
}
}
- In your PHP code, use a GraphQL library (such as graphql-php) to execute the query and pass in the filter arguments. For example:
$minPrice = 100;
$maxPrice = 200;
$search = 'shirt';
$result = $client->executeQuery($query, [
'minPrice' => $minPrice,
'maxPrice' => $maxPrice,
'search' => $search,
]);
-
In your PHP code, define a resolver function for the products field in the GraphQL schema. This resolver function should use the filter arguments to build a database query that retrieves the appropriate products from the database. For example:
Make sure to bind the resolver function to the products field in the GraphQL schema.
hope this helps!
Hi, thank you so much for replying.
Unfortunately, it doesn’t work neither. Even if my search is not a variable, I get almost the same error:
Field ‘products’ doesn’t accept argument ‘search’.
I even tried the exact same code as in the documentation: https://shopify.dev/custom-storefronts/products-collections/filter-products#query-products-by-metafield-value
But I still have the same return: Field ‘products’ doesn’t accept argument ‘filter’.
Even when I use the argument “query”: Field ‘products’ doesn’t accept argument ‘query’.
I have tried everything and I really don’t understand why the doc is so misleading.
I think I found something.
When I use the GraphQL explorer, I get the same errors. However, when I switch from Admin API to Storefront API, the query works.
So the solution to my problem is that I’m not using the right API.
So now my question is, how can I switch from Admin API to Storefront API with my code? Do I only have to change the endpoint?
Thank you!
Ok I found out how to switch to Storefront API.
For those who might be interested, I changed the URL from:
$graphql_url = 'https://'.$API_KEY.':'.$PASSWORD.'@'.$SHOP_URL.'/admin/api/2022-10/graphql.json';
to:
$graphql_url = 'https://'.$STOREFRONT_API_KEY.':'.$PASSWORD.'@'.$SHOP_URL.'/api/2022-10/graphql.json';
I also had to change to headers from application/json to application/graphql. The query is a bit different also.
Now I’m close to make it work as intended, but I still struggle to pass my PHP array to my GraphQL query.
$filters = '{productMetafield:{namespace:"custom",key:"color",value:"Noir"}}';
/* GraphQL */
$query = <<<'GQL'
query MyRequest($id: ID!, $cursor: String, $sortKey: ProductCollectionSortKeys, $filters: [ProductFilter!]) {
collection(id: $id) {
products(
first:12
sortKey:$sortKey
after:$cursor
filters: $filters
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
handle
vendor
totalInventory
images (first:1) {
edges{
node{ url }
}
}
color: metafield(namespace: "custom", key: "color") {
value
type
}
vendor_collection: metafield(namespace: "custom", key: "vendor_collection") {
value
}
variants (first:1) {
edges {
node {
id
priceV2 {
amount
}
}
}
}
}
}
}
}
}
GQL;
$post_data = array();
$post_data['query'] = $query;
$post_data['variables'] = [
'id' => $id,
'cursor' => $cursor,
'sortKey' => $sortKey,
'filters' => $filters,
];
I get an error saying that “Variable $filters of type [ProductFilter!] was provided invalid value for 0”.
However, the query works when filters are not a variable.
Any idea?
Aaand I found the solution… Here is how you have to format your filter array in PHP:
$filters = [
'productMetafield' =>
[
'namespace' => 'custom',
'key' => 'color',
'value' => 'Noir'
]
];