Commit 2bad7f8a authored by 王源's avatar 王源 🎧

shopify添加graphQL接口支持

parent 49c5f416
...@@ -15,6 +15,7 @@ use Meibuyu\Micro\Shopify\lib\Collection; ...@@ -15,6 +15,7 @@ use Meibuyu\Micro\Shopify\lib\Collection;
use Meibuyu\Micro\Shopify\lib\CustomCollection; use Meibuyu\Micro\Shopify\lib\CustomCollection;
use Meibuyu\Micro\Shopify\lib\Event; use Meibuyu\Micro\Shopify\lib\Event;
use Meibuyu\Micro\Shopify\lib\FulfillmentService; use Meibuyu\Micro\Shopify\lib\FulfillmentService;
use Meibuyu\Micro\Shopify\lib\Graphql;
use Meibuyu\Micro\Shopify\lib\InventoryItem; use Meibuyu\Micro\Shopify\lib\InventoryItem;
use Meibuyu\Micro\Shopify\lib\InventoryLevel; use Meibuyu\Micro\Shopify\lib\InventoryLevel;
use Meibuyu\Micro\Shopify\lib\Location; use Meibuyu\Micro\Shopify\lib\Location;
...@@ -43,6 +44,7 @@ use Meibuyu\Micro\Shopify\lib\Webhook; ...@@ -43,6 +44,7 @@ use Meibuyu\Micro\Shopify\lib\Webhook;
* @property-read Order $Order * @property-read Order $Order
* @property-read Event $Event * @property-read Event $Event
* @property-read FulfillmentService $FulfillmentService * @property-read FulfillmentService $FulfillmentService
* @property-read GraphQL $GraphQL
* *
* @method Webhook Webhook(integer $id = null) * @method Webhook Webhook(integer $id = null)
* @method Collection Collection(integer $id = null) * @method Collection Collection(integer $id = null)
...@@ -57,6 +59,7 @@ use Meibuyu\Micro\Shopify\lib\Webhook; ...@@ -57,6 +59,7 @@ use Meibuyu\Micro\Shopify\lib\Webhook;
* @method Order Order(integer $id = null) * @method Order Order(integer $id = null)
* @method Event Event(integer $id = null) * @method Event Event(integer $id = null)
* @method FulfillmentService FulfillmentService(integer $id = null) * @method FulfillmentService FulfillmentService(integer $id = null)
* @method GraphQL GraphQL()
* *
*/ */
class ShopifyApp class ShopifyApp
...@@ -77,6 +80,7 @@ class ShopifyApp ...@@ -77,6 +80,7 @@ class ShopifyApp
'Order', 'Order',
'Event', 'Event',
'FulfillmentService', 'FulfillmentService',
'GraphQL',
]; ];
protected $childResources = array( protected $childResources = array(
......
...@@ -55,7 +55,7 @@ abstract class AbstractShopify ...@@ -55,7 +55,7 @@ abstract class AbstractShopify
{ {
$this->config = $config; $this->config = $config;
$this->id = $id; $this->id = $id;
$this->pluralizeKey = $this->resourceKey . 's'; $this->pluralizeKey = $this->pluralizeKey ?: $this->resourceKey . 's';
$this->resourceUrl = ($parentResourceUrl ? "$parentResourceUrl/" : $config['api_url']) . $this->pluralizeKey . ($this->id ? "/{$this->id}" : ''); $this->resourceUrl = ($parentResourceUrl ? "$parentResourceUrl/" : $config['api_url']) . $this->pluralizeKey . ($this->id ? "/{$this->id}" : '');
if (isset($config['api_password'])) { if (isset($config['api_password'])) {
......
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2021/01/08
* Time: 09:34:30
*/
namespace Meibuyu\Micro\Shopify\lib;
use Exception;
use Meibuyu\Micro\Shopify\tools\HttpRequestGraphQL;
/**
* Class Graphql
* @package Meibuyu\Micro\Shopify\lib
*/
class Graphql extends AbstractShopify
{
protected $resourceKey = 'graphql';
protected $pluralizeKey = 'graphql';
public function post($graphQL, $url = null, $wrapData = false, $variables = null)
{
if (!$url) $url = $this->generateUrl();
$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders, $variables);
return $this->processResponse($response);
}
public function get($urlParams = array(), $url = null, $dataKey = null)
{
throw new Exception("GraphQL 只支持 POST 请求!");
}
public function put($id, $dataArray, $url = null, $wrapData = true)
{
throw new Exception("GraphQL 只支持 POST 请求!");
}
public function delete($id = null, $urlParams = [], $url = null)
{
throw new Exception("GraphQL 只支持 POST 请求!");
}
}
<?php
/**
* Created by PhpStorm.
* User: Zero
* Time: 2021/1/11 10:10
*/
namespace Meibuyu\Micro\Shopify\tools;
use Meibuyu\Micro\Tools\CurlRequest;
use Meibuyu\Micro\Tools\HttpRequestJson;
class HttpRequestGraphQL extends HttpRequestJson
{
protected static function prepareRequest($headers = [], $data = [], $variables = null)
{
if (is_string($data)) {
$postDataGraphQL = $data;
} else {
throw new \Exception("Only GraphQL string is allowed!");
}
if (is_array($variables)) {
$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]);
$headers['Content-type'] = 'application/json';
} else {
$headers['Content-type'] = 'application/graphql';
}
return [$headers, $postDataGraphQL];
}
public static function post($url, $data, $headers = [], $variables = null)
{
[$headers, $postDataGraphQL] = self::prepareRequest($headers, $data, $variables);
return CurlRequest::post($url, $postDataGraphQL, $headers);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment