luminarix/laravel-shopify-graphql
Composer 安装命令:
composer require luminarix/laravel-shopify-graphql
包简介
GraphQL client for Shopify.
README 文档
README
A Laravel package for interacting with Shopify's GraphQL Admin API. Built on Saloon with automatic rate limiting and retry handling.
Installation
composer require luminarix/laravel-shopify-graphql
Publish the config file:
php artisan vendor:publish --tag="shopify-graphql-config"
Basic Usage
use Luminarix\Shopify\GraphQLClient\Facades\GraphQLClient; use Luminarix\Shopify\GraphQLClient\Authenticators\ShopifyApp; $authenticator = new ShopifyApp($shopDomain, $accessToken); $client = GraphQLClient::factory()->create($authenticator); // Query $response = $client->query(' query { shop { name email } } '); $data = $response->toArray(); // Mutation $response = $client->mutate(' mutation orderMarkAsPaid($input: OrderMarkAsPaidInput!) { orderMarkAsPaid(input: $input) { order { id } userErrors { field message } } } ', [ 'input' => [ 'id' => 'gid://shopify/Order/123456789', ], ]);
Eloquent Model Integration
Create a service class to manage client instances:
namespace App\Services; use App\Models\ShopifyShop; use Luminarix\Shopify\GraphQLClient\Facades\GraphQLClient; use Luminarix\Shopify\GraphQLClient\Authenticators\ShopifyApp; use Luminarix\Shopify\GraphQLClient\GraphQLClientMethods; class ShopifyGraphQLService { public function with(ShopifyShop $shop): GraphQLClientMethods { $authenticator = new ShopifyApp($shop->domain, $shop->access_token); return GraphQLClient::factory()->create($authenticator); } }
Create a trait for your models:
namespace App\Traits; use App\Services\ShopifyGraphQLService; use Luminarix\Shopify\GraphQLClient\GraphQLClientMethods; trait InteractsWithShopifyGraphQL { public function graphql(): GraphQLClientMethods { return app(ShopifyGraphQLService::class)->with($this); } }
Use on your Shopify shop model:
namespace App\Models; use App\Traits\InteractsWithShopifyGraphQL; use Illuminate\Database\Eloquent\Model; class ShopifyShop extends Model { use InteractsWithShopifyGraphQL; }
Now you can call GraphQL directly from your model:
$shop = ShopifyShop::find(1); $result = $shop->graphql()->query(' query { shop { name } } ')->toArray();
Response Handling
All query and mutation methods return a GraphQLClientTransformer:
$response = $client->query($query); $response->toArray(); // array $response->toCollection(); // Illuminate\Support\Collection $response->toFluent(); // Illuminate\Support\Fluent $response->toJson(); // string $response->toDTO(MyDTO::class); // Custom DTO instance
The transformer is also equipped with the Macroable trait so you can add your own custom methods if needed.
Getting Response with Extensions
Pass withExtensions: true to include cost and rate limit data:
$response = $client->query($query, withExtensions: true); // Returns: ['data' => [...], 'extensions' => ['cost' => [...]]]
Bulk Operations
For large data exports, use bulk operations:
// Start a bulk operation $result = $client->createBulkOperation(' { products { edges { node { id title } } } } '); // Check current bulk operation status $status = $client->getCurrentBulkOperation(); // Get a specific bulk operation by ID $operation = $client->getBulkOperation($numericId); // Cancel running bulk operation $client->cancelBulkOperation();
Rate Limiting
The package automatically handles Shopify's rate limits:
- Tracks query costs from response extensions
- Waits when throttled before retrying
- Configurable retry attempts
Get current rate limit info:
$info = $client->getRateLimitInfo(); // [ // 'requestedQueryCost' => 10, // 'actualQueryCost' => 8, // 'maxAvailableLimit' => 1000, // 'lastAvailableLimit' => 992, // 'restoreRate' => 50, // 'isThrottled' => false, // ]
Custom Rate Limit Service
Implement RateLimitable to customize rate limit handling:
use Luminarix\Shopify\GraphQLClient\Contracts\RateLimitable; class CustomRateLimitService implements RateLimitable { public function getRateLimitInfo(): array { /* ... */ } public function updateRateLimitInfo(array $data): void { /* ... */ } public function calculateWaitTime(float $requestedQueryCost): float { /* ... */ } public function waitIfNecessary(float $requestedQueryCost): void { /* ... */ } } $client = GraphQLClient::factory()->create($authenticator, new CustomRateLimitService());
Configuration
// config/shopify-graphql.php return [ // Shopify API version (format: YYYY-MM) 'api_version' => env('SHOPIFY_API_VERSION', '2025-01'), // Fail immediately when throttled (vs. waiting and retrying) 'fail_on_throttled' => env('SHOPIFY_FAIL_ON_THROTTLED', true), // Max retry attempts when throttled 'throttle_max_tries' => env('SHOPIFY_THROTTLE_MAX_TRIES', 5), ];
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
luminarix/laravel-shopify-graphql 适用场景与选型建议
luminarix/laravel-shopify-graphql 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.91k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2024 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Luminarix Labs」 「laravel-shopify-graphql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 luminarix/laravel-shopify-graphql 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 luminarix/laravel-shopify-graphql 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 luminarix/laravel-shopify-graphql 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A JSON Lines parser for Laravel.
RedKiteCms is an Open Source Content Management System Application, built on top of Symfony2 and TwitterBootstrap frameworks, providing an easy an intuitive user-interface, to improve the website's content management experience
Useful enum concern.
IconScoop - find your favicons
Minimal PHP tinker, redesigned.
Blockade Labs SDK for Laravel
统计信息
- 总下载量: 7.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-20