Skip to content

Instantly share code, notes, and snippets.

@wasalwayshere
Created September 22, 2023 02:56
Show Gist options
  • Save wasalwayshere/9ce2cb784a78625b55cfc62776829a13 to your computer and use it in GitHub Desktop.
Save wasalwayshere/9ce2cb784a78625b55cfc62776829a13 to your computer and use it in GitHub Desktop.
Shopify GraphQL Gather Product Media, Delete Media, Add Media (sample laravel class)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\ShopifyGraphQLQuery;
use Illuminate\Support\Facades\App;
use Log;
class ShopifyGraphQLProductAddMedia extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke($data)
{
$shop_url = $data['shop_url'];
$ShopifyGraphQLQuery = new ShopifyGraphQLQuery();
$media = $data['images'];
$productId = $data['id'];
// Gather existing media if any media
$query = 'query getProductMedia($productId: ID!) {
product(id: $productId) {
media(first: 100) {
edges {
node {
... on MediaImage {
id
}
... on Video {
id
}
... on Model3d {
id
}
... on ExternalVideo {
id
}
}
}
}
}
}
';
$variables = [
'productId' => $productId,
];
$response = $ShopifyGraphQLQuery->__invoke($shop_url, $query, $variables);
if (App::environment(['local', 'staging', 'testing'])) {
Log::info('ShopifyGraphQLProductAddMedia-found:', ['$response' => $response]);
}
$mediaIds = collect([]);
$mediaIds = isset($response['data']['product']['media']['edges']) ? collect($response['data']['product']['media']['edges'])->pluck('node.id') : collect([]);
if (App::environment(['local', 'staging', 'testing'])) {
Log::info('ShopifyGraphQLProductAddMedia-found:', ['$mediaIds' => $mediaIds->toArray()]);
}
if ($mediaIds->isNotEmpty()) {
$query = 'mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) {
productDeleteMedia(mediaIds: $mediaIds, productId: $productId) {
deletedMediaIds
deletedProductImageIds
mediaUserErrors {
# MediaUserError fields
code
field
message
}
product {
# Product fields
id
}
}
}';
$variables = [
'productId' => $productId,
'mediaIds' => $mediaIds->toArray(),
];
$response = $ShopifyGraphQLQuery->__invoke($shop_url, $query, $variables);
if (App::environment(['local', 'staging', 'testing'])) {
Log::info('ShopifyGraphQLProductAddMedia-delete:', ['$response' => $response]);
}
}
// Now add the media from the request
$query = 'mutation productCreateMedia($media: [CreateMediaInput!]!, $productId: ID!) {
productCreateMedia(media: $media, productId: $productId) {
media {
alt
mediaContentType
}
mediaUserErrors {
field
message
}
product {
id
title
}
}
}';
$variables = [
'productId' => $productId,
'media' => $media
];
$response = $ShopifyGraphQLQuery->__invoke($shop_url, $query, $variables);
if (App::environment(['local', 'staging', 'testing'])) {
Log::info('ShopifyGraphQLProductAddMedia:', ['$response' => $response]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment