Unable to Retrieve Media URL Using Shopify GraphQL API

Unable to Retrieve Media URL Using Shopify GraphQL API

HimanshuRahi
Shopify Partner
1 0 0

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?

Replies 0 (0)