Commit 2bad7f8a authored by 王源's avatar 王源
Browse files

shopify添加graphQL接口支持

parent 49c5f416
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ use Meibuyu\Micro\Shopify\lib\Collection;
use Meibuyu\Micro\Shopify\lib\CustomCollection;
use Meibuyu\Micro\Shopify\lib\Event;
use Meibuyu\Micro\Shopify\lib\FulfillmentService;
use Meibuyu\Micro\Shopify\lib\Graphql;
use Meibuyu\Micro\Shopify\lib\InventoryItem;
use Meibuyu\Micro\Shopify\lib\InventoryLevel;
use Meibuyu\Micro\Shopify\lib\Location;
@@ -43,6 +44,7 @@ use Meibuyu\Micro\Shopify\lib\Webhook;
 * @property-read Order $Order
 * @property-read Event $Event
 * @property-read FulfillmentService $FulfillmentService
 * @property-read GraphQL $GraphQL
 *
 * @method Webhook Webhook(integer $id = null)
 * @method Collection Collection(integer $id = null)
@@ -57,6 +59,7 @@ use Meibuyu\Micro\Shopify\lib\Webhook;
 * @method Order Order(integer $id = null)
 * @method Event Event(integer $id = null)
 * @method FulfillmentService FulfillmentService(integer $id = null)
 * @method GraphQL GraphQL()
 *
 */
class ShopifyApp
@@ -77,6 +80,7 @@ class ShopifyApp
        'Order',
        'Event',
        'FulfillmentService',
        'GraphQL',
    ];

    protected $childResources = array(
+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ abstract class AbstractShopify
    {
        $this->config = $config;
        $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}" : '');

        if (isset($config['api_password'])) {
+47 −0
Original line number Diff line number Diff line
<?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 请求!");
    }

}
+38 −0
Original line number Diff line number Diff line
<?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);
    }

}