thalia / shopify-rest-to-graphql
Shopify REST to GraphQL : Converts Shopify REST API payloads to GraphQL queries with minimal changes for seamless Laravel integration.
Requires
- php: >=7.4
- guzzlehttp/guzzle: >=7.5
README
This package designed to help developers migrate from Shopify's REST API to the more modern and efficient GraphQL API. The package provides a set of utility functions and endpoints that map REST API functionality to GraphQL queries and mutations, making it easier to transition your Laravel application to use Shopify's GraphQL API. NOTE : This is experimental package
Features
- Easy Integration: Simplifies the integration of Shopify's GraphQL API into your Laravel application.
- Minimal Changes: Converts Shopify REST API payloads to GraphQL with minimal modifications, reducing development time and effort.
Table of Contents
Overview
Shopify's GraphQL API offers several advantages over the REST API, including more efficient data fetching, fewer requests, and more flexible queries. This Laravel package provides a seamless way to transition from the REST API to GraphQL by offering utility functions and endpoints that mimic REST API behavior but use GraphQL under the hood.
Installation
To install the package in your Laravel application, follow these steps:
composer require thalia/shopify-rest-to-graphql
Publish the configuration file:
php artisan vendor:publish --provider="Thalia\ShopifyRestToGraphql\ShopifyRestToGraphqlServiceProvider"
Usage
The package provides a GraphqlService class with methods to handle various Shopify GraphQL operations.
use Thalia\ShopifyRestToGraphql\GraphqlService; $shopifyGraphql = new GraphqlService($shop, $accessToken); $product = $shopifyGraphql->graphqlPostProduct($params); print_r($product);
Example Usage for calling graphql query
-
Basic Query Without Variables
$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 With Variables
$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); }
GraphqlService Class
The GraphqlService
class is the core of this package and provides direct interaction with Shopify's GraphQL API.
Available Methods
General GraphQL
graphqlQueryThalia(string $query, array $variables = [])
: Execute any GraphQL query with optional variablesgraphQLQuery($query, $shop, $accessToken)
: Alternative method to execute GraphQL queries with different credentials
Products
graphqlPostProduct($params)
: Create a new productgraphqlPostProductWithVariants($params)
: Create a product with multiple variantsgraphqlUpdateProduct($params)
: Update an existing productgraphqlGetProducts($params)
: Get a list of products with filtering optionsgraphqlGetProductsCount()
: Get the total count of productsgraphqlGetProduct($shopifyid)
: Get a single product by IDgraphqlGetProductWithoutInventory($shopifyid)
: Get a product without inventory informationgraphqlDeleteProduct($shopifyid)
: Delete a productgraphqlCheckProductOnShopify($shopifyid)
: Check if a product exists on ShopifyreOrderProductImages($params)
: Reorder product imagesgraphqlCreateProductImage($images, $productShopifyId)
: Add Product ImagegraphqlDeleteProductImage(imageIds, $productShopifyId)
: Delete Product Image
Variants
graphqlDeleteVariant($shopifyid, $variantid)
: Delete a variantgraphqlGetProductVariants($shopifyid)
: Get all variants for a productgraphqlGetVariant($variantid)
: Get a single variant by IDgetProductIdFromVairant($variantid)
: Get product ID that a variant belongs tographqlUpdateVariant($shopifyId, $variantId, $params)
: Update a variant
Other
getCollectionHandle($collection_id)
: Get a collection's handle by ID
Example: Creating a Product
$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);
Example: Updating a Product
$updateData = [ 'product' => [ 'id' => '1234567890', 'title' => 'Updated Product Title', 'tags' => 'updated,tags', 'variants' => [ [ 'id' => '9876543210', 'price' => '29.99' ] ] ] ]; $response = $shopifyGraphql->graphqlUpdateProduct($updateData);
Example: Getting Products
$params = [ 'limit' => 10, 'vendor' => 'Example Vendor', 'published_status' => 'published', 'fields' => 'id,title,variants,tags' ]; $products = $shopifyGraphql->graphqlGetProducts($params);
Example: Add Product Image
$images = [ [ 'url' => 'https://fastly.picsum.photos/id/365/200/300.jpg?hmac=n_4DxqK0o938eabBZRnEywWtPwgF2MKoTfnRmJ7vlKQ', 'alt' => 'lorem ipsum', ] ]; $response = $shopifyGraphql->graphqlCreateProductImage($images, $productShopifyId);
Example: Delete Product Image
$imageIds = [ "4145546145154","5456564465234", ]; $response = $shopifyGraphql->graphqlDeleteProductImage($imageIds, $productShopifyId);
Endpoints
The package provides endpoint classes that map to different Shopify resources. Each endpoint class offers methods that correspond to REST API operations but utilize GraphQL under the hood.
Products
use Thalia\ShopifyRestToGraphql\Endpoints\ProductsEndpoints; $productsEndpoint = new ProductsEndpoints($shop, $accessToken);
getProducts($params)
: Get a list of products with filtering optionsgetProduct($productId)
: Get a single product by IDproductVariantsCount()
: Get the total count of product variantsdeleteAllProductImages($productId)
: Delete all images for a product
Variants
use Thalia\ShopifyRestToGraphql\Endpoints\VariantsEndpoints; $variantsEndpoint = new VariantsEndpoints($shop, $accessToken);
productVariantsBulkUpdate($shopifyId, $variantId, $params)
: Update a variantproductVariantsBulkDelete($shopifyId, $variantId)
: Delete a variant
Collections
use Thalia\ShopifyRestToGraphql\Endpoints\CollectionsEndpoints; $collectionsEndpoint = new CollectionsEndpoints($shop, $accessToken);
getSmartCollections()
: Get a list of smart collectionsgetCollection($collectionId)
: Get a single collection by IDgetCustomCollections()
: Get a list of custom collections
Orders
use Thalia\ShopifyRestToGraphql\Endpoints\OrdersEndpoints; $ordersEndpoint = new OrdersEndpoints($shop, $accessToken);
getOrders($params)
: Get a list of orders with filtering optionsgetOrder($orderId)
: Get a single order by IDcreateOrder($params)
: Create a new orderupdateOrder($orderId, $params)
: Update an existing ordercancelOrder($orderId, $params)
: Cancel an ordercloseOrder($orderId)
: Close an orderreopenOrder($orderId)
: Reopen a closed order
Inventory
use Thalia\ShopifyRestToGraphql\Endpoints\InventoryEndpoints; $inventoryEndpoint = new InventoryEndpoints($shop, $accessToken);
getInventoryLevels($params)
: Get inventory levelsadjustInventoryLevel($params)
: Adjust inventory levelgetInventoryItem($inventoryItemId)
: Get inventory item by IDupdateInventoryItem($inventoryItemId, $params)
: Update inventory item
ApplicationCharges
use Thalia\ShopifyRestToGraphql\Endpoints\ApplicationChargesEndpoints; $chargesEndpoint = new ApplicationChargesEndpoints($shop, $accessToken);
appPurchaseOneTimeCreate($params)
: Create a one-time application chargecurrentAppInstallationForOneTime($chargeId)
: Get details of a one-time charge
Metafields
use Thalia\ShopifyRestToGraphql\Endpoints\MetafieldsEndpoints; $metafieldsEndpoint = new MetafieldsEndpoints($shop, $accessToken);
getMetafields($params)
: Get metafieldsgetMetafield($metafieldId)
: Get a single metafieldcreateMetafield($params)
: Create a metafieldupdateMetafield($metafieldId, $params)
: Update a metafielddeleteMetafield($metafieldId)
: Delete a metafield
Fulfillments
use Thalia\ShopifyRestToGraphql\Endpoints\FulfillmentsEndpoints; $fulfillmentsEndpoint = new FulfillmentsEndpoints($shop, $accessToken);
createFulfillment($params)
: Create a fulfillmentupdateFulfillment($fulfillmentId, $params)
: Update a fulfillmentgetFulfillment($fulfillmentId)
: Get a single fulfillmentcancelFulfillment($fulfillmentId)
: Cancel a fulfillment
Webhooks
use Thalia\ShopifyRestToGraphql\Endpoints\WebhooksEndpoints; $webhooksEndpoint = new WebhooksEndpoints($shop, $accessToken);
createWebhook($params)
: Create a webhookgetWebhooks($params)
: Get webhooksgetWebhook($webhookId)
: Get a single webhookupdateWebhook($webhookId, $params)
: Update a webhookdeleteWebhook($webhookId)
: Delete a webhook
Themes
use Thalia\ShopifyRestToGraphql\Endpoints\ThemesEndpoints; $themesEndpoint = new ThemesEndpoints($shop, $accessToken);
getThemes()
: Get all themesgetTheme($themeId)
: Get a single themecreateTheme($params)
: Create a new themeupdateTheme($themeId, $params)
: Update a themedeleteTheme($themeId)
: Delete a theme
Other Endpoints
Additional endpoint classes include:
ShopEndpoints
: Shop-related operationsLocationsEndpoints
: Location managementDiscountsEndpoints
: Discount operationsScriptTagsEndPoints
: Script tag operationsRecurringApplicationChargesEndpoints
: Recurring charge operationsOauthEndpoints
: OAuth operationsOauthScopeEndpoints
: OAuth scope operationsShippingEndpoints
: Shipping operations
Contributing
We welcome contributions! To contribute:
- Fork the repository.
- Create a feature branch (
git checkout -b feature-branch
). - Commit your changes (
git commit -m 'Add new feature'
). - Push to your branch (
git push origin feature-branch
). - Open a Pull Request.