Skip to content

Commit 0244f58

Browse files
committed
Add parameter $variables to the GraphQL::post()
1 parent 6921da0 commit 0244f58

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"type": "library",
77
"require": {
88
"php": ">=5.6",
9-
"ext-curl": "*"
9+
"ext-curl": "*",
10+
"ext-json": "*"
1011
},
1112
"license": "Apache-2.0",
1213
"authors": [

lib/GraphQL.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ protected function getResourcePath()
3030
* @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.
3131
* @param string $url
3232
* @param bool $wrapData
33+
* @param array|null $variables
3334
*
3435
* @uses HttpRequestGraphQL::post() to send the HTTP request
3536
*
3637
* @return array
3738
*/
38-
public function post($graphQL, $url = null, $wrapData = false)
39+
public function post($graphQL, $url = null, $wrapData = false, $variables = null)
3940
{
4041
if (!$url) $url = $this->generateUrl();
4142

42-
$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders);
43+
$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders, $variables);
4344

4445
return $this->processResponse($response);
4546
}

lib/HttpRequestGraphQL.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ class HttpRequestGraphQL extends HttpRequestJson
2323
/**
2424
* Prepare the data and request headers before making the call
2525
*
26-
* @param mixed $data
2726
* @param array $httpHeaders
27+
* @param mixed $data
28+
* @param array|null $variables
2829
*
2930
* @return void
3031
*
3132
* @throws SdkException if $data is not a string
3233
*/
33-
protected static function prepareRequest($httpHeaders = array(), $data = array())
34+
protected static function prepareRequest($httpHeaders = array(), $data = array(), $variables = null)
3435
{
3536

3637
if (is_string($data)) {
@@ -44,8 +45,13 @@ protected static function prepareRequest($httpHeaders = array(), $data = array()
4445
}
4546

4647
self::$httpHeaders = $httpHeaders;
47-
self::$httpHeaders['Content-type'] = 'application/graphql';
4848

49+
if (is_array($variables)) {
50+
self::$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]);
51+
self::$httpHeaders['Content-type'] = 'application/json';
52+
} else {
53+
self::$httpHeaders['Content-type'] = 'application/graphql';
54+
}
4955
}
5056

5157
/**
@@ -54,12 +60,13 @@ protected static function prepareRequest($httpHeaders = array(), $data = array()
5460
* @param string $url
5561
* @param mixed $data
5662
* @param array $httpHeaders
63+
* @param array|null $variables
5764
*
5865
* @return string
5966
*/
60-
public static function post($url, $data, $httpHeaders = array())
67+
public static function post($url, $data, $httpHeaders = array(), $variables = null)
6168
{
62-
self::prepareRequest($httpHeaders, $data);
69+
self::prepareRequest($httpHeaders, $data, $variables);
6370

6471
$response = CurlRequest::post($url, self::$postDataGraphQL, self::$httpHeaders);
6572

lib/HttpRequestJson.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class HttpRequestJson
3838
/**
3939
* Prepare the data and request headers before making the call
4040
*
41-
* @param array $dataArray
4241
* @param array $httpHeaders
42+
* @param array $dataArray
4343
*
4444
* @return void
4545
*/

0 commit comments

Comments
 (0)