From 0244f582bab4cc7d95c96f97184e91ae2cae1d95 Mon Sep 17 00:00:00 2001 From: Alexey Sinkevich <2714877+seka19@users.noreply.github.com> Date: Wed, 21 Aug 2019 21:35:09 +0300 Subject: [PATCH] Add parameter $variables to the GraphQL::post() --- composer.json | 3 ++- lib/GraphQL.php | 5 +++-- lib/HttpRequestGraphQL.php | 17 ++++++++++++----- lib/HttpRequestJson.php | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 04d06e6..6fdced1 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "type": "library", "require": { "php": ">=5.6", - "ext-curl": "*" + "ext-curl": "*", + "ext-json": "*" }, "license": "Apache-2.0", "authors": [ diff --git a/lib/GraphQL.php b/lib/GraphQL.php index e6fa430..7d49d25 100644 --- a/lib/GraphQL.php +++ b/lib/GraphQL.php @@ -30,16 +30,17 @@ protected function getResourcePath() * @param string $graphQL A valid GraphQL String. @see https://help.shopify.com/en/api/graphql-admin-api/graphiql-builder GraphiQL builder - you can build your graphql string from here. * @param string $url * @param bool $wrapData + * @param array|null $variables * * @uses HttpRequestGraphQL::post() to send the HTTP request * * @return array */ - public function post($graphQL, $url = null, $wrapData = false) + public function post($graphQL, $url = null, $wrapData = false, $variables = null) { if (!$url) $url = $this->generateUrl(); - $response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders); + $response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders, $variables); return $this->processResponse($response); } diff --git a/lib/HttpRequestGraphQL.php b/lib/HttpRequestGraphQL.php index b4595a8..8aef907 100644 --- a/lib/HttpRequestGraphQL.php +++ b/lib/HttpRequestGraphQL.php @@ -23,14 +23,15 @@ class HttpRequestGraphQL extends HttpRequestJson /** * Prepare the data and request headers before making the call * - * @param mixed $data * @param array $httpHeaders + * @param mixed $data + * @param array|null $variables * * @return void * * @throws SdkException if $data is not a string */ - protected static function prepareRequest($httpHeaders = array(), $data = array()) + protected static function prepareRequest($httpHeaders = array(), $data = array(), $variables = null) { if (is_string($data)) { @@ -44,8 +45,13 @@ protected static function prepareRequest($httpHeaders = array(), $data = array() } self::$httpHeaders = $httpHeaders; - self::$httpHeaders['Content-type'] = 'application/graphql'; + if (is_array($variables)) { + self::$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]); + self::$httpHeaders['Content-type'] = 'application/json'; + } else { + self::$httpHeaders['Content-type'] = 'application/graphql'; + } } /** @@ -54,12 +60,13 @@ protected static function prepareRequest($httpHeaders = array(), $data = array() * @param string $url * @param mixed $data * @param array $httpHeaders + * @param array|null $variables * * @return string */ - public static function post($url, $data, $httpHeaders = array()) + public static function post($url, $data, $httpHeaders = array(), $variables = null) { - self::prepareRequest($httpHeaders, $data); + self::prepareRequest($httpHeaders, $data, $variables); $response = CurlRequest::post($url, self::$postDataGraphQL, self::$httpHeaders); diff --git a/lib/HttpRequestJson.php b/lib/HttpRequestJson.php index 0427cc6..ee8372a 100644 --- a/lib/HttpRequestJson.php +++ b/lib/HttpRequestJson.php @@ -38,8 +38,8 @@ class HttpRequestJson /** * Prepare the data and request headers before making the call * - * @param array $dataArray * @param array $httpHeaders + * @param array $dataArray * * @return void */