PHP code example of thalia / shopify-rest-to-graphql

1. Go to this page and download the library: Download thalia/shopify-rest-to-graphql library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thalia / shopify-rest-to-graphql example snippets


use Thalia\ShopifyRestToGraphql\GraphqlService;

$shopifyGraphql = new GraphqlService($shop, $accessToken);
$product = $shopifyGraphql->graphqlPostProduct($params);
print_r($product);


    $query = 'query { shop { name email } }';
    $variables = [];

    $response = $shopifyGraphql->graphqlQueryThalia($query, $variables);

    if (isset($response['errors'])) {
        echo "Error: " . $response['errors']['message'];
    } else {
        print_r($response);
    }
    

    $query = 'mutation createProduct($title: String!) { productCreate(input: { title: $title }) { product { id      title } } }';

    $variables = [
        'title' => 'New Product'
    ];

    $response = $shopifyGraphql->graphqlQueryThalia($query, $variables);

    if (isset($response['errors'])) {
        echo "Error: " . $response['errors']['message'];
    } else {
        print_r($response);
    }
    

use Thalia\ShopifyRestToGraphql\Endpoints\OauthEndpoints;

$oauth = new OauthEndpoints($shop, $appApiKey, $appSecret);

// 1) Redirect the merchant to install (unchanged):
$url = $oauth->getAuthorizeUrl($appScope, $redirectUrl);

$tokens  = $oauth->getExpiringAccessToken($_GET['code']);
// $tokens = [access_token, expires_in, refresh_token, refresh_token_expires_in, scope]

$storage = OauthEndpoints::toStorage($tokens);
// $storage = [access_token, refresh_token, scope, expires_at, refresh_token_expires_at]
// → persist $storage keyed by the shop domain

if (time() >= $storage['expires_at'] - 300) {                 // 5-minute skew
    $rotated = $oauth->refreshOfflineAccessToken($storage['refresh_token']);
    $storage = OauthEndpoints::toStorage($rotated);           // persist BEFORE using
}
$service = new \Thalia\ShopifyRestToGraphql\GraphqlService($shop, $storage['access_token']);

try {
    $result = $service->graphqlQueryThalia($query);
} catch (\Thalia\ShopifyRestToGraphql\GraphqlException $e) {
    if (! $e->isUnauthorized()) {
        throw $e;
    }
    $storage = OauthEndpoints::toStorage($oauth->refreshOfflineAccessToken($storage['refresh_token']));
    // persist $storage, rebuild $service with the new access token, retry once
}

$tokens  = $oauth->exchangeIdTokenForOfflineToken($idToken);  // expiring=1 by default
$storage = OauthEndpoints::toStorage($tokens);                // persist

$tokens  = $oauth->migrateToExpiringToken($existingNonExpiringToken);
$storage = OauthEndpoints::toStorage($tokens);                // persist immediately
// The original non-expiring token is REVOKED on success.

$productData = [
    'product' => [
        'title' => 'New Product',
        'body_html' => '<p>Product description</p>',
        'vendor' => 'My Vendor',
        'product_type' => 'Clothing',
        'tags' => 'tag1,tag2',
        'variants' => [
            [
                'price' => '19.99',
                'sku' => 'SKU123',
                'inventory_management' => 'shopify'
            ]
        ],
        'images' => [
            [
                'src' => 'https://example.com/image.jpg',
                'alt' => 'Product Image'
            ]
        ]
    ]
];

$response = $shopifyGraphql->graphqlPostProduct($productData);

$updateData = [
    'product' => [
        'id' => '1234567890',
        'title' => 'Updated Product Title',
        'tags' => 'updated,tags',
        'variants' => [
            [
                'id' => '9876543210',
                'price' => '29.99'
            ]
        ]
    ]
];

$response = $shopifyGraphql->graphqlUpdateProduct($updateData);

$params = [
    'limit' => 10,
    'vendor' => 'Example Vendor',
    'published_status' => 'published',
    'fields' => 'id,title,variants,tags'
];

$products = $shopifyGraphql->graphqlGetProducts($params);

        $images = [
            [
                'url' => 'https://fastly.picsum.photos/id/365/200/300.jpg?hmac=n_4DxqK0o938eabBZRnEywWtPwgF2MKoTfnRmJ7vlKQ',
                'alt' => 'lorem ipsum',
            ]
        ];

$response = $shopifyGraphql->graphqlCreateProductImage($images, $productShopifyId);

        $imageIds = [
            "4145546145154","5456564465234",
        ];


$response = $shopifyGraphql->graphqlDeleteProductImage($imageIds, $productShopifyId);

use Thalia\ShopifyRestToGraphql\Endpoints\ProductsEndpoints;

$productsEndpoint = new ProductsEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\VariantsEndpoints;

$variantsEndpoint = new VariantsEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\CollectionsEndpoints;

$collectionsEndpoint = new CollectionsEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\OrdersEndpoints;

$ordersEndpoint = new OrdersEndpoints($shop, $accessToken);

$orders = $ordersEndpoint->getOrders(['limit' => 50, 'query' => ['status' => 'any']]);

$orders = $ordersEndpoint->getOrders([
    'limit' => 50,
    'fields' => ['id', 'name', 'displayFulfillmentStatus'],
]);

use Thalia\ShopifyRestToGraphql\Endpoints\InventoryEndpoints;

$inventoryEndpoint = new InventoryEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\ApplicationChargesEndpoints;

$chargesEndpoint = new ApplicationChargesEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\MetafieldsEndpoints;

$metafieldsEndpoint = new MetafieldsEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\FulfillmentsEndpoints;

$fulfillmentsEndpoint = new FulfillmentsEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\WebhooksEndpoints;

$webhooksEndpoint = new WebhooksEndpoints($shop, $accessToken);

use Thalia\ShopifyRestToGraphql\Endpoints\ThemesEndpoints;

$themesEndpoint = new ThemesEndpoints($shop, $accessToken);
sh
php artisan vendor:publish --provider="Thalia\ShopifyRestToGraphql\ShopifyRestToGraphqlServiceProvider"