All things Shopify and commerce
I'm encountering an issue while attempting to retrieve the URL of a media item created using the Shopify GraphQL API. Despite successfully creating the media item, I'm unable to retrieve its URL programmatically.
Here's a simplified version of my code:
Query
public function filesQuery($query)
{
$query = <<<QUERY
query {
files(first: 3, query: "$query") {
edges {
node {
id
createdAt
updatedAt
alt
... on MediaImage {
id
image {
id
url
altText
height
width
}
}
}
}
}
}
QUERY;
return $query;
}
public function files($par)
{
$query = $this->filesQuery($par);
$response = $this->client->query(['query' => $query]);
return $response->getBody();
}
// Initialize Shopify GraphQL client
$shopify = new ShopifyGraphQL();
// Create a media item
$media = $shopify->createMedia(123456, [
'alt' => 'test',
'originalSource' => 'https://source.unsplash.com/random',
'mediaContentType' => 'IMAGE',
]);
// Extract the media ID
$mediaId = $media['data']['productCreateMedia']['media'][0]['id'];
$mediaIdParts = explode('/', $mediaId);
$mediaId = end($mediaIdParts);
// Attempt to retrieve media information including the URL
$result = $shopify->files('product_id:1234 AND id:'.$mediaId);
// $result contains empty data, including no URL information
return $result;
Result :
{
"data": {
"files": {
"edges": [
]
}
},
}
Despite the successful creation of the media item, the files method call to retrieve its information returns empty data, including no URL information. I've verified that the media ID is correct and that the API credentials have the necessary permissions.
Could anyone provide insights into why I might be experiencing this issue?
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024